import Foundation

#if os(iOS) && targetEnvironment(simulator)

struct RunnerSystemPromptStrings {
  let english: [String: String]

  let localized: [String: [String: String]]



  static let promptTables = [
    (path: "/System/Library/PrivateFrameworks/UserNotificationsServer.framework", table: "Localizable"),
    (path: "/System/Library/PrivateFrameworks/TCC.framework", table: "Localizable"),
    (path: "/System/Library/Frameworks/CoreLocation.framework", table: "locationd")
  ]



  static let launchConfirmationTable = (
    path: "/System/Library/Frameworks/CoreServices.framework",
    table: "SchemeApproval"
  )



  static let launchConfirmationLabelKeys: Set<String> = ["SCHEME_APPROVAL_OPEN_BUTTON", "SCHEME_APPROVAL_DO_NOT_OPEN_BUTTON"]






  static func read(
    _ tables: [(path: String, table: String)],
    languages: [String]
  ) -> Result<[RunnerSystemPromptStrings], UnavailableSource> {
    let foundationPath = "/System/Library/Frameworks/Foundation.framework"
    guard let foundation = Bundle(identifier: "com.apple.Foundation")?.bundlePath, foundation.hasSuffix(foundationPath)
    else { return .failure(UnavailableSource(source: foundationPath)) }
    let root = String(foundation.dropLast(foundationPath.count))
    var read: [RunnerSystemPromptStrings] = []
    for (path, table) in tables {
      guard let bundle = Bundle(path: root + path), let english = strings(in: bundle, table: table, localization: "en")
      else { return .failure(UnavailableSource(source: path)) }
      let localizations = Set(languages.compactMap {
        Bundle.preferredLocalizations(from: bundle.localizations, forPreferences: [$0]).first
      }).subtracting(["en"])
      var localized: [String: [String: String]] = [:]
      for localization in localizations {
        guard let strings = strings(in: bundle, table: table, localization: localization)
        else { return .failure(UnavailableSource(source: "\(path) \(localization)")) }
        localized[localization] = strings
      }
      read.append(RunnerSystemPromptStrings(english: english, localized: localized))
    }
    return .success(read)
  }


  func limited(to keys: Set<String>) -> RunnerSystemPromptStrings {
    RunnerSystemPromptStrings(
      english: english.filter { keys.contains($0.key) },
      localized: localized.mapValues { $0.filter { keys.contains($0.key) } }
    )
  }

  struct UnavailableSource: Error {
    let source: String
  }

  static func logUnavailable(_ failure: UnavailableSource, subsystem: String, languages: [String]) {
    NSLog(
      "AGENT_DEVICE_RUNNER_SYSTEM_PROMPT_STRINGS_UNAVAILABLE subsystem=%@ source=%@ languages=%@",
      subsystem, failure.source, languages.joined(separator: ",")
    )
  }

  private static func strings(in bundle: Bundle, table: String, localization: String) -> [String: String]? {
    guard let url = bundle.url(forResource: table, withExtension: "strings", subdirectory: nil, localization: localization)
    else { return nil }
    return NSDictionary(contentsOf: url) as? [String: String]
  }
}
#endif
