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