import { jsx as e, Fragment as h, jsxs as p } from "react/jsx-runtime";
import { useEffect as L } from "react";
import { a as S } from "../../useQuery-n4ls1rLk.js";
import { b as D, u as I } from "../../index.esm-BQxOvVZs.js";
import O from "../../components/Card/index.js";
import { S as b } from "../../index-20bzTFSx.js";
import R from "../../components/Layouts.js";
import c from "../FormInputs.js";
import { RenderEach as y } from "../../utils/components/RenderEach.js";
import { RenderIf as C } from "../../utils/components/RenderIf.js";
import "../../utils/functions.js";
import { formClient as v } from "../../config/index.js";
import "../../components/Icons.js";
import "../../clsx-7d8NmkQq.js";
import "../../components/Card/variants.js";
import "../../index-xVF7D3FM.js";
import "../../components/Inputs/variants.js";
import "../../motion-dVICNqWI.js";
import "../../SVGVisualElement-aqO4D4Z5.js";
import "../../LayoutGroupContext-3euHzeKi.js";
import "../../hooks/useHasNextSiblingByAttribute.js";
import "../../context/generalContext/index.js";
import "../../context/index.js";
import "../../axios-f2q_J-wz.js";
const ar = ({ payload: d, url: r, config: T }) => {
  const { fields: g, replace: A } = D({
    name: "signatories"
  }), { data: o, isPending: u, isError: E } = S({
    queryKey: ["signatories"],
    queryFn: async () => (await v.post(r, d, T)).data.findFirmantesByContribuyente.items,
    select: (n) => n.reduce(
      (i, l) => (l.firmante_cargo === "REPRESENTANTE LEGAL" ? (i["REPRESENTANTE LEGAL"] || (i["REPRESENTANTE LEGAL"] = []), i["REPRESENTANTE LEGAL"].push(l)) : l.firmante_cargo === "CONTADOR" && (i.CONTADOR || (i.CONTADOR = []), i.CONTADOR.push(l)), i),
      {
        "REPRESENTANTE LEGAL": [],
        CONTADOR: []
      }
    ),
    staleTime: 0,
    gcTime: 0
  }), s = o ?? {
    "REPRESENTANTE LEGAL": [],
    CONTADOR: []
  }, m = [...s["REPRESENTANTE LEGAL"], ...s.CONTADOR];
  return L(() => {
    const n = m == null ? void 0 : m.map((t) => ({
      firmName: t.firmante_nombre,
      firmDoc: t.firmante_tipo_documento,
      firmNumDoc: t.firmante_documento,
      fisNumTarjetaProf: t.firmante_cargo == "REPRESENTANTE LEGAL" ? void 0 : t.firmante_tarjeta_profesional,
      firmTipo: t.firmante_tipo,
      firmante_cargo: t.firmante_cargo
    }));
    (!o || ((o == null ? void 0 : o.length) ?? 0) == 0) && A(n.slice(0, 2));
  }, [o]), !o && !u ? /* @__PURE__ */ e(h, { children: "No hay data..." }) : u ? /* @__PURE__ */ e(h, { children: "Loading..." }) : E ? /* @__PURE__ */ e(h, { children: "Oops hubo un error" }) : /* @__PURE__ */ e(
    y,
    {
      of: g,
      render: (n, t) => /* @__PURE__ */ e(
        F,
        {
          data: m[t],
          index: t,
          dataArray: m,
          dataWithoutPrincipal: o.CONTADOR,
          dataWithPrincipal: o["REPRESENTANTE LEGAL"]
        },
        n.id
      )
    }
  );
}, F = ({ data: d, index: r, dataArray: T, dataWithoutPrincipal: g, dataWithPrincipal: A }) => {
  const {
    setValue: o,
    watch: u,
    formState: { errors: E }
  } = I(), s = (d == null ? void 0 : d.firmante_tipo) == "PRINCIPAL", m = s ? "FIRMA DEL RESPONSABLE FISCAL" : "FIRMA DEL DECLARANTE", n = (N) => {
    N.preventDefault();
    const f = N.target.value, a = T.find((_) => _.firmante_nombre == f);
    o(`signatories.${r}.firmName`, f, {
      shouldValidate: !0
    }), a && (o(`signatories.${r}.firmDoc`, a.firmante_tipo_documento, {
      shouldValidate: !0
    }), o(`signatories.${r}.firmNumDoc`, a.firmante_documento, {
      shouldValidate: !0
    }), o(`signatories.${r}.firmTipo`, a.firmante_tipo, {
      shouldValidate: !0
    }), o(`signatories.${r}.firmante_cargo`, a.firmante_cargo, {
      shouldValidate: !0
    }), s || o(`signatories.${r}.fisNumTarjetaProf`, a.firmante_tarjeta_profesional, {
      shouldValidate: !0
    }));
  }, [t, i, l] = u([`signatories.${r}.firmName`, `signatories.${r}.firmDoc`, `signatories.${r}.firmNumDoc`]), P = (N) => {
    const f = E == null ? void 0 : E.signatories;
    if (Array.isArray(f)) {
      const a = f ? f[r] : void 0, _ = a ? a[N] : void 0;
      return _ ? _.message : void 0;
    }
  };
  return /* @__PURE__ */ e(O.Wrapper, { titleToShow: m, className: "_mx-4 _my-4", variant: { shadow: "withShadow" }, children: /* @__PURE__ */ p("div", { className: "_px-4 _py-2 _flex _flex-col _gap-[8px]", children: [
    /* @__PURE__ */ e(C, { condition: !s, children: /* @__PURE__ */ p(R.InputsWrapper, { children: [
      /* @__PURE__ */ e("div", { className: "_flex _flex-col _flex-1", children: /* @__PURE__ */ p(R.InputsWrapper, { className: "xl:_mt-2", children: [
        /* @__PURE__ */ e(
          c.RadioFormCntx,
          {
            inputProps: { value: "CONTADOR", disabled: i },
            currentId: `signatories.${r}.firmante_cargo`,
            label: "Contador"
          }
        ),
        /* @__PURE__ */ e(
          c.RadioFormCntx,
          {
            inputProps: { value: "REVISOR_FISCAL", disabled: i },
            currentId: `signatories.${r}.firmante_cargo`,
            label: "Revisor Fiscal"
          }
        )
      ] }) }),
      /* @__PURE__ */ e("div", { className: "flex-1" })
    ] }) }),
    /* @__PURE__ */ e(
      b,
      {
        inputProps: { onChange: n, value: u(`signatories.${r}.firmName`) },
        label: "Nombres y Apellidos",
        selectOptions: s ? A : g,
        placeholder: "Seleccionar un declarante",
        optionsEntries: ["firmante_nombre", "firmante_nombre"]
      }
    ),
    /* @__PURE__ */ p(R.InputsWrapper, { children: [
      /* @__PURE__ */ p("div", { className: "_flex _flex-col _flex-1", children: [
        /* @__PURE__ */ e("p", { className: "_text-input", children: "Tipo de identificación*" }),
        /* @__PURE__ */ p(R.InputsWrapper, { className: "xl:_mt-2", children: [
          /* @__PURE__ */ e(
            c.RadioFormCntx,
            {
              inputProps: { value: "CC", disabled: i },
              currentId: `signatories.${r}.firmDoc`,
              label: "Cédula de Ciudadanía"
            }
          ),
          /* @__PURE__ */ e(
            c.RadioFormCntx,
            {
              inputProps: { value: "CE", disabled: i },
              currentId: `signatories.${r}.firmDoc`,
              label: "Cédula de Extranjería"
            }
          )
        ] })
      ] }),
      /* @__PURE__ */ e(c.InputFormCntx, { currentId: `signatories.${r}.firmNumDoc`, label: "No. de documento*", inputProps: { disabled: l } })
    ] }),
    /* @__PURE__ */ e(C, { condition: !s, children: /* @__PURE__ */ e(
      c.InputFormCntx,
      {
        fieldError: P("fisNumTarjetaProf"),
        currentId: `signatories.${r}.fisNumTarjetaProf`,
        label: "No. Tarjeta Profesional",
        inputProps: { disabled: t }
      }
    ) })
  ] }) });
};
export {
  ar as CardsSignatories
};
