import { jsx as e, jsxs as B, Fragment as K } from "@emotion/react/jsx-runtime";
import { forwardRef as W, useId as X } from "react";
import { useFormContext as Y, Controller as $ } from "react-hook-form";
import { InputAdornment as g } from "@mui/material";
import { getPropDataTestId as h } from "../../../test/getNameDataTestId.js";
import { RHF_TEXT_FIELD_KEY_COMPONENT as i } from "./constant.js";
import { useComponentSize as q } from "../../../hooks/useComponentSize/index.js";
import { getComponentSlotRoot as G } from "../../../utils/getComponentSlotRoot.js";
import J from "clsx";
import { RHFTextFieldRootStyled as Q, LabelStyled as U, TextFieldStyled as Z, HelperErrorStyled as ee } from "./slots/RHFTextFieldSlots.js";
import { RHFTextFieldSlots as F } from "./slots/RHFTextFieldEnum.js";
import { getLimitCharacters as te } from "../../../utils/getLimitCharacters/getLimitCharacters.js";
const oe = W((S, x) => {
  const {
    name: n,
    autoComplete: I = "off",
    type: m,
    label: d,
    size: y = "medium",
    startIcon: p,
    endIcon: u,
    helperMessage: C = "",
    disabled: c,
    mandatory: T,
    variant: b = "outlined",
    color: R = "default",
    isInteger: z = !0,
    mandatoryMessage: L,
    helperText: re = !0,
    className: w,
    htmlFor: H,
    InputProps: E,
    ...a
  } = S, N = X(), t = H ?? N, P = `field-label-${t}`, {
    control: v
  } = Y(), { currentSize: l } = q(y), o = l === "small" || l === "medium" ? l : "medium", s = {
    name: n,
    color: R,
    isInteger: z,
    type: m ?? "text",
    size: o,
    disabled: c
  }, _ = G(i);
  return /* @__PURE__ */ e(
    Q,
    {
      ownerState: { ...s },
      className: J(_, w),
      ...h(i, F.root),
      children: /* @__PURE__ */ e(
        $,
        {
          name: n,
          control: v,
          render: ({ field: { onChange: j, value: A, ref: M }, fieldState: { error: r }, formState: { isSubmitting: D } }) => {
            const O = (f) => {
              const V = a.maxLength ? te({ limitCharacters: a.maxLength, value: f.target.value }) : f.target.value, k = String(V || "");
              j(k);
            };
            return /* @__PURE__ */ B(K, { children: [
              d && /* @__PURE__ */ e(
                U,
                {
                  ownerState: { ...s },
                  size: o,
                  label: d,
                  htmlFor: t,
                  mandatory: T,
                  mandatoryMessage: L,
                  helperMessage: C
                }
              ),
              /* @__PURE__ */ e(
                Z,
                {
                  ownerState: { ...s, error: !!r },
                  disabled: c || D,
                  size: o,
                  inputRef: M,
                  ref: x,
                  InputLabelProps: { shrink: !0 },
                  autoComplete: I,
                  onChange: O,
                  variant: b,
                  value: String(A || ""),
                  fullWidth: !0,
                  error: !!r,
                  type: m,
                  InputProps: {
                    ...E,
                    name: n,
                    id: t,
                    "aria-labelledby": P,
                    ...h(i, F.textField),
                    ...p && {
                      startAdornment: /* @__PURE__ */ e(g, { position: "start", children: p })
                    },
                    ...u && {
                      endAdornment: /* @__PURE__ */ e(g, { position: "end", children: u })
                    }
                  },
                  ...a
                }
              ),
              r?.message ? /* @__PURE__ */ e(
                ee,
                {
                  ownerState: {},
                  message: r.message,
                  size: o,
                  htmlFor: t
                }
              ) : null
            ] });
          }
        }
      )
    }
  );
});
oe.displayName = "RHFTextField";
export {
  oe as RHFTextField
};
