"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const useContentMemo = require("../../hooks/useContentMemo.cjs");
const keyChain = require("../utils/keyChain.cjs");
const touchedState = require("../utils/touchedState.cjs");
const validation = require("../utils/validation.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);
const useFormBase = (formStateLive, formStateRef, dispatch, clientValidationErrors, clientValidationDispatcher, touchedState$1, touchedStateRef, touchedStateDispatcher, parseValidationSchema, initialDataObject, formConfigObject, globalTouchOverride, parentKeyChain) => {
  var _a;
  const formConfig = useContentMemo.useContentMemo(formConfigObject);
  const initialData = useContentMemo.useContentMemo(initialDataObject);
  const [isGlobalTouched, setGlobalTouched] = React__namespace.useState(globalTouchOverride ?? false);
  const set = React__namespace.useCallback((keyChain2, newValue) => {
    if (keyChain2.length === 1) {
      dispatch({
        type: "set-one",
        propertyKey: keyChain2[0],
        value: newValue
      });
    } else {
      dispatch({ type: "set-path", keyChain: keyChain2, value: newValue });
    }
  }, [dispatch]);
  React__namespace.useEffect(() => {
    setGlobalTouched(globalTouchOverride ?? false);
  }, [globalTouchOverride]);
  const addValidationError = React__namespace.useCallback((...errors) => {
    clientValidationDispatcher({ type: "add-validation", errors });
  }, [clientValidationDispatcher]);
  const addValidationErrorFromKeyChain = React__namespace.useCallback((keyChain$1, messages, identifier) => {
    const messageArray = Array.isArray(messages) ? messages : [messages];
    const key = keyChain.keyStringFromKeyChain(keyChain$1, "dots");
    addValidationError(...messageArray.map((message) => ({ key, message, identifier })));
  }, [addValidationError]);
  const clearClientValidationErrors = React__namespace.useCallback((...identifiers) => {
    clientValidationDispatcher({
      type: "clear-validation",
      identifiers: identifiers.length ? identifiers : void 0
    });
  }, [clientValidationDispatcher]);
  const clearValidationErrorsByKeyChain = React__namespace.useCallback((keyChain$1, identifiers) => {
    const dotKey = keyChain.keyStringFromKeyChain(keyChain$1, "dots");
    const bracketKey = keyChain.keyStringFromKeyChain(keyChain$1, "brackets");
    clientValidationDispatcher({
      type: "clear-validation",
      key: dotKey,
      identifiers
    });
    clientValidationDispatcher({
      type: "clear-validation",
      key: bracketKey,
      identifiers
    });
  }, [clientValidationDispatcher]);
  const add = React__namespace.useCallback((keyChain$1, currentValue, newItem) => {
    if (keyChain.isArrayValue(currentValue, "formProp.add")) {
      const newValue = [...currentValue ?? []];
      newValue.push(newItem);
      dispatch({ type: "set-path", keyChain: keyChain$1, value: newValue });
    }
  }, [dispatch]);
  const remove = React__namespace.useCallback((keyChain$1, currentValue, index) => {
    if (keyChain.isArrayValue(currentValue, "formProp.remove")) {
      const newValue = [...currentValue ?? []];
      newValue.splice(index, 1);
      dispatch({ type: "set-path", keyChain: keyChain$1, value: newValue });
    }
  }, [dispatch]);
  const insert = React__namespace.useCallback((keyChain$1, currentValue, index, newItem) => {
    if (keyChain.isArrayValue(currentValue, "formProp.insert")) {
      const newValue = [...currentValue ?? []];
      newValue.splice(index, 0, newItem);
      dispatch({ type: "set-path", keyChain: keyChain$1, value: newValue });
    }
  }, [dispatch]);
  const pop = React__namespace.useCallback((keyChain$1, currentValue) => {
    if (keyChain.isArrayValue(currentValue, "formProp.pop")) {
      const newValue = [...currentValue ?? []];
      newValue.pop();
      dispatch({ type: "set-path", keyChain: keyChain$1, value: newValue });
    }
  }, [dispatch]);
  const setTouched = React__namespace.useCallback((keyChain2, touched) => {
    touchedStateDispatcher({ type: "set-touched", keyChain: keyChain2, touched });
  }, [touchedStateDispatcher]);
  const combinedValidationErrors = React__namespace.useMemo(() => [...clientValidationErrors, ...(formConfig == null ? void 0 : formConfig.validationErrors) ?? []], [clientValidationErrors, formConfig == null ? void 0 : formConfig.validationErrors]);
  const bind = React__namespace.useCallback((keyChain$1, bindConfig) => {
    const myTouchedState = touchedState.touchedStateForKeyChain(touchedState$1, keyChain$1);
    const myValidationErrors = validation.validationErrorsByKeyChain(combinedValidationErrors, keyChain$1);
    return {
      value: keyChain.valueByKeyChain(formStateLive, keyChain$1),
      setValue: (newValue) => set(keyChain$1, newValue),
      bindConfig,
      formConfig,
      myValidationErrors,
      dispatch,
      keyChain: keyChain$1,
      fullKeyChain: parentKeyChain ? [...parentKeyChain, ...keyChain$1] : keyChain$1,
      initialValue: keyChain.valueByKeyChain(initialData, keyChain$1),
      addValidationError: (messages, identifier) => addValidationErrorFromKeyChain(keyChain$1, messages, identifier),
      clearClientValidationErrors: (...identifiers) => clearValidationErrorsByKeyChain(keyChain$1, identifiers),
      setTouched: (isTouched) => setTouched(keyChain$1, isTouched),
      isTouched: !!myTouchedState.length || isGlobalTouched || !!globalTouchOverride,
      myTouchedState,
      clientValidationDispatcher,
      touchedStateDispatcher,
      allTouched: isGlobalTouched,
      validate: (andSetInputToTouched = true, silent = false, validateAll = false) => {
        if (andSetInputToTouched) {
          setTouched(keyChain$1, true);
        }
        return parseValidationSchema(validateAll ? void 0 : keyChain$1, silent);
      },
      isValid: !myValidationErrors.length,
      parseValidationSchema
    };
  }, [
    touchedState$1,
    combinedValidationErrors,
    formStateLive,
    formConfig,
    dispatch,
    parentKeyChain,
    initialData,
    isGlobalTouched,
    globalTouchOverride,
    clientValidationDispatcher,
    touchedStateDispatcher,
    parseValidationSchema,
    set,
    addValidationErrorFromKeyChain,
    clearValidationErrorsByKeyChain,
    setTouched
  ]);
  const validate = React__namespace.useCallback((andSetGlobalTouched = true, silent = false) => {
    if (andSetGlobalTouched) {
      setGlobalTouched(true);
    }
    return parseValidationSchema([], silent);
  }, [parseValidationSchema, setGlobalTouched]);
  const formProp = React__namespace.useCallback((...keyChain$1) => {
    const value = keyChain.valueByKeyChain(formStateRef.current, keyChain$1);
    const myTouchedState = touchedState.touchedStateForKeyChain(touchedStateRef.current, keyChain$1);
    const myValidationErrors = validation.validationErrorsByKeyChain(combinedValidationErrors, keyChain$1);
    const methods = {
      bind: (bindConfig) => bind(keyChain$1, bindConfig),
      set: (newValue) => {
        set(keyChain$1, newValue);
        return formProp(...keyChain$1);
      },
      clear: () => {
        set(keyChain$1, void 0);
        return formProp(...keyChain$1);
      },
      get: () => value,
      add: (newItem) => {
        add(keyChain$1, value, newItem);
        return formProp(...keyChain$1);
      },
      pop: () => {
        pop(keyChain$1, value);
        return formProp(...keyChain$1);
      },
      insert: (newItem, index) => {
        insert(keyChain$1, value, index, newItem);
        return formProp(...keyChain$1);
      },
      remove: (index) => {
        remove(keyChain$1, value, index);
        return formProp(...keyChain$1);
      },
      addValidationError: (messages, identifier) => {
        addValidationErrorFromKeyChain(keyChain$1, messages, identifier);
        return formProp(...keyChain$1);
      },
      clearClientValidationErrors: (...identifiers) => {
        clearValidationErrorsByKeyChain(keyChain$1, identifiers);
        return formProp(...keyChain$1);
      },
      validate: (andSetInputToTouched = true, silent = false) => {
        if (andSetInputToTouched) {
          setTouched(keyChain$1, true);
        }
        return parseValidationSchema(keyChain$1, silent);
      },
      isTouched: !!myTouchedState.length || isGlobalTouched || !!globalTouchOverride,
      setTouched: (isTouched) => {
        setTouched(keyChain$1, isTouched);
        return formProp(...keyChain$1);
      },
      isValid: !myValidationErrors.length
    };
    return methods;
  }, [
    formStateRef,
    touchedStateRef,
    isGlobalTouched,
    globalTouchOverride,
    combinedValidationErrors,
    bind,
    set,
    add,
    pop,
    insert,
    remove,
    addValidationErrorFromKeyChain,
    clearValidationErrorsByKeyChain,
    parseValidationSchema,
    setTouched
  ]);
  const getFormData = React__namespace.useCallback(() => {
    return formStateRef.current;
  }, [formStateRef]);
  const resetFormData = React__namespace.useCallback(() => {
    dispatch({ type: "set-all", data: initialData });
  }, [initialData, dispatch]);
  const setFormData = React__namespace.useCallback((newFormData) => {
    return dispatch({ type: "set-all", data: newFormData });
  }, [dispatch]);
  const resetTouchedState = React__namespace.useCallback(() => {
    setGlobalTouched(false);
    touchedStateDispatcher({ type: "reset-touched" });
  }, [touchedStateDispatcher]);
  return {
    formState: formStateLive,
    formProp,
    resetFormData,
    getFormData,
    setFormData,
    clearClientValidationErrors,
    addValidationError,
    validate,
    touchAll: () => setGlobalTouched(true),
    allTouched: isGlobalTouched,
    isValid: !clientValidationErrors.length && !((_a = formConfig == null ? void 0 : formConfig.validationErrors) == null ? void 0 : _a.length) && parseValidationSchema(void 0, true),
    resetTouchedState
  };
};
exports.useFormBase = useFormBase;
//# sourceMappingURL=useFormBase.cjs.map
