"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const useSSRLayoutEffect = require("../../hooks/useSSRLayoutEffect.cjs");
require("../../utils/globals.cjs");
const typescript = require("../../utils/typescript.cjs");
const keyChain = require("../utils/keyChain.cjs");
const useFormBase = require("./useFormBase.cjs");
function useChildForm(parentBinder) {
  const formStateRef = React.useRef(parentBinder == null ? void 0 : parentBinder.value);
  const touchedStateRef = React.useRef((parentBinder == null ? void 0 : parentBinder.myTouchedState) ?? []);
  useSSRLayoutEffect.useDidUpdateSSRLayoutEffect(() => {
    formStateRef.current = parentBinder == null ? void 0 : parentBinder.value;
  }, [parentBinder == null ? void 0 : parentBinder.value]);
  const validationErrors = React.useMemo(() => {
    var _a;
    return ((_a = parentBinder == null ? void 0 : parentBinder.myValidationErrors) == null ? void 0 : _a.map((ve) => ({
      ...ve,
      key: keyChain.childKeyChainStringFromParent(ve.key, parentBinder == null ? void 0 : parentBinder.keyChain)
    }))) ?? [];
  }, [parentBinder == null ? void 0 : parentBinder.keyChain, parentBinder == null ? void 0 : parentBinder.myValidationErrors]);
  const touchedState = React.useMemo(() => {
    var _a;
    return ((_a = parentBinder == null ? void 0 : parentBinder.myTouchedState) == null ? void 0 : _a.map((k) => keyChain.childKeyChainStringFromParent(k, parentBinder == null ? void 0 : parentBinder.keyChain))) ?? [];
  }, [parentBinder == null ? void 0 : parentBinder.keyChain, parentBinder == null ? void 0 : parentBinder.myTouchedState]);
  const dispatch = React.useCallback((action) => {
    let fullState = {};
    switch (action.type) {
      case "set-all":
        fullState = parentBinder == null ? void 0 : parentBinder.dispatch({
          type: "set-path",
          keyChain: parentBinder == null ? void 0 : parentBinder.keyChain,
          value: action.data
        });
        break;
      case "set-path":
        fullState = parentBinder == null ? void 0 : parentBinder.dispatch({
          ...action,
          keyChain: [...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], ...action.keyChain]
        });
        break;
      case "set-one":
        fullState = parentBinder == null ? void 0 : parentBinder.dispatch({
          type: "set-path",
          keyChain: [...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], action.propertyKey],
          value: action.value
        });
        break;
      default:
        typescript.assertNever(action);
        throw new Error(`Invalid action: ${JSON.stringify(typescript.assertNever ?? {})}`);
    }
    formStateRef.current = keyChain.valueByKeyChain(fullState, parentBinder == null ? void 0 : parentBinder.keyChain);
    return formStateRef.current;
  }, [parentBinder]);
  const addValidationError = React.useCallback((action) => {
    switch (action.type) {
      case "add-validation":
        parentBinder == null ? void 0 : parentBinder.clientValidationDispatcher({
          ...action,
          errors: action.errors.map((e) => ({
            ...e,
            key: keyChain.keyStringFromKeyChain([...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], e.key], "dots")
          }))
        });
        break;
      case "clear-validation":
        parentBinder == null ? void 0 : parentBinder.clientValidationDispatcher({
          type: "clear-validation",
          key: action.key && keyChain.keyStringFromKeyChain([...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], action.key], "dots"),
          identifiers: action.identifiers
        });
        break;
      default:
        typescript.assertNever(action);
        throw new Error(`Invalid action: ${JSON.stringify(typescript.assertNever)}`);
    }
  }, [parentBinder]);
  const touchedStateDispatcher = React.useCallback((action) => {
    let fullTouchState = [];
    switch (action.type) {
      case "set-touched":
        fullTouchState = (parentBinder == null ? void 0 : parentBinder.touchedStateDispatcher({
          ...action,
          keyChain: [...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], ...action.keyChain]
        })) ?? [];
        break;
      case "reset-touched":
        fullTouchState = (parentBinder == null ? void 0 : parentBinder.touchedStateDispatcher({
          type: "set-touched",
          keyChain: parentBinder.keyChain,
          touched: false
        })) ?? [];
        break;
      default:
        typescript.assertNever(action);
        throw new Error(`Invalid action: ${JSON.stringify(typescript.assertNever)}`);
    }
    touchedStateRef.current = fullTouchState.filter((k) => keyChain.isMyKeyChainItem(k, keyChain.keyStringFromKeyChain(parentBinder == null ? void 0 : parentBinder.keyChain, "dots"))).map((k) => keyChain.childKeyChainStringFromParent(k, (parentBinder == null ? void 0 : parentBinder.keyChain) ?? [])) ?? [];
    return touchedStateRef.current;
  }, [parentBinder]);
  const parseValidationSchema = (keyChain2, silent) => {
    if (!(parentBinder == null ? void 0 : parentBinder.parseValidationSchema)) {
      return true;
    }
    return (parentBinder == null ? void 0 : parentBinder.parseValidationSchema([...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], ...keyChain2 ?? []], silent)) ?? true;
  };
  return useFormBase.useFormBase(parentBinder == null ? void 0 : parentBinder.value, formStateRef, dispatch, validationErrors, addValidationError, touchedState, touchedStateRef, touchedStateDispatcher, parseValidationSchema, parentBinder == null ? void 0 : parentBinder.initialValue, parentBinder == null ? void 0 : parentBinder.formConfig, parentBinder == null ? void 0 : parentBinder.allTouched, parentBinder == null ? void 0 : parentBinder.fullKeyChain);
}
exports.useChildForm = useChildForm;
//# sourceMappingURL=useChildForm.cjs.map
