import SwiftUI

/// A view modifier that adds a keyboard toolbar with a "Done" button to dismiss the keyboard.
/// Use this for numeric keyboards (.numberPad, .decimalPad, .phonePad) and multi-line text fields
/// that don't have a return key for dismissal.
struct KeyboardDismissToolbar: ViewModifier {
    func body(content: Content) -> some View {
        content
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("Done") {
                        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                    }
                    .foregroundColor(Color.appPrimary)
                    .fontWeight(.medium)
                }
            }
    }
}

extension View {
    /// Adds a keyboard toolbar with a "Done" button to dismiss the keyboard.
    /// Use this for numeric keyboards and multi-line text fields.
    func keyboardDismissToolbar() -> some View {
        modifier(KeyboardDismissToolbar())
    }
}
