import Foundation /// A text replacement implementation. Accessibility direct replacement is preferred; the paste /// fallback is the second strategy (`docs/SELECTION_AND_CLIPBOARD.md` ยง4). public protocol TextInserting: Sendable { /// Replaces the captured selection with `translation`. func replace( selection: CapturedText, with translation: String, target: InsertionTarget ) async throws -> InsertionOutcome } /// Captures the source app and, when Accessibility is trusted, the focused selection at trigger /// time. Never reads a full document value. @MainActor public protocol InsertionTargetProviding { /// Returns the target, or nil when no app is frontmost. func captureTarget() -> InsertionTarget? } /// The safety checks shared by AX and paste insertion. public enum InsertionPolicy { /// Verifies the frontmost app and, when available, the selected text. public static func verify( frontmostPID: pid_t, target: InsertionTarget, selectedText: String?, source: String ) throws { guard frontmostPID == target.pid else { throw SprachmittlerError.insertionTargetChanged } guard let selectedText else { return } let expected = selectedText .replacingOccurrences(of: "\r\n", with: "\n") .replacingOccurrences(of: "\r", with: "\n") .trimmingCharacters(in: .whitespacesAndNewlines) let actual = source .replacingOccurrences(of: "\r\n", with: "\n") .replacingOccurrences(of: "\r", with: "\n") .trimmingCharacters(in: .whitespacesAndNewlines) guard expected == actual else { throw SprachmittlerError.insertionTargetChanged } } /// True when the text should not be replaced because a blocking warning exists. public static func isBlocked(by warnings: [TranslationWarning]) -> Bool { warnings.contains(where: \.blocksReplacement) } }