import U from "./index-extends.js";
import * as r from "react";
import { useRifm as G } from "./index-rifm.esm.js";
import { useUtils as J } from "./index-useUtils.js";
import { getMaskFromCurrentFormat as K, checkMaskIsValidForCurrentFormat as N, maskedDateFormatter as O, getDisplayDate as T } from "./index-text-field-helper.js";
const $ = ({
  acceptRegex: u = /[\d]/gi,
  disabled: p,
  disableMaskedInput: m,
  ignoreInvalidInputs: v,
  inputFormat: n,
  inputProps: y,
  label: H,
  mask: d,
  onChange: S,
  rawValue: t,
  readOnly: P,
  rifmFormatter: A,
  TextFieldProps: V,
  validationError: E
}) => {
  const e = J(), R = e.getFormatHelperText(n), {
    shouldUseMaskedInput: c,
    maskToUse: f
  } = r.useMemo(() => {
    if (m)
      return {
        shouldUseMaskedInput: !1,
        maskToUse: ""
      };
    const s = K(d, n, u, e);
    return {
      shouldUseMaskedInput: N(s, n, u, e),
      maskToUse: s
    };
  }, [u, m, n, d, e]), q = r.useMemo(() => c && f ? O(f, u) : (s) => s, [u, f, c]), L = t === null ? null : e.date(t), [i, h] = r.useState(L), [g, I] = r.useState(T(e, t, n)), k = r.useRef(), M = r.useRef(e.locale), C = r.useRef(n);
  r.useEffect(() => {
    const s = t !== k.current, o = e.locale !== M.current, l = n !== C.current;
    if (k.current = t, M.current = e.locale, C.current = n, !s && !o && !l)
      return;
    const a = t === null ? null : e.date(t), j = t === null || e.isValid(a), z = i === null ? a === null : a !== null && Math.abs(e.getDiff(i, a, "seconds")) === 0;
    if (!o && !l && (!j || z))
      return;
    const B = T(e, t, n);
    h(a), I(B);
  }, [e, t, n, i]);
  const D = (s) => {
    const o = s === "" || s === d ? "" : s;
    I(o);
    const l = o === null ? null : e.parse(o, n);
    v && !e.isValid(l) || (h(l), S(l, o || void 0));
  }, _ = G({
    value: g,
    onChange: D,
    format: A || q
  });
  return U({
    label: H,
    disabled: p,
    error: E,
    inputProps: U({}, c ? _ : {
      value: g,
      onChange: (s) => {
        D(s.currentTarget.value);
      }
    }, {
      disabled: p,
      placeholder: R,
      readOnly: P,
      type: c ? "tel" : "text"
    }, y)
  }, V);
};
export {
  $ as useMaskedInput
};
