import { jsx as t } from "@emotion/react/jsx-runtime";
import z, { forwardRef as O, useMemo as x, useImperativeHandle as L } from "react";
import l from "clsx";
import { useModuleSkeleton as k } from "@m4l/core";
import { useComponentSize as w } from "../../hooks/useComponentSize/index.js";
import { TEST_PROP_ID as V } from "../../test/constants_no_mock.js";
import { LinearProgress as Y } from "../extended/mui/LinearProgress/index.js";
import { DynamicFormContentRootStyled as g, DynamicFormFormProviderStyled as j } from "./slots/DynamicFormSlots.js";
import { DYNAMIC_FORM_CLASSES as a, EMPTY_DATA as H } from "./constants.js";
import { useDynamicForm as U } from "./hooks/useDynamicForm/useDynamicForm.js";
import { useDynamicFormData as W } from "./hooks/useDynamicFormData/useDynamicFormData.js";
import { DynamicFormContent as d } from "./subcomponents/DynamicFormContent/DynamicFormContent.js";
const q = O((D, S) => {
  const {
    formId: y,
    formInstanceId: F,
    inputEndpoint: I,
    outputEndpoint: i,
    initialData: s,
    formatInputData: R,
    formatOutputData: E,
    onSubmit: c,
    onSubmitError: N,
    size: P,
    className: u,
    dataTestId: e,
    fieldWrapperClassName: f,
    coupled: n = !1,
    actionFormIntroProps: v,
    renderActions: C
  } = D, { currentSize: r } = w(P), T = k(), {
    formData: _,
    statusLoad: b,
    handleSubmit: A
  } = W({
    formId: y,
    formInstanceId: F,
    inputEndpoint: I,
    outputEndpoint: i,
    initialData: s,
    formatInputData: R,
    formatOutputData: E,
    onSubmit: c
  }), o = _ || s, { initialValues: M, validationSchema: h } = U(o || H), m = x(() => ({
    size: r
  }), [r]), p = z.useRef(null);
  return L(S, () => p.current, []), T ? /* @__PURE__ */ t(
    g,
    {
      ownerState: m,
      className: l(u, a.contentRoot),
      ...process.env.NODE_ENV !== "production" ? { [V]: e } : {},
      children: /* @__PURE__ */ t(Y, {})
    }
  ) : o ? n ? /* @__PURE__ */ t(
    d,
    {
      data: o,
      ownerState: m,
      currentSize: r,
      fieldWrapperClassName: f,
      dataTestId: e,
      coupled: n
    }
  ) : /* @__PURE__ */ t(
    j,
    {
      ref: p,
      values: M,
      validationSchema: h,
      onSubmit: i ? A : c ?? (() => {
      }),
      onSubmitError: N,
      statusLoad: b,
      mode: "onSubmit",
      className: l(u, a.contentRoot, a.formProvider),
      children: o && /* @__PURE__ */ t(
        d,
        {
          data: o,
          ownerState: m,
          currentSize: r,
          fieldWrapperClassName: f,
          dataTestId: e,
          coupled: n,
          actionFormIntroProps: v,
          renderActions: C
        }
      )
    }
  ) : null;
});
q.displayName = "DynamicForm";
export {
  q as DynamicForm
};
