import { jsx as D } from "react/jsx-runtime";
import { useState as n, useEffect as y, useCallback as L } from "react";
import { I as i, F as R } from "./FormInputIcon.D-QBOOZ2.js";
const T = {
  value: "",
  helper: "",
  isError: !1,
  withIcon: !1
};
function j(...u) {
  let t, f, e, s, p, r;
  u[0] instanceof Function ? [r, s = T] = u : [t = "", r] = u, typeof s == "object" && ({
    value: t = "",
    helper: e,
    isError: f,
    readOnly: p
  } = s);
  const [c, I] = n(t || ""), [O, v] = n(f), [C, E] = n(e), [h, m] = n(i.IDLE), [S, d] = n(!1);
  y(() => {
    let a = !1;
    return async function() {
      if (r && S) {
        let l, o;
        m(i.LOADING);
        try {
          l = await r(c) || "", o = !!l;
        } catch (x) {
          l = x?.message, o = !0;
        }
        if (a)
          return;
        v(o), E(!o && e ? e : l);
        const w = o ? i.ERROR : i.SUCCESS;
        m(w);
      }
    }(), () => {
      a = !0;
    };
  }, [c, S, r, e]), y(() => {
    t && (d(!0), I(t));
  }, [t]);
  const F = L(async (a) => {
    d(!0), I(a.target.value);
  }, []);
  return {
    value: c,
    isError: O,
    helper: C,
    status: h,
    readOnly: p,
    onChange: F,
    ...s && s.withIcon && { suffixIcon: /* @__PURE__ */ D(R, { status: h }) }
  };
}
export {
  j as u
};
