import React__default from "react";
import { useDidUpdateSSRLayoutEffect } from "../../hooks/useSSRLayoutEffect.js";
import "../../utils/globals.js";
import { assertNever } from "../../utils/typescript.js";
import { childKeyChainStringFromParent, valueByKeyChain, keyStringFromKeyChain, isMyKeyChainItem } from "../utils/keyChain.js";
import { useFormBase } from "./useFormBase.js";
function useChildForm(parentBinder) {
  const formStateRef = React__default.useRef(parentBinder == null ? void 0 : parentBinder.value);
  const touchedStateRef = React__default.useRef((parentBinder == null ? void 0 : parentBinder.myTouchedState) ?? []);
  useDidUpdateSSRLayoutEffect(() => {
    formStateRef.current = parentBinder == null ? void 0 : parentBinder.value;
  }, [parentBinder == null ? void 0 : parentBinder.value]);
  const validationErrors = React__default.useMemo(() => {
    var _a;
    return ((_a = parentBinder == null ? void 0 : parentBinder.myValidationErrors) == null ? void 0 : _a.map((ve) => ({
      ...ve,
      key: childKeyChainStringFromParent(ve.key, parentBinder == null ? void 0 : parentBinder.keyChain)
    }))) ?? [];
  }, [parentBinder == null ? void 0 : parentBinder.keyChain, parentBinder == null ? void 0 : parentBinder.myValidationErrors]);
  const touchedState = React__default.useMemo(() => {
    var _a;
    return ((_a = parentBinder == null ? void 0 : parentBinder.myTouchedState) == null ? void 0 : _a.map((k) => childKeyChainStringFromParent(k, parentBinder == null ? void 0 : parentBinder.keyChain))) ?? [];
  }, [parentBinder == null ? void 0 : parentBinder.keyChain, parentBinder == null ? void 0 : parentBinder.myTouchedState]);
  const dispatch = React__default.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:
        assertNever(action);
        throw new Error(`Invalid action: ${JSON.stringify(assertNever ?? {})}`);
    }
    formStateRef.current = valueByKeyChain(fullState, parentBinder == null ? void 0 : parentBinder.keyChain);
    return formStateRef.current;
  }, [parentBinder]);
  const addValidationError = React__default.useCallback((action) => {
    switch (action.type) {
      case "add-validation":
        parentBinder == null ? void 0 : parentBinder.clientValidationDispatcher({
          ...action,
          errors: action.errors.map((e) => ({
            ...e,
            key: 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 && keyStringFromKeyChain([...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], action.key], "dots"),
          identifiers: action.identifiers
        });
        break;
      default:
        assertNever(action);
        throw new Error(`Invalid action: ${JSON.stringify(assertNever)}`);
    }
  }, [parentBinder]);
  const touchedStateDispatcher = React__default.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:
        assertNever(action);
        throw new Error(`Invalid action: ${JSON.stringify(assertNever)}`);
    }
    touchedStateRef.current = fullTouchState.filter((k) => isMyKeyChainItem(k, keyStringFromKeyChain(parentBinder == null ? void 0 : parentBinder.keyChain, "dots"))).map((k) => childKeyChainStringFromParent(k, (parentBinder == null ? void 0 : parentBinder.keyChain) ?? [])) ?? [];
    return touchedStateRef.current;
  }, [parentBinder]);
  const parseValidationSchema = (keyChain, silent) => {
    if (!(parentBinder == null ? void 0 : parentBinder.parseValidationSchema)) {
      return true;
    }
    return (parentBinder == null ? void 0 : parentBinder.parseValidationSchema([...(parentBinder == null ? void 0 : parentBinder.keyChain) ?? [], ...keyChain ?? []], silent)) ?? true;
  };
  return 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);
}
export {
  useChildForm
};
//# sourceMappingURL=useChildForm.js.map
