import { jsxs as d, jsx as n } from "react/jsx-runtime";
import { useRef as S, useState as k, useEffect as E } from "react";
import M from "../../hooks/useUploadFiles.js";
import w from "../Icons.js";
import { cn as x } from "../../utils/functions.js";
import { a as z } from "../../index-OHO5PlsB.js";
import R from "./CircleProgressLoading.js";
import "../../axios-f2q_J-wz.js";
import "../../clsx-7d8NmkQq.js";
import "../../utils/components/RenderEach.js";
import "../../utils/components/RenderIf.js";
import "./variants.js";
import "../../index-xVF7D3FM.js";
import "../../motion-dVICNqWI.js";
import "../../SVGVisualElement-aqO4D4Z5.js";
import "../../LayoutGroupContext-3euHzeKi.js";
const Q = ({ multiple: m, inputProps: p, setValue: s, currentId: c, fieldError: l, triggerValidation: _, baseURL: h, label: v }) => {
  const { handleUploadImage: N, loadingProgress: y } = M(), f = S(null), [r, u] = k([]), F = (o, t = 2) => {
    if (o === 0)
      return "0 Bytes";
    const e = 1024, i = t < 0 ? 0 : t, a = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], g = Math.floor(Math.log(o) / Math.log(e));
    return parseFloat((o / Math.pow(e, g)).toFixed(i)) + " " + a[g];
  }, B = () => {
    f.current && (f.current.value = null);
  }, b = (o) => {
    const t = r.filter((e) => e.id !== o);
    u(t), s(c, t), _(c);
  }, C = async (o) => {
    const t = [];
    for (let e = 0; e < o.target.files.length; e++) {
      const i = o.target.files[e];
      try {
        const a = await N(h, i);
        t.push({
          id: `file_${Date.now()}_${e}`,
          ...a,
          filename: i.name,
          filetype: i.type,
          datetime: i.lastModifiedDate.toLocaleString("en-IN"),
          filesize: F(i.size),
          fileimage: i.fileimage
        });
      } catch (a) {
        console.error("Error uploading file:", a);
      }
    }
    u((e) => [...e, ...t]), s(c, [...r, ...t]), _(c);
  };
  return E(() => {
    B();
  }, [r]), /* @__PURE__ */ d("div", { className: x("_flex _flex-col _rounded-md"), "data-id": "input-wrapper", children: [
    /* @__PURE__ */ n("label", { className: x("_font-main _text-common _text-input"), children: v }),
    /* @__PURE__ */ d("div", { className: "_flex", children: [
      /* @__PURE__ */ d("label", { className: "_relative _inline-block _cursor-pointer _overflow-hidden _rounded-l-md _bg-primary _px-4 _py-2 _text-white _outline-none", children: [
        "Seleccionar archivo",
        /* @__PURE__ */ n("input", { ...p, ref: f, type: "file", className: "_hidden", multiple: m, onChange: C })
      ] }),
      /* @__PURE__ */ n(
        "input",
        {
          type: "text",
          value: r && r.length > 0 ? `${r.length} archivo(s)` : "No hay archivo seleccionado",
          className: "_flex-grow _rounded-r-md _border _border-gray-300 _px-4 _py-2 _outline-none",
          readOnly: !0
        }
      )
    ] }),
    r && r.map((o) => {
      const { id: t, filename: e, downloadUrl: i, filesize: a } = o;
      return /* @__PURE__ */ d("div", { className: "_mt-4 _flex _items-center", children: [
        /* @__PURE__ */ n(R, { value: y }),
        /* @__PURE__ */ d("div", { className: "_ml-4 _flex _w-[full] _flex-col _overflow-hidden", children: [
          /* @__PURE__ */ n("p", { className: "_truncate", children: e }),
          /* @__PURE__ */ d("span", { children: [
            "Peso: ",
            a
          ] })
        ] }),
        /* @__PURE__ */ n("a", { className: "_cursor-pointer _mx-4", onClick: () => $(`${h}${i}`, e), title: "Descargar", children: /* @__PURE__ */ n(w.SaveIcon, { className: "_h-6 _w-6" }) }),
        /* @__PURE__ */ n("span", { title: "Eliminar", children: /* @__PURE__ */ n(w.DeleteFile, { className: "_fill-red-500 _h-4 _w-4 _cursor-pointer", onClick: () => b(t) }) })
      ] }, t);
    }),
    /* @__PURE__ */ n(z, { message: l })
  ] });
};
function $(m, p) {
  fetch(m).then((s) => s.blob()).then((s) => {
    const c = window.URL.createObjectURL(new Blob([s])), l = document.createElement("a");
    l.href = c, l.setAttribute("download", p || ""), document.body.appendChild(l), l.click(), l && l.parentNode && l.parentNode.removeChild(l);
  });
}
export {
  Q as default
};
