import { jsx as o } from "@emotion/react/jsx-runtime";
import J, { forwardRef as K, useMemo as R, useState as Q, useEffect as X, useImperativeHandle as Z, useCallback as N } from "react";
import O from "clsx";
import { useModuleSkeleton as $ } from "@m4l/core";
import { useComponentSize as tt } from "../../hooks/useComponentSize/index.js";
import { TEST_PROP_ID as et } from "../../test/constants_no_mock.js";
import { LinearProgress as ot } from "../extended/mui/LinearProgress/index.js";
import { DynamicFormContentRootStyled as rt, DynamicFormFormProviderStyled as nt } from "./slots/DynamicFormSlots.js";
import { DYNAMIC_FORM_CLASSES as f, EMPTY_DATA as at } from "./constants.js";
import { FIELD_COMPONENT_STRATEGY_REGISTRY as mt } from "./helpers/resolveFieldComponent/strategies/registry.js";
import { useDynamicForm as it } from "./hooks/useDynamicForm/useDynamicForm.js";
import { useDynamicFormData as st } from "./hooks/useDynamicFormData/useDynamicFormData.js";
import { DynamicFormContent as _ } from "./subcomponents/DynamicFormContent/DynamicFormContent.js";
import { mapImageSelectorValuesForSubmit as ut } from "./helpers/mapImageSelectorValuesForSubmit/mapImageSelectorValuesForSubmit.js";
const ct = K((h, C) => {
  const {
    formId: M,
    formInstanceId: P,
    inputEndpoint: k,
    outputEndpoint: r,
    submitMode: n,
    initialData: p,
    formatInputData: v,
    formatOutputData: w,
    onSubmit: a,
    onSubmitError: S,
    size: A,
    className: b,
    dataTestId: s,
    dataMeta: u,
    fieldWrapperClassName: D,
    coupled: c = !1,
    actionFormIntroProps: z,
    renderActions: H
  } = h, { currentSize: m } = tt(A), L = $(), {
    formData: Y,
    statusLoad: g,
    handleSubmit: F
  } = st({
    formId: M,
    formInstanceId: P,
    inputEndpoint: k,
    outputEndpoint: r,
    submitMode: n,
    initialData: p,
    formatInputData: v,
    formatOutputData: w,
    onSubmit: a
  }), e = Y || p, y = R(() => new Set(
    (e?.fields ?? []).filter((t) => {
      const d = t.fieldType?.componentId;
      return !d || !mt[d];
    }).map((t) => t.name).filter((t) => typeof t == "string" && t.trim() !== "")
  ), [e]), { initialValues: x, validationSchema: V } = it(e || at), [l, i] = Q(null);
  X(() => {
    if (!l)
      return;
    const t = setTimeout(() => i(null), 5e3);
    return () => clearTimeout(t);
  }, [l]);
  const j = R(() => ({
    size: m
  }), [m]), E = J.useRef(null);
  Z(C, () => E.current, []);
  const G = N(
    async (t) => {
      if (!!!(r || n === "perField" || a))
        return;
      const U = r || n === "perField" ? F : a ?? (() => {
      }), I = {};
      for (const [T, q] of Object.entries(t))
        y.has(T) || (I[T] = q);
      const W = ut(I, e?.fields ?? []);
      try {
        await U(W), i("success");
      } catch {
        i("error");
      }
    },
    [e?.fields, F, a, r, n, y]
  ), B = N(
    (t) => {
      i("error"), S?.(t);
    },
    [S]
  );
  return L ? /* @__PURE__ */ o(
    rt,
    {
      ownerState: j,
      className: O(b, f.contentRoot),
      ...process.env.NODE_ENV !== "production" ? { [et]: s, "data-meta": u } : {},
      children: /* @__PURE__ */ o(ot, {})
    }
  ) : e ? c ? /* @__PURE__ */ o(
    _,
    {
      data: e,
      currentSize: m,
      fieldWrapperClassName: D,
      dataTestId: s,
      dataMeta: u,
      coupled: c
    }
  ) : /* @__PURE__ */ o(
    nt,
    {
      ref: E,
      values: x,
      validationSchema: V,
      onSubmit: G,
      onSubmitError: B,
      statusLoad: g,
      mode: "onSubmit",
      className: O(b, f.contentRoot, f.formProvider),
      children: /* @__PURE__ */ o(
        _,
        {
          data: e,
          currentSize: m,
          fieldWrapperClassName: D,
          dataTestId: s,
          dataMeta: u,
          coupled: c,
          actionFormIntroProps: z,
          renderActions: H,
          submitFeedbackStatus: l
        }
      )
    }
  ) : null;
});
ct.displayName = "DynamicForm";
export {
  ct as DynamicForm
};
