import { defineComponent as K, useCssModule as W, ref as U, inject as Z, useAttrs as q, computed as g, openBlock as a, createElementBlock as c, normalizeClass as u, unref as l, createVNode as E, withModifiers as p, withCtx as x, renderSlot as N, createTextVNode as w, toDisplayString as f, createElementVNode as v, Fragment as k, createBlock as Q, createCommentVNode as R, renderList as ee, withDirectives as te, mergeProps as le, vShow as ne } from "vue";
import se from "@leaflink/snitch";
import { I as ae } from "./index-D6bxWkZ1.js";
import { t as m } from "./locale.js";
import F from "./Button.js";
import oe from "./Icon.js";
import { _ as ie } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const L = {
  CSV: {
    EXTENSION: ["csv"],
    MIME_TYPES: ["text/csv", "application/octet-stream", "application/vnd.ms-excel"],
    ILLUSTRATION: "csv"
  },
  PDF: {
    EXTENSION: ["pdf"],
    MIME_TYPES: ["application/pdf"],
    ILLUSTRATION: "pdf"
  },
  PNG: {
    EXTENSION: ["png"],
    MIME_TYPES: ["image/png"],
    ILLUSTRATION: "image"
  },
  JPEG: {
    EXTENSION: ["jpg", "jpeg"],
    MIME_TYPES: ["image/jpeg"],
    ILLUSTRATION: "image"
  },
  DOC: {
    EXTENSION: ["doc", "docx"],
    MIME_TYPES: ["application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
    ILLUSTRATION: "document"
  },
  XLS: {
    EXTENSION: ["xls", "xlsx"],
    MIME_TYPES: ["application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],
    ILLUSTRATION: "csv"
  }
};
var h = /* @__PURE__ */ ((n) => (n.Dense = "dense", n.Standard = "standard", n))(h || {});
const re = { key: 0 }, ce = { class: "text-ice-900" }, de = {
  key: 0,
  class: "mt-6 text-center text-xs text-ice-700"
}, ue = ["disabled", "accept", "multiple"], pe = /* @__PURE__ */ K({
  __name: "FileUpload",
  props: {
    files: { default: () => [] },
    fileTypes: { default: () => ["CSV", "PDF", "PNG", "JPEG", "DOC", "XLS"] },
    buttonOnly: { type: Boolean, default: !1 },
    multiple: { type: Boolean, default: !1 },
    disabled: { type: Boolean, default: !1 },
    size: { default: "standard" }
  },
  emits: ["file-select", "file-delete", "file-error"],
  setup(n, { emit: A }) {
    const o = n, d = W(), b = A, y = U(!1), T = U(), I = Z("stashOptions"), M = q(), X = g(() => {
      const e = { ...M };
      return delete e["data-test"], delete e.class, delete e.type, delete e.accept, e;
    });
    function D(e, t) {
      return e.concat(t);
    }
    const S = g(() => o.fileTypes.map((e) => L[e].MIME_TYPES).reduce(D)), O = g(() => o.fileTypes.map((e) => L[e].EXTENSION).reduce(D)), B = g(() => `${I == null ? void 0 : I.staticPath}/illustrations/FileUpload/${L[o.fileTypes[0]].ILLUSTRATION}.svg`);
    function _() {
      T.value && (T.value.value = "", T.value.click());
    }
    function Y() {
      y.value = !0;
    }
    function $() {
      y.value = !1;
    }
    function j(e) {
      const t = m("ll.fileUpload.errors.incorrectFileType", {
        fileTypes: O.value.join(", ")
      });
      b("file-error", t), se.log(e);
    }
    async function V(e) {
      if (!S.value.length) return !0;
      const t = await Promise.all(e.map((r) => H(r)));
      if (!(!!t.length && t.every((r) => S.value.includes(r))))
        throw new Error("One or more files contains an unacceptable mime type.");
      if (!e.every((r) => {
        const C = r.name.split(".").pop();
        return C && O.value.includes(C.toLowerCase());
      }))
        throw new Error("One or more files contains an unacceptable extension.");
      return !0;
    }
    async function P(e) {
      try {
        await V(e), b("file-select", { files: e });
      } catch (t) {
        j(t);
      }
    }
    function z(e) {
      var s;
      const t = [...((s = e.target) == null ? void 0 : s.files) || []];
      P(t);
    }
    function G(e) {
      var s;
      if (o.disabled)
        return;
      const t = [...((s = e.dataTransfer) == null ? void 0 : s.files) || []];
      return y.value = !1, P(t);
    }
    function J(e) {
      b("file-delete", e);
    }
    function H(e) {
      return new Promise((t, s) => {
        if (e.type)
          return t(e.type);
        if (window.FileReader) {
          const i = new FileReader();
          i.onload = () => {
            const r = i.result && i.result.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/) ? i.result.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)[0] : "";
            t(r);
          }, i.readAsDataURL(e);
        } else
          s(new Error("Failed to read file."));
      });
    }
    return (e, t) => (a(), c("div", {
      class: u(["stash-file-upload", l(M).class]),
      "data-test": "stash-file-upload"
    }, [
      n.buttonOnly ? (a(), c("div", re, [
        E(F, {
          secondary: "",
          type: "button",
          disabled: o.disabled,
          onClick: p(_, ["stop", "prevent"])
        }, {
          default: x(() => [
            N(e.$slots, "submitText", {}, () => [
              w(f(l(m)("ll.fileUpload.uploadFile")), 1)
            ])
          ]),
          _: 3
        }, 8, ["disabled"])
      ])) : (a(), c("div", {
        key: 1,
        class: u(["rounded p-6", [
          l(d)["file-dropbox"],
          {
            [l(d)["is-dragging"]]: y.value,
            [l(d)["is-disabled"]]: o.disabled
          }
        ]]),
        onDragover: p(Y, ["prevent"]),
        onDrop: p(G, ["prevent"]),
        onDragleave: p($, ["prevent"])
      }, [
        v("div", {
          class: u(["flex flex-col items-center justify-center text-center", [{ "items-center md:flex-row": n.size === l(h).Dense }]])
        }, [
          n.files.length ? (a(!0), c(k, { key: 1 }, ee(n.files, (s) => (a(), c("div", {
            key: s.name
          }, [
            E(oe, { name: "file" }),
            v("span", null, f(s.name), 1),
            E(F, {
              class: u([l(d)["remove-button"], l(d).button]),
              onClick: p((i) => J(s), ["stop", "prevent"])
            }, {
              default: x(() => [
                w(f(l(m)("ll.fileUpload.remove")), 1)
              ]),
              _: 1
            }, 8, ["class", "onClick"])
          ]))), 128)) : (a(), c(k, { key: 0 }, [
            n.size !== l(h).Dense ? (a(), Q(l(ae), {
              key: 0,
              src: B.value,
              name: "file",
              width: "84",
              height: "96"
            }, null, 8, ["src"])) : R("", !0),
            v("span", ce, f(l(m)("ll.fileUpload.dragDropFileHere")), 1),
            v("span", {
              class: u(n.size === l(h).Dense ? "md:ml-1.5 md:mr-3 md:my-0 my-1.5 text-ice-900" : "mt-1.5 my-1.5")
            }, f(l(m)("ll.fileUpload.or")), 3),
            E(F, {
              class: u(["mt-1.5", l(d)["file-select-button"]]),
              secondary: "",
              type: "button",
              disabled: n.disabled,
              onClick: p(_, ["stop", "prevent"])
            }, {
              default: x(() => [
                N(e.$slots, "submitText", {}, () => [
                  w(f(l(m)("ll.fileUpload.uploadFile")), 1)
                ])
              ]),
              _: 3
            }, 8, ["class", "disabled"])
          ], 64))
        ], 2),
        e.$slots.hint && !n.files.length ? (a(), c("div", de, [
          N(e.$slots, "hint")
        ])) : R("", !0)
      ], 34)),
      te(v("input", le(X.value, {
        ref_key: "fileUploadRef",
        ref: T,
        "data-test": "stash-file-upload|input",
        type: "file",
        disabled: n.disabled,
        accept: S.value.join(","),
        multiple: o.multiple,
        onChange: z
      }), null, 16, ue), [
        [ne, !1]
      ])
    ], 2));
  }
}), fe = "_button_ZeikL", me = {
  "file-dropbox": "_file-dropbox_tvvbm",
  "is-dragging": "_is-dragging_B4UnW",
  "is-disabled": "_is-disabled_eiaSr",
  "upload-icon": "_upload-icon_A7tPR",
  "remove-button": "_remove-button_LakK5",
  button: fe
}, ve = {
  $style: me
}, Se = /* @__PURE__ */ ie(pe, [["__cssModules", ve]]);
export {
  L as FILE_TYPES,
  h as FileUploadSizes,
  Se as default
};
//# sourceMappingURL=FileUpload.js.map
