"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFormContext = exports.FormContext = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const acui_1 = require("@Acme Corporation/acui");
exports.FormContext = (0, react_1.createContext)(null);
const useFormContext = () => {
    const context = (0, react_1.useContext)(exports.FormContext);
    if (!context) {
        throw new Error('useFormContext must be used within a FormProvider');
    }
    return context;
};
exports.useFormContext = useFormContext;
const Form = (0, react_1.memo)(({ onSubmit, onReset, initialValues = {}, validate, children, className = '', ariaLabel, role = 'form', noValidate = true, autoComplete = 'off', method = 'post', action, encType = 'application/x-www-form-urlencoded' }) => {
    const theme = (0, acui_1.useTheme)();
    const [isSubmitting, setIsSubmitting] = react_1.default.useState(false);
    const [isDirty, setIsDirty] = react_1.default.useState(false);
    const [values, setValues] = react_1.default.useState(initialValues);
    const [errors, setErrors] = react_1.default.useState({});
    const [touched, setTouched] = react_1.default.useState({});
    const setFieldValue = (0, react_1.useCallback)((field, value) => {
        setValues(prev => {
            const newValues = { ...prev, [field]: value };
            setIsDirty(true);
            if (validate) {
                const validationErrors = validate(newValues);
                setErrors(validationErrors);
            }
            return newValues;
        });
    }, [validate]);
    const setFieldTouched = (0, react_1.useCallback)((field, isTouched) => {
        setTouched(prev => ({ ...prev, [field]: isTouched }));
    }, []);
    const setFieldError = (0, react_1.useCallback)((field, error) => {
        setErrors(prev => ({ ...prev, [field]: error }));
    }, []);
    const handleSubmit = (0, react_1.useCallback)(async (e) => {
        e.preventDefault();
        if (isSubmitting)
            return;
        setIsSubmitting(true);
        try {
            if (validate) {
                const validationErrors = validate(values);
                if (Object.keys(validationErrors).length > 0) {
                    setErrors(validationErrors);
                    return;
                }
            }
            await onSubmit(values);
        }
        catch (error) {
            console.error('Form submission error:', error);
        }
        finally {
            setIsSubmitting(false);
        }
    }, [isSubmitting, onSubmit, validate, values]);
    const handleReset = (0, react_1.useCallback)(() => {
        setValues(initialValues);
        setErrors({});
        setTouched({});
        setIsDirty(false);
        onReset?.();
    }, [initialValues, onReset]);
    const contextValue = react_1.default.useMemo(() => ({
        isSubmitting,
        isDirty,
        errors,
        touched,
        values,
        setFieldValue,
        setFieldTouched,
        setFieldError,
        handleSubmit,
        handleReset
    }), [
        isSubmitting,
        isDirty,
        errors,
        touched,
        values,
        setFieldValue,
        setFieldTouched,
        setFieldError,
        handleSubmit,
        handleReset
    ]);
    return ((0, jsx_runtime_1.jsx)(exports.FormContext.Provider, { value: contextValue, children: (0, jsx_runtime_1.jsx)("form", { className: `bf-form ${className}`, onSubmit: handleSubmit, onReset: handleReset, role: role, "aria-label": ariaLabel, noValidate: noValidate, autoComplete: autoComplete, method: method, action: action, encType: encType, children: children }) }));
});
Form.displayName = 'Form';
exports.default = Form;
//# sourceMappingURL=Form.js.map