import { jsx as u } from "react/jsx-runtime";
import { useMemo as V, useEffect as v } from "react";
import { a as B } from "../useQuery-n4ls1rLk.js";
import { u as m, C as y, a as b } from "../index.esm-BQxOvVZs.js";
import { I as E, T as P, S as T, a as q, R as A, C as D, b as R } from "../index-20bzTFSx.js";
import { useGeneralForm as F } from "../context/generalContext/index.js";
import { cn as p } from "../utils/functions.js";
const k = ({ currentId: e, inputProps: n, className: a, fieldError: t, ...r }) => {
  var o;
  const {
    register: l,
    formState: { errors: i }
  } = m();
  return /* @__PURE__ */ u(
    E,
    {
      ...r,
      className: p("_flex-1", a),
      inputProps: { ...n, ...l(e) },
      fieldError: t || ((o = i[e]) == null ? void 0 : o.message)
    }
  );
}, M = ({ currentId: e, inputProps: n, className: a, fieldError: t, ...r }) => {
  var o;
  const {
    register: l,
    formState: { errors: i }
  } = m();
  return /* @__PURE__ */ u(
    P,
    {
      ...r,
      className: p("_flex-1", a),
      inputProps: { ...n, ...l(e) },
      fieldError: t || ((o = i[e]) == null ? void 0 : o.message)
    }
  );
}, w = ({ currentId: e, inputProps: n, disabled: a = !1, className: t, ...r }) => {
  var o;
  const {
    register: l,
    formState: { errors: i }
  } = m();
  return /* @__PURE__ */ u(
    T,
    {
      disabled: a,
      className: p("_flex-1", t),
      ...r,
      inputProps: { ...n, ...l(e) },
      fieldError: (o = i[e]) == null ? void 0 : o.message
    }
  );
}, O = ({ currentId: e, className: n, label: a, labelClass: t }) => {
  const {
    formState: { errors: r },
    control: l
  } = m();
  return /* @__PURE__ */ u(
    y,
    {
      control: l,
      name: e,
      render: ({ field: { value: i, onChange: o } }) => {
        var c;
        return /* @__PURE__ */ u(
          q,
          {
            className: p("_flex-1", n),
            labelClass: t,
            label: a,
            value: i,
            onChange: () => o(!i),
            fieldError: (c = r[e]) == null ? void 0 : c.message
          }
        );
      }
    }
  );
}, j = ({ currentId: e, inputProps: n, label: a }) => {
  const { register: t } = m();
  return /* @__PURE__ */ u(A, { inputProps: { ...n, ...t(e) }, label: a });
}, G = ({ currentId: e, inputProps: n, ...a }) => {
  const { register: t } = m();
  return /* @__PURE__ */ u(D, { inputProps: { ...n, ...t(e) }, ...a });
}, K = ({ currentId: e, inputClass: n, exact: a = !0, ...t }) => {
  const [r] = F((s) => (s.formules_schema ?? {})[e]), [l] = F((s) => s.functions), {
    setValue: i,
    formState: { dirtyFields: o, errors: c, isSubmitting: h, isSubmitted: S },
    control: N
  } = m(), d = b({ name: (r == null ? void 0 : r.req) ?? "", exact: a }), [x, _] = V(() => {
    if (!d)
      return [void 0, void 0];
    let s = [], C = {};
    for (const [g, f] of r.req.entries())
      C[f] = d[g] || 0, s.push(!c[f] && o[f]);
    return [C, s];
  }, [r, c, o, d]);
  return v(() => {
    if (!(!x || !r || h || S) && _.some((s) => s))
      try {
        const s = r.fx(x, l);
        i(e, s, { shouldValidate: !0, shouldDirty: !0, shouldTouch: !0 });
      } catch (s) {
        console.error("Oops hubo un error en el calculo del campo: " + e), console.error(s == null ? void 0 : s.message);
      }
  }, [e, x, _, r, l, h, S, i]), /* @__PURE__ */ u(
    y,
    {
      control: N,
      name: e,
      render: ({ field: { onChange: s, ref: C, ...g } }) => /* @__PURE__ */ u(
        R,
        {
          ...t,
          ...g,
          disabled: t.disabled,
          onValueChange: ({ value: f }) => s(f),
          decimalSeparator: ",",
          thousandSeparator: ".",
          prefix: "$",
          inputClass: n
        }
      )
    }
  );
}, L = ({ currentId: e, targetValue: n, fetchOptions: a, ...t }) => {
  const r = b({ name: n }), {
    data: l,
    isLoading: i,
    error: o
  } = B({
    queryKey: ["Select", e, r],
    queryFn: async () => r ? await a(r) : [],
    enabled: !!r
  });
  return o ? /* @__PURE__ */ u("div", { className: "_h-[43px] _mt-[23px] _w-full _flex-1 _text-red-700", children: o.message }) : i ? /* @__PURE__ */ u("div", { className: "_h-[43px] _mt-[23px] _w-full _flex-1 _bg-gray-200 _animate-pulse" }) : /* @__PURE__ */ u(w, { selectOptions: l || [], currentId: e, ...t });
}, X = {
  InputFormCntx: k,
  SelectFormCntx: w,
  TextAreaFormCntx: M,
  CalcInput: K,
  SelectByAnotherField: L,
  SwitchFormCntx: O,
  RadioFormCntx: j,
  CheckBoxFormCntx: G
};
export {
  X as default
};
