import { jsxs as pe, jsx as u } from "react/jsx-runtime";
import { forwardRef as me, useState as d, useRef as fe } from "react";
import { omit as de, isEmpty as he } from "es-toolkit/compat";
import { isNil as Ce, isFunction as ge } from "es-toolkit/predicate";
import { noop as t } from "es-toolkit/function";
import { IMaskInput as ve } from "react-imask";
import h from "../../utils/classNames.js";
import ke from "../../hooks/usePrevious.js";
import ye from "../../hooks/useMergeRefs.js";
const R = "text", Ie = ["text", "password", "email"], T = (i) => !he(`${i}`), De = me((i, C) => {
  const {
    type: D = R,
    defaultValue: M,
    value: s,
    showClearButton: g = !0,
    maxLength: S,
    tabIndex: O = 0,
    hasError: L = !1,
    inputRef: v,
    autoComplete: _,
    onChange: a = t,
    onBlur: j = t,
    onFocus: A = t,
    onClear: k = t,
    onKeyPress: K = t,
    onClick: U = t,
    mask: y,
    maskPlaceholder: Y = "_",
    maskVisibility: z = "always",
    maskOverwrite: $ = !0,
    maskDefinitions: q,
    maskDispatch: I,
    inputClassName: G = "",
    disabled: c = !1,
    readOnly: H = !1,
    className: P = "",
    children: p,
    ...J
  } = i, w = s || M || "", [b, Q] = d(w), [N, W] = d(T(w)), [V, x] = d(!1), B = !Ce(s), o = fe(null), X = ye(o, v, C), r = !!y, Z = () => {
    switch (z) {
      case "always":
        return !0;
      case "onFocus":
        return V;
      case "never":
        return !1;
      default:
        return V;
    }
  }, l = (e = "") => {
    b !== e && (Q(e), W(T(e)));
  };
  ke(s) !== s && l(s);
  const ee = (e) => {
    const n = e.target.value;
    B || l(n), a(n, e);
  }, te = (e, n, we) => {
    if (!o.current)
      return;
    const f = {
      target: o.current,
      currentTarget: o.current,
      // Standard synthetic event props
      bubbles: !0,
      cancelable: !0,
      defaultPrevented: !1,
      isTrusted: !0,
      preventDefault: () => {
      },
      stopPropagation: () => {
      },
      persist: () => {
      },
      nativeEvent: {},
      type: "change",
      timeStamp: Date.now()
    };
    f.target.unmaskedValue = n.unmaskedValue, B || l(e), a(e, f);
  }, se = (e) => {
    x(!0), A(e);
  }, ae = (e) => {
    x(!1), j(e);
  }, ne = (e) => {
    K(e);
  }, oe = (e) => {
    l(""), a("", e), k && k(e);
  }, re = h(
    "ClearableInput",
    "input-group",
    L && "has-error",
    c && "pointer-events-none",
    P && P
  ), le = h(
    "form-control",
    G,
    N && g && "withClearButton",
    r && "withInputMask",
    c && "disabled"
  ), ue = h("clearButton", (!N || !g) && "hide"), E = D?.toLowerCase(), ie = Ie.indexOf(E) !== -1 ? E : R, m = {
    ...de(J, ["value", "defaultValue", "onClear"]),
    className: le,
    autoComplete: _,
    type: ie,
    value: b,
    onKeyPress: ne,
    onBlur: ae,
    onFocus: se,
    onChange: r ? void 0 : ee,
    // In case of masked input, the onAccept callback is used
    onClick: U,
    disabled: c,
    readOnly: H,
    maxLength: r ? void 0 : S,
    tabIndex: O
  }, F = {};
  I && (F.dispatch = I);
  const ce = r ? /* @__PURE__ */ u(
    ve,
    {
      ...m,
      inputRef: X,
      mask: y,
      definitions: q,
      ...F,
      placeholderChar: Y,
      onAccept: te,
      lazy: !Z(),
      overwrite: $
    }
  ) : /* @__PURE__ */ u("input", { ...m, ref: v || C });
  return /* @__PURE__ */ pe("div", { className: re, children: [
    p && ge(p) ? p(m) : ce,
    /* @__PURE__ */ u("span", { className: ue, onClick: oe, children: /* @__PURE__ */ u("span", { className: "clearButtonIcon rioglyph rioglyph-remove-sign" }) })
  ] });
});
export {
  R as DEFAULT_TYPE,
  Ie as SUPPORTED_TYPES,
  De as default
};
//# sourceMappingURL=ClearableInput.js.map
