import SprachmittlerText /// The effective target for one translation, after an optional reverse-direction switch. public struct TranslationDirection: Sendable, Equatable { /// The language the request will translate into. public let target: ModelLanguage /// True when the configured target was replaced by the fallback target. public let didSwitch: Bool /// The configured target when `didSwitch` is true. public let originalTarget: ModelLanguage? /// Creates a direction. public init(target: ModelLanguage, didSwitch: Bool, originalTarget: ModelLanguage?) { self.target = target self.didSwitch = didSwitch self.originalTarget = originalTarget } } /// Chooses the direction for a capture. Detection stays advisory: a switch needs the same /// high-confidence agreement as the same-language advisory. public enum TranslationDirectionPolicy { /// Resolves the target. /// /// - Parameters: /// - configuredTarget: The user's saved target language. /// - fallbackTarget: The language to use when the source is already the configured target. /// - detection: The advisory source-language detection. /// - autoSwitch: Whether the fallback is enabled. /// - override: True after an explicit target choice for the current capture. public static func resolve( configuredTarget: ModelLanguage, fallbackTarget: ModelLanguage?, detection: LanguageDetection?, autoSwitch: Bool, override: Bool ) -> TranslationDirection { guard autoSwitch, !override, let fallbackTarget, fallbackTarget.code.caseInsensitiveCompare(configuredTarget.code) != .orderedSame, let detection, LanguageMatching.isSameLanguage(detection, target: configuredTarget) else { return TranslationDirection(target: configuredTarget, didSwitch: false, originalTarget: nil) } return TranslationDirection(target: fallbackTarget, didSwitch: true, originalTarget: configuredTarget) } }