import { j as e } from "../../_virtual/jsx-runtime.js";
import { useRef as B, useEffect as E } from "react";
import { UploaderWrapper as _, ActionButtonsWrapper as A, FileContainer as J } from "./styles.js";
import { Button as l } from "../Button/index.js";
import { getImageUrlFromFile as N, isImageFile as O, openFileExplorer as u } from "./utils.js";
import { FileInfoWrapper as P } from "./FileInfoWrapper.js";
import { UploaderContainerWrapper as S } from "./UploaderContainerWrapper.js";
import { getId as x } from "../../utils/index.js";
function V({
  label: j,
  uploaderTitle: b,
  maxSizeDescription: v,
  uploadFileBtnText: y = "Browse",
  removeFileBtnText: k = "Remove",
  retryUploadBtnText: f = "Retry",
  cancelBtnText: C = "Cancel",
  assistText: h,
  optionalText: I,
  fileType: F = "*",
  successMessage: U,
  errorMessage: a,
  width: R,
  getFileCallback: w,
  removeFileCallback: W,
  cancelFileCallback: $,
  file: t,
  shouldDisplayThumbnail: c = !1,
  loading: r,
  ...d
}) {
  const s = Date.now(), i = B(null), p = `uploader_${s}`, m = `file_${s}`, n = x(`${d["data-testid"]}-uploader`), o = x(`${d["data-trackid"]}-uploader`);
  return E(() => {
    N({ file: t, fileId: m });
  }, [JSON.stringify(t)]), /* @__PURE__ */ e.jsxs(
    S,
    {
      "data-testid": n("container"),
      "data-trackid": o("container"),
      width: R,
      optionalText: I,
      assistText: h,
      label: j,
      children: [
        /* @__PURE__ */ e.jsx(
          "input",
          {
            id: p,
            "data-testid": n("input"),
            type: "file",
            ref: i,
            onChange: w,
            name: "uploader",
            multiple: !1,
            accept: F,
            style: { display: "none" },
            onClick: () => {
              i != null && i.current && (i.current.value = "");
            }
          }
        ),
        /* @__PURE__ */ e.jsxs(
          _,
          {
            hasFile: !!t,
            "data-testid": n("wrapper"),
            "data-trackid": o("wrapper"),
            "aria-label": d["aria-label"],
            shouldDisplayThumbnail: c,
            children: [
              /* @__PURE__ */ e.jsx(
                P,
                {
                  file: t,
                  uploaderTitle: b,
                  maxSizeDescription: v,
                  loading: r,
                  successMessage: U,
                  errorMessage: a
                }
              ),
              /* @__PURE__ */ e.jsxs(A, { children: [
                !a && !t && r && /* @__PURE__ */ e.jsx(
                  l,
                  {
                    variant: "secondary",
                    size: "small-expressive",
                    onClick: $,
                    "aria-label": "Cancel",
                    children: C
                  }
                ),
                !a && !t && !r && /* @__PURE__ */ e.jsx(
                  l,
                  {
                    variant: "secondary",
                    size: "small-expressive",
                    "aria-label": "Upload",
                    "data-testid": n("upload-button"),
                    "data-trackid": o("upload-button"),
                    onClick: () => u({ inputId: p, file: t, errorMessage: a }),
                    children: y
                  }
                ),
                (a || t) && !r && /* @__PURE__ */ e.jsx(
                  l,
                  {
                    variant: "secondary",
                    size: "small-expressive",
                    "data-testid": n(
                      a ? "retry-button" : "remove-button"
                    ),
                    "data-trackid": o(
                      a ? "retry-button" : "remove-button"
                    ),
                    "aria-label": a ? "Retry" : "Remove",
                    onClick: (z) => {
                      z.stopPropagation(), W(), a && u({ inputId: p, file: t, errorMessage: a });
                    },
                    children: a ? f : k
                  }
                )
              ] })
            ]
          }
        ),
        c && t && O(t) && !a && /* @__PURE__ */ e.jsx(J, { children: /* @__PURE__ */ e.jsx(
          "img",
          {
            id: m,
            "data-testid": n("thumbnail"),
            src: "@jet-pie/react/src/components/Uploader/index",
            alt: t && typeof t != "string" ? t.name : ""
          }
        ) })
      ]
    }
  );
}
export {
  V as Uploader
};
