import Foundation
















enum RunnerAlertButtonChoice {
  private static let acceptPreferences = compiled([
    "^allow while using (?:the )?app$", "^while using the app$", "^allow all$",
    "^always allow$", "^allow all the time$", "^allow notifications$", "^allow$",
    "^ok$", "^yes$", "^continue$", "^save$", "^confirm", "^turn on$",
    "^open settings$", "^done$", "^open$", "^allow once$", "^only this time$"
  ])
  private static let dismissPreferences = compiled([
    "^cancel$", "^deny$", "^don.?t allow$", "^not now$", "^no$", "^dismiss$",
    "^close$", "^close app$", "^later$", "^skip$", "^ask app not to track$", "^keep browsing$"
  ])

  static func buttonIndex(
    for labels: [String],
    action: String,
    popupMarkerSurface: Bool = false,
    vocabulary: RunnerAlertLabelVocabulary
  ) -> Int? {
    let preferences = action == "accept" ? acceptPreferences : dismissPreferences
    let candidates = labels.map { [$0] + vocabulary.englishLabels(for: $0) }
    for pattern in preferences {
      let matchingIndices = labels.indices.filter { candidates[$0].contains { matches($0, pattern: pattern) } }
      if matchingIndices.count > 1 { return nil }
      if let index = matchingIndices.first { return index }
    }
    return action != "accept" && popupMarkerSurface ? labels.indices.last : nil
  }




  static func action(of label: String, wholeLabel: Bool = false) -> String? {
    if acceptPreferences.contains(where: { matches(label, pattern: $0, wholeLabel: wholeLabel) }) { return "accept" }
    if dismissPreferences.contains(where: { matches(label, pattern: $0, wholeLabel: wholeLabel) }) { return "dismiss" }
    return nil
  }

  private static func compiled(_ patterns: [String]) -> [NSRegularExpression] {
    patterns.map { try! NSRegularExpression(pattern: $0, options: .caseInsensitive) }
  }

  private static func matches(_ label: String, pattern: NSRegularExpression, wholeLabel: Bool = false) -> Bool {
    let trimmed = label.trimmingCharacters(in: .whitespacesAndNewlines)
    let range = NSRange(trimmed.startIndex..., in: trimmed)
    guard let match = pattern.firstMatch(in: trimmed, range: range) else { return false }
    return !wholeLabel || match.range == range
  }
}
