"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const useContentMemo = require("../../hooks/useContentMemo.cjs");
const useSSRLayoutEffect = require("../../hooks/useSSRLayoutEffect.cjs");
const data = require("../utils/data.cjs");
const keyChain = require("../utils/keyChain.cjs");
const touchedState = require("../utils/touchedState.cjs");
const validation = require("../utils/validation.cjs");
const useFormBase = require("./useFormBase.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
function useForm(initialDataProp, formConfig) {
  const initialData = useContentMemo.useContentMemo(initialDataProp);
  const [formState, setFormState] = React__namespace.useState(data.initialDataIsCallback(initialData) ? initialData() : initialData ?? {});
  const [clientValidationErrors, clientValidationDispatcher] = React__namespace.useReducer(validation.clientValidationReducer, []);
  const [touchedState$1, setTouchedState] = React__namespace.useState([]);
  const formStateRef = React__namespace.useRef(formState);
  const touchedStateRef = React__namespace.useRef(touchedState$1);
  const liveInitialDataObject = React__namespace.useMemo(() => {
    return data.initialDataIsCallback(initialData) ? initialData(formStateRef.current) : initialData ?? {};
  }, [initialData]);
  const liveInitialData = useContentMemo.useContentMemo(liveInitialDataObject);
  const dispatch = React__namespace.useCallback((action) => {
    formStateRef.current = data.dataReducer(formStateRef.current, action);
    setFormState(formStateRef.current);
    return formStateRef.current;
  }, [setFormState]);
  const touchedStateDispatcher = React__namespace.useCallback((action) => {
    touchedStateRef.current = touchedState.touchedStateReducer(touchedStateRef.current, action);
    setTouchedState(touchedStateRef.current);
    return touchedStateRef.current;
  }, [setTouchedState]);
  useSSRLayoutEffect.useDidUpdateSSRLayoutEffect(() => {
    dispatch({ type: "set-all", data: liveInitialData });
  }, [liveInitialData]);
  const zodValidationSchema = React__namespace.useMemo(() => {
    const initialSchema = formConfig == null ? void 0 : formConfig.validationSchema;
    if (initialSchema) {
      const finalSchema = validation.rootValidationSchemaIsFunction(initialSchema) ? initialSchema(formState) : initialSchema;
      return validation.zodFromValidationSchema(finalSchema);
    }
    return void 0;
  }, [formConfig == null ? void 0 : formConfig.validationSchema, formState]);
  const parseZodSchema = React__namespace.useCallback((keyChain$1 = [], silent) => {
    if (!zodValidationSchema) {
      throw new Error("No validation schema has been provided");
    }
    const keyChainString = keyChain$1.length ? keyChain.keyStringFromKeyChain(keyChain$1, "dots") : void 0;
    if (!silent) {
      clientValidationDispatcher({
        type: "clear-validation",
        key: keyChainString
      });
    }
    const results = zodValidationSchema.safeParse(formStateRef.current ?? {});
    let errors = [];
    if (!results.success) {
      errors = validation.getMyZodErrors(results.error.errors, keyChainString);
      if (errors.length && !silent) {
        clientValidationDispatcher({ type: "add-validation", errors });
      }
    }
    const valid = !errors.length;
    if (formConfig == null ? void 0 : formConfig.logSchemaErrors) {
      console.log(`Armstrong validation log`, {
        zodResults: results,
        errors,
        valid,
        keyChainString,
        silent,
        keyChain: keyChain$1
      });
    }
    return valid;
  }, [zodValidationSchema, clientValidationDispatcher, formStateRef, formConfig == null ? void 0 : formConfig.logSchemaErrors]);
  const parseValidationSchema = (keyChain2, silent) => {
    if (zodValidationSchema) {
      return parseZodSchema(keyChain2, silent);
    }
    return true;
  };
  return useFormBase.useFormBase(formState, formStateRef, dispatch, clientValidationErrors, clientValidationDispatcher, touchedState$1, touchedStateRef, touchedStateDispatcher, parseValidationSchema, liveInitialData, formConfig);
}
exports.useForm = useForm;
//# sourceMappingURL=useForm.cjs.map
