"use client";
import "../../chunk-CUOVI2HT.js";
import {
  TextField
} from "../../chunk-6RDK3FM2.js";
import "../../chunk-I6CFRGID.js";
import "../../chunk-3PRSHEVX.js";
import "../../chunk-MZJT3USG.js";
import "../../chunk-NPOXFILT.js";
import "../../chunk-XH3OJQMW.js";
import "../../chunk-YC2UYVFY.js";
import "../../chunk-ZN7OWLZY.js";
import "../../chunk-R43NQOOV.js";
import "../../chunk-7IRHPNBO.js";
import "../../chunk-TKIP5Q5H.js";
import "../../chunk-ZXSCKR6G.js";
import "../../chunk-KTJCAQT4.js";
import "../../chunk-A66R42LG.js";
import "../../chunk-XXRLHJSM.js";
import "../../chunk-N67T2EUZ.js";
import "../../chunk-7LSDEZNB.js";
import "../../chunk-RXSPBHPL.js";
import "../../chunk-FB3CIPAZ.js";
import "../../chunk-F3FNFTZY.js";
import "../../chunk-7FQ7SXK4.js";
import "../../chunk-FUTRSCIO.js";
import "../../chunk-JOHPO5VS.js";
import "../../chunk-UYVF3QSW.js";
import "../../chunk-RU5TC5EX.js";
import "../../chunk-YLEFK7S2.js";
import "../../chunk-HJTYN3P2.js";
import "../../chunk-RQJCLQZI.js";
import "../../chunk-OFEF3DZT.js";
import "../../chunk-DYOFXXZD.js";
import "../../chunk-FIAL4HTE.js";
import "../../chunk-QGGILW3D.js";
import "../../chunk-74GPBJTH.js";
import "../../chunk-JVBAJ33L.js";
import "../../chunk-YTNOJLN7.js";
import "../../chunk-DTWRSP5P.js";
import "../../chunk-LRZS4WSR.js";
import "../../chunk-54MSCPN2.js";
import "../../chunk-IDU2FHOY.js";
import "../../chunk-YYBF2WRK.js";
import "../../chunk-CZWEMAKZ.js";
import "../../chunk-RIUSD7SC.js";
import "../../chunk-VHAA22YE.js";

// src/components/MaskedField/MaskedField.tsx
import { useCallback, useEffect } from "react";
import { useIMask } from "react-imask";
import { jsx } from "react/jsx-runtime";
var useAcceptHandler = (onChange, useMaskedValue, name) => {
  return useCallback(
    (maskedValue, maskInstance) => {
      if (!onChange) {
        return;
      }
      const formattedValue = maskedValue;
      const rawValue = maskInstance.unmaskedValue;
      const valueToEmit = useMaskedValue ? formattedValue : rawValue;
      onChange({
        target: { name, value: valueToEmit }
      });
    },
    [onChange, useMaskedValue, name]
  );
};
var useCombinedRef = (imaskRef, forwardedRef) => {
  return useCallback(
    (element) => {
      imaskRef.current = element;
      if (typeof forwardedRef === "function") {
        forwardedRef(element);
      } else if (forwardedRef) {
        forwardedRef.current = element;
      }
    },
    [imaskRef, forwardedRef]
  );
};
var useBlurHandler = (onBlur, maskRef, useMaskedValue, name) => {
  return useCallback(
    (event) => {
      if (!onBlur || !maskRef.current) {
        return;
      }
      const formattedValue = maskRef.current.value;
      const rawValue = maskRef.current.unmaskedValue;
      const valueToEmit = useMaskedValue ? formattedValue : rawValue;
      onBlur({
        ...event,
        target: { ...event.target, name, value: valueToEmit }
      });
    },
    [onBlur, maskRef, useMaskedValue, name]
  );
};
var ControlledMaskedField = ({
  mask,
  useMaskedValue = false,
  value,
  onChange,
  onBlur,
  name,
  forwardedRef,
  ...textFieldProps
}) => {
  const onAccept = useAcceptHandler(onChange, useMaskedValue, name);
  const { ref: imaskRef, maskRef, setValue } = useIMask(mask, { onAccept });
  const combinedRef = useCombinedRef(imaskRef, forwardedRef);
  const handleBlur = useBlurHandler(onBlur, maskRef, useMaskedValue, name);
  useEffect(() => {
    if (!maskRef.current) {
      return;
    }
    const stringValue = value.toString();
    const currentMasked = maskRef.current.value;
    const currentUnmasked = maskRef.current.unmaskedValue;
    if (currentMasked !== stringValue && currentUnmasked !== stringValue) {
      setValue(stringValue);
    }
  }, [value, maskRef, setValue, imaskRef]);
  return /* @__PURE__ */ jsx(
    TextField,
    {
      ...textFieldProps,
      ref: combinedRef,
      name,
      onBlur: handleBlur
    }
  );
};
var UncontrolledMaskedField = ({
  mask,
  useMaskedValue = false,
  defaultValue,
  onChange,
  onBlur,
  name,
  forwardedRef,
  ...textFieldProps
}) => {
  const onAccept = useAcceptHandler(onChange, useMaskedValue, name);
  const { ref: imaskRef, maskRef } = useIMask(mask, { onAccept });
  const combinedRef = useCombinedRef(imaskRef, forwardedRef);
  const handleBlur = useBlurHandler(onBlur, maskRef, useMaskedValue, name);
  return /* @__PURE__ */ jsx(
    TextField,
    {
      ...textFieldProps,
      ref: combinedRef,
      name,
      onBlur: handleBlur,
      defaultValue: defaultValue?.toString()
    }
  );
};
var MaskedField = ({ ref: forwardedRef, ...props }) => {
  const { value, defaultValue, ...rest } = props;
  if ("value" in props) {
    return /* @__PURE__ */ jsx(
      ControlledMaskedField,
      {
        ...rest,
        value: value ?? "",
        forwardedRef
      }
    );
  } else {
    return /* @__PURE__ */ jsx(
      UncontrolledMaskedField,
      {
        ...rest,
        defaultValue,
        forwardedRef
      }
    );
  }
};
MaskedField.displayName = "MaskedField";
export {
  MaskedField
};
//# sourceMappingURL=MaskedField.js.map
