//
//  PasswordPrompt.swift
//  SwanDesk — synchronous NSAlert-based password prompt.
//
//  Works from any call site (menu bar action, toolbar button) without a SwiftUI
//  sheet hierarchy. Returns nil if the user cancels or leaves the field empty.
//

import AppKit

enum PasswordPrompt {
    @MainActor
    static func request(for profile: VPNProfile) -> String? {
        let label = profile.authMethod.passwordLabel

        let alert = NSAlert()
        alert.messageText = profile.name
        alert.informativeText = "Enter \(label) to connect."
        alert.addButton(withTitle: "Connect")
        alert.addButton(withTitle: "Cancel")

        let field = NSSecureTextField(frame: NSRect(x: 0, y: 0, width: 280, height: 24))
        field.placeholderString = label
        alert.accessoryView = field
        alert.window.initialFirstResponder = field

        guard alert.runModal() == .alertFirstButtonReturn else { return nil }
        let value = field.stringValue
        return value.isEmpty ? nil : value
    }
}
