//
//  ChargedDeviceEditorScaffoldView.swift
//  USB Meter
//
//  Created by Codex on 22/04/2026.
//

import SwiftUI

struct ChargedDeviceEditorScaffoldView<Content: View>: View {
    @Environment(\.dismiss) private var dismiss

    let title: String
    let saveButtonTitle: String
    let canSave: Bool
    let standalone: Bool
    let save: () -> Void
    @ViewBuilder let content: () -> Content

    var body: some View {
        if standalone {
            NavigationView { formContent }
                .navigationViewStyle(StackNavigationViewStyle())
        } else {
            formContent
        }
    }

    private var formContent: some View {
        Form {
            content()
        }
        .navigationTitle(title)
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .cancellationAction) {
                Button("Cancel") {
                    dismiss()
                }
            }
            ToolbarItem(placement: .confirmationAction) {
                Button(saveButtonTitle, action: save)
                    .disabled(!canSave)
            }
        }
    }
}
