import * as React from "react";
import { useContentMemo } from "../../hooks/useContentMemo.js";
import { useDidUpdateSSRLayoutEffect } from "../../hooks/useSSRLayoutEffect.js";
import { initialDataIsCallback, dataReducer } from "../utils/data.js";
import { keyStringFromKeyChain } from "../utils/keyChain.js";
import { touchedStateReducer } from "../utils/touchedState.js";
import { clientValidationReducer, rootValidationSchemaIsFunction, zodFromValidationSchema, getMyZodErrors } from "../utils/validation.js";
import { useFormBase } from "./useFormBase.js";
function useForm(initialDataProp, formConfig) {
  const initialData = useContentMemo(initialDataProp);
  const [formState, setFormState] = React.useState(initialDataIsCallback(initialData) ? initialData() : initialData ?? {});
  const [clientValidationErrors, clientValidationDispatcher] = React.useReducer(clientValidationReducer, []);
  const [touchedState, setTouchedState] = React.useState([]);
  const formStateRef = React.useRef(formState);
  const touchedStateRef = React.useRef(touchedState);
  const liveInitialDataObject = React.useMemo(() => {
    return initialDataIsCallback(initialData) ? initialData(formStateRef.current) : initialData ?? {};
  }, [initialData]);
  const liveInitialData = useContentMemo(liveInitialDataObject);
  const dispatch = React.useCallback((action) => {
    formStateRef.current = dataReducer(formStateRef.current, action);
    setFormState(formStateRef.current);
    return formStateRef.current;
  }, [setFormState]);
  const touchedStateDispatcher = React.useCallback((action) => {
    touchedStateRef.current = touchedStateReducer(touchedStateRef.current, action);
    setTouchedState(touchedStateRef.current);
    return touchedStateRef.current;
  }, [setTouchedState]);
  useDidUpdateSSRLayoutEffect(() => {
    dispatch({ type: "set-all", data: liveInitialData });
  }, [liveInitialData]);
  const zodValidationSchema = React.useMemo(() => {
    const initialSchema = formConfig == null ? void 0 : formConfig.validationSchema;
    if (initialSchema) {
      const finalSchema = rootValidationSchemaIsFunction(initialSchema) ? initialSchema(formState) : initialSchema;
      return zodFromValidationSchema(finalSchema);
    }
    return void 0;
  }, [formConfig == null ? void 0 : formConfig.validationSchema, formState]);
  const parseZodSchema = React.useCallback((keyChain = [], silent) => {
    if (!zodValidationSchema) {
      throw new Error("No validation schema has been provided");
    }
    const keyChainString = keyChain.length ? keyStringFromKeyChain(keyChain, "dots") : void 0;
    if (!silent) {
      clientValidationDispatcher({
        type: "clear-validation",
        key: keyChainString
      });
    }
    const results = zodValidationSchema.safeParse(formStateRef.current ?? {});
    let errors = [];
    if (!results.success) {
      errors = 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
      });
    }
    return valid;
  }, [zodValidationSchema, clientValidationDispatcher, formStateRef, formConfig == null ? void 0 : formConfig.logSchemaErrors]);
  const parseValidationSchema = (keyChain, silent) => {
    if (zodValidationSchema) {
      return parseZodSchema(keyChain, silent);
    }
    return true;
  };
  return useFormBase(formState, formStateRef, dispatch, clientValidationErrors, clientValidationDispatcher, touchedState, touchedStateRef, touchedStateDispatcher, parseValidationSchema, liveInitialData, formConfig);
}
export {
  useForm
};
//# sourceMappingURL=useForm.js.map
