import { useRef as E, useState as y, useCallback as k, useEffect as m } from "react";
function $(t, s) {
  return new $.InputMask(t, s);
}
function b(t, s) {
  let {
    onAccept: c,
    onComplete: f,
    ref: v = E(null),
    defaultValue: l,
    defaultUnmaskedValue: T,
    defaultTypedValue: U
  } = s === void 0 ? {} : s;
  const u = E(null), [A, p] = y({}), [d, M] = y(""), [o, I] = y(""), [i, R] = y(), V = k(() => {
    var e;
    (e = u.current) == null || e.destroy(), u.current = null;
  }, []), _ = k(() => {
    const e = u.current;
    e && (p({
      value: e.value,
      unmaskedValue: e.unmaskedValue,
      typedValue: e.typedValue
    }), R(e.typedValue), I(e.unmaskedValue), M(e.value));
  }, []), r = k((e) => {
    const n = u.current;
    n && (_(), c == null || c(n.value, n, e));
  }, [c]), S = k((e) => u.current && (f == null ? void 0 : f(u.current.value, u.current, e)), [f]);
  return m(() => {
    const {
      value: e,
      ...n
    } = A, a = u.current;
    !a || d === void 0 || (e !== d && (a.value = d, a.value !== d && r()), p(n));
  }, [d]), m(() => {
    const {
      unmaskedValue: e,
      ...n
    } = A, a = u.current;
    !a || o === void 0 || (e !== o && (a.unmaskedValue = o, a.unmaskedValue !== o && r()), p(n));
  }, [o]), m(() => {
    const {
      typedValue: e,
      ...n
    } = A, a = u.current;
    !a || i === void 0 || (e !== i && (a.typedValue = i, a.masked.typedValueEquals(i) || r()), p(n));
  }, [i]), m(() => {
    const e = v.current;
    if (!e || !(t != null && t.mask)) return V();
    const n = u.current;
    n ? n == null || n.updateOptions(t) : e && t != null && t.mask && (u.current = $(e, t), _(), l !== void 0 && M(l), T !== void 0 && I(T), U !== void 0 && R(U));
  }, [t, V, r]), m(() => {
    if (!u.current) return;
    const e = u.current;
    return e.on("accept", r), e.on("complete", S), () => {
      e.off("accept", r), e.off("complete", S);
    };
  }, [r, S]), m(() => V, [V]), {
    ref: v,
    maskRef: u,
    value: d,
    setValue: M,
    unmaskedValue: o,
    setUnmaskedValue: I,
    typedValue: i,
    setTypedValue: R
  };
}
const L = (t, s) => {
  if (typeof t == "function") {
    t(s);
    return;
  }
  t && (t.current = s);
};
function w({ mask: t, ref: s, onChange: c }) {
  const { ref: f } = b({ mask: t ?? /^[\s\S]*$/ }, { onAccept: t ? (l) => c == null ? void 0 : c(l) : void 0 });
  return { mergedRef: k(
    (l) => {
      t && L(f, l), L(s, l);
    },
    [f, s, t]
  ), isMasked: !!t };
}
export {
  w as default
};
//# sourceMappingURL=useInputMask.js.map
