import { jsx as a, jsxs as _ } from "@emotion/react/jsx-runtime";
import { useDropzone as D } from "react-dropzone";
import { useReducer as P, useEffect as v } from "react";
import { ContainerDropZoneStyled as T, DropZoneStyled as h } from "../../slots/RHFUploadSingleFileSlots.js";
import { useHostTools as L, useModuleDictionary as b, useModuleSkeleton as w } from "@m4l/core";
import { COMPONENTS_DICTIONARY_KEYS as p } from "../../dictionary.js";
import { BlockContent as z } from "../BlockContent/BlockContent.js";
import { Preview as O } from "../Preview/Preview.js";
import { RHF_UPLOAD_SINGLE_FILE_CLASSES as g } from "../../constants.js";
import { uploadReducer as A } from "../../helpers/uploadReducer.js";
import { formatFileSize as C } from "../../helpers/formatFileSize.js";
import { getReadableFileType as N } from "../../helpers/getReadableFileType.js";
function B(R) {
  const { file: t, accept: n, onChange: c, maxFileSize: d, size: u, disabled: f } = R, { toast: S } = L(), { getLabel: i } = b(), E = w(), [e, r] = P(A, {
    progress: null,
    preview: !1,
    validFile: !1
  }), { getRootProps: F, getInputProps: I } = D({
    multiple: !1,
    disabled: f,
    /**
     * Maneja el evento de drop del archivo
     */
    onDrop: (o) => {
      try {
        if (r({ type: "RESET" }), o.length > 0) {
          const l = o[0], m = Object.entries(n).some(([s]) => !!(s === "*/*" || l.type === s || s.endsWith("/*") && l.type.startsWith(s.slice(0, -2)))), y = l.size <= d;
          !m || !y ? (m || S({ title: `${i(p.errorFileRejected)} ${i(p.acceptFile)} ${N(Object.keys(n)[0])}.` }, { type: "error" }), y || S({ title: `${i(p.errorMaxSize)} ${i(p.acceptSize)} ${C(d)}.` }, { type: "error" })) : (c?.(l), r({ type: "SET_VALID_FILE", payload: !0 }));
        }
      } catch (l) {
        console.error("Error in file drop:", l);
      }
    }
  });
  return v(() => {
    t && !e.validFile ? (r({ type: "SET_VALID_FILE", payload: !0 }), r({ type: "SET_PREVIEW", payload: !0 })) : !t && e.validFile && r({ type: "RESET" });
  }, [t, e.validFile]), v(() => {
    let o;
    return t && e.validFile && !e.preview && (o = setInterval(() => {
      r({ type: "SET_PROGRESS", payload: (e.progress ?? 0) + 10 }), e.progress !== null && e.progress >= 100 && (clearInterval(o), r({ type: "SET_PROGRESS", payload: null }), r({ type: "SET_PREVIEW", payload: !0 }));
    }, 100)), () => {
      o && clearInterval(o);
    };
  }, [t, e.progress, e.validFile, e.preview]), /* @__PURE__ */ a(T, { ownerState: {}, className: g.containerDropZone, children: /* @__PURE__ */ _(h, { ...F(), ownerState: { isSkeleton: E }, className: g.dropZone, children: [
    /* @__PURE__ */ a(z, { value: e.progress, hidden: e.preview, size: u }),
    e.preview && !!t && /* @__PURE__ */ a(O, { uploadProgress: e.progress, file: t, onChange: c, onClose: () => r({ type: "RESET" }), size: u, disabled: f }),
    !E && /* @__PURE__ */ a("input", { role: "button", "aria-label": "button-upload-single-file", ...I() })
  ] }) });
}
export {
  B as default
};
