import { jsx as g } from "react/jsx-runtime";
/* empty css                */
import { validInput as j } from "./index-valid-input.js";
import { FormElement as k } from "./index-FormElement.js";
import l, { useState as K, useEffect as N } from "react";
import { ComponentContainer as P } from "./index-ComponentContainer.js";
import { useFormConfigContext as R, FormConfigProvider as B } from "./index-FormConfigContext.js";
const J = (S) => {
  const {
    className: h,
    children: _,
    config: r,
    onChange: c
  } = S, i = {}, f = R(), C = (f == null ? void 0 : f.parentOnChange) || c, [y, d] = K({
    hasChanges: !1,
    isValid: !0,
    inputsChanges: {},
    inputsStatus: {},
    currentConfig: null
  });
  N(() => {
    C && C(y);
  }, [y]);
  const x = (n) => {
    let t = null;
    if (typeof n != "string" && typeof n != "number" && typeof n != "boolean") {
      const e = n == null ? void 0 : n.props;
      t = e.formconfig || e.formConfig;
    }
    return Object.assign({}, t);
  }, V = (n) => {
    let t = null;
    return typeof n != "string" && typeof n != "number" && typeof n != "boolean" && (t = (n == null ? void 0 : n.props).formconfig ? "formconfig" : "formConfig"), t;
  }, b = () => {
    let n = !1;
    if (r && r._internalInputs)
      for (const t in r._internalInputs.current)
        j(r._internalInputs.current[t].config) || (n = !0);
    return {
      hasError: n
    };
  }, v = (n) => {
    r && r._removeInternalInput && r._removeInternalInput(n);
    const {
      hasError: t
    } = b();
    d((e) => ({
      isValid: !t,
      hasChanges: e.hasChanges,
      inputsChanges: e.inputsChanges,
      inputsStatus: e.inputsStatus,
      currentConfig: e.currentConfig
    }));
  }, A = (n) => {
    const t = String(n.childFormConfig.arrayPosition ?? ""), e = {};
    e[n.name] = n.isValid, e[n.name + t] = n.isValid;
    const o = {};
    o[n.name] = n.hasChanges, o[n.name + t] = n.hasChanges, d((a) => {
      const s = {
        ...a.inputsStatus,
        ...e
      }, m = Object.entries(s).filter(([O, p]) => p === !1), F = {
        ...a.inputsChanges,
        ...o
      };
      return {
        hasChanges: Object.entries(F).filter(([O, p]) => p === !0).length !== 0,
        inputsChanges: F,
        isValid: m.length === 0,
        inputsStatus: s,
        currentConfig: n.childFormConfig
      };
    });
  }, E = (n) => {
    A(n);
  }, I = (n) => l.Children.map(n, (t) => {
    if (l.isValidElement(t)) {
      let e;
      const o = x(t), a = V(t), s = o.stateFieldName, u = t == null ? void 0 : t.props;
      return !o || !o.stateFieldName || !a ? u.children ? l.cloneElement(t, {}, I(u.children)) : t : (o.isArray && s && (i[s] !== void 0 ? i[s]++ : i[s] = 0, e = i[s]), g(k, {
        onChange: A,
        onBlur: E,
        childConfig: o,
        formConfigKey: a,
        formConfig: r,
        idx: e,
        childAdded: (m) => {
          r && r._addInternalInput && r._addInternalInput(m);
        },
        childRemoved: v,
        children: t
      }));
    }
    return t;
  });
  return g(B, {
    formConfig: r || null,
    parentOnChange: c,
    children: g(P, {
      className: `Form-container ${h}`,
      children: I(_)
    })
  });
};
export {
  J as Form,
  R as useFormConfigContext
};
