import Foundation import SprachmittlerEngines private struct DecodedDirection { let autoSwitch: Bool let fallback: String } extension AppSettings { public init(from decoder: any Decoder) throws { let defaults = AppSettings() let container = try decoder.container(keyedBy: CodingKeys.self) endpoint = try container.decodeIfPresent(URL.self, forKey: .endpoint) ?? defaults.endpoint allowRemoteEndpoint = try container.decodeIfPresent(Bool.self, forKey: .allowRemoteEndpoint) ?? defaults.allowRemoteEndpoint model = try container.decodeIfPresent(String.self, forKey: .model) ?? defaults.model fastModel = container.contains(.fastModel) ? try container.decodeIfPresent(String.self, forKey: .fastModel) : defaults.fastModel let decodedTarget = try container.decodeIfPresent(String.self, forKey: .targetLanguage) ?? defaults.targetLanguage targetLanguage = HyMT3Languages.resolve(decodedTarget)?.code ?? defaults.targetLanguage let direction = try Self.decodeDirection(from: container, defaults: defaults, target: targetLanguage) autoSwitchTarget = direction.autoSwitch fallbackTargetLanguage = direction.fallback recentTargetLanguages = try Self.decodeRecentTargets(from: container) let limits = try Self.decodeLimits(from: container, defaults: defaults) doubleCopyWindowMs = limits.window maxCharactersLatin = limits.latin maxCharactersCJK = limits.cjk numCtx = limits.numCtx numPredict = limits.numPredict translationFontSize = limits.fontSize let sampling = try Self.decodeSampling(from: container, defaults: defaults) temperature = sampling.temperature topP = sampling.topP topK = sampling.topK repeatPenalty = sampling.repeatPenalty let keep = try container.decodeIfPresent(String.self, forKey: .keepModelLoaded) ?? defaults.keepModelLoaded keepModelLoaded = ["5m", "30m", "1h", "0"].contains(keep) ? keep : defaults.keepModelLoaded warmUpOnLaunch = try container.decodeIfPresent(Bool.self, forKey: .warmUpOnLaunch) ?? defaults.warmUpOnLaunch let policy = try container.decodeIfPresent(PromptVariantPolicy.self, forKey: .promptVariantPolicy) ?? defaults.promptVariantPolicy promptVariantPolicy = policy // Auto-copy is a hard invariant: decoding cannot turn it on. autoCopy = false showSourcePreview = try container.decodeIfPresent(Bool.self, forKey: .showSourcePreview) ?? defaults.showSourcePreview renderMarkdown = try container.decodeIfPresent(Bool.self, forKey: .renderMarkdown) ?? defaults.renderMarkdown closeWithEscape = try container.decodeIfPresent(Bool.self, forKey: .closeWithEscape) ?? defaults.closeWithEscape launchAtLogin = try container.decodeIfPresent(Bool.self, forKey: .launchAtLogin) ?? defaults.launchAtLogin keepRecentTranslationInMemory = try container.decodeIfPresent( Bool.self, forKey: .keepRecentTranslationInMemory ) ?? defaults.keepRecentTranslationInMemory hasCompletedOnboarding = try container.decodeIfPresent(Bool.self, forKey: .hasCompletedOnboarding) ?? defaults.hasCompletedOnboarding settingsVersion = Self.currentVersion } public func encode(to encoder: any Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(endpoint, forKey: .endpoint) try container.encode(allowRemoteEndpoint, forKey: .allowRemoteEndpoint) try container.encode(model, forKey: .model) try container.encode(fastModel, forKey: .fastModel) try container.encode(targetLanguage, forKey: .targetLanguage) try container.encode(autoSwitchTarget, forKey: .autoSwitchTarget) try container.encode(fallbackTargetLanguage, forKey: .fallbackTargetLanguage) try container.encode(recentTargetLanguages, forKey: .recentTargetLanguages) try container.encode(doubleCopyWindowMs, forKey: .doubleCopyWindowMs) try container.encode(maxCharactersLatin, forKey: .maxCharactersLatin) try container.encode(maxCharactersCJK, forKey: .maxCharactersCJK) try container.encode(numCtx, forKey: .numCtx) try container.encode(numPredict, forKey: .numPredict) try container.encode(temperature, forKey: .temperature) try container.encode(topP, forKey: .topP) try container.encode(topK, forKey: .topK) try container.encode(repeatPenalty, forKey: .repeatPenalty) try container.encode(keepModelLoaded, forKey: .keepModelLoaded) try container.encode(warmUpOnLaunch, forKey: .warmUpOnLaunch) try container.encode(promptVariantPolicy, forKey: .promptVariantPolicy) try container.encode(autoCopy, forKey: .autoCopy) try container.encode(showSourcePreview, forKey: .showSourcePreview) try container.encode(translationFontSize, forKey: .translationFontSize) try container.encode(renderMarkdown, forKey: .renderMarkdown) try container.encode(closeWithEscape, forKey: .closeWithEscape) try container.encode(launchAtLogin, forKey: .launchAtLogin) try container.encode(keepRecentTranslationInMemory, forKey: .keepRecentTranslationInMemory) try container.encode(hasCompletedOnboarding, forKey: .hasCompletedOnboarding) try container.encode(settingsVersion, forKey: .settingsVersion) } private static func decodeDirection( from container: KeyedDecodingContainer, defaults: AppSettings, target: String ) throws -> DecodedDirection { let autoSwitch = try container.decodeIfPresent(Bool.self, forKey: .autoSwitchTarget) ?? defaults.autoSwitchTarget let code = try container.decodeIfPresent(String.self, forKey: .fallbackTargetLanguage) ?? defaults.fallbackTargetLanguage return DecodedDirection( autoSwitch: autoSwitch, fallback: AppSettings.normalizedFallback(code, target: target) ) } private static func decodeRecentTargets( from container: KeyedDecodingContainer ) throws -> [String] { let recents = try container.decodeIfPresent([String].self, forKey: .recentTargetLanguages) ?? [] var seen = Set() return recents.compactMap { HyMT3Languages.resolve($0)?.code } .filter { seen.insert($0).inserted } .prefix(3) .map(\.self) } }