import '@das-fed/upf-ui/packages/components/image-upload/style.css';
import { withInstall as F } from "@das-fed/upf-utils/with-install/index";
import { currentLang as O, i18n as A, t as z, setI18nRule as E } from "@das-fed/upf-web/packages/i18n/index";
import { computed as _, defineComponent as R, openBlock as K, createElementBlock as W, createCommentVNode as S, withDirectives as b, createVNode as p, unref as o, normalizeClass as P, withCtx as m, createElementVNode as h, toDisplayString as w, vShow as N, createTextVNode as H } from "vue";
import { UploadDragger as L, Upload as q } from "ant-design-vue";
import "ant-design-vue/es/upload/style/css";
import { DasSpin as G } from "@das-fed/upf-ui/packages/components/spin/index";
import { DasButton as J } from "@das-fed/upf-ui/packages/components/button/index";
import { DasIconAddto as Q } from "@das-fed/upf-ui/packages/icons/addto/index";
import { DasIconWarning as X } from "@das-fed/upf-ui/packages/icons/warning/index";
import { DasMessage as I } from "@das-fed/upf-ui/packages/components/message/index";
import { getFileExtension as Y } from "@das-fed/upf-utils/common-tools";
import { useModelValue as Z } from "@das-fed/upf-utils/vue";
const ee = () => ({ size2B: (t) => {
  isNaN(Number(t)) ? t = t.toString() : t = t + "M";
  const i = Number(t.slice(0, -1)), r = t[t.length - 1].toUpperCase(), n = 1024, g = {
    M: n * n,
    K: n,
    B: 1
  };
  return { finalSize: i * g[r], sizeText: t };
} }), ae = (e, t) => {
  const i = Z(e, "status"), { size2B: r } = ee(), n = [".mp4", ".avi", ".mkv", ".mov", ".wmv", ".flv", ".mpeg", ".webm"], g = [".mp3", ".wav", ".aac", ".flac", ".ogg", ".alff", ".wma", ".m4a"], s = _(() => {
    var f;
    let a = e.accept ? (f = e.accept) == null ? void 0 : f.split(",") : [];
    return a.includes("image/jpg") && !a.includes("image/jpeg") && a.push("image/jpeg"), a.includes(".jpg") && !a.includes(".jpeg") && a.push(".jpeg"), e.acceptVideo && (a = [...a, ...n]), e.acceptAudio && (a = [...a, ...g]), a.join(",");
  }), v = _(() => i.value === "uploading"), y = _(() => i.value === "error");
  return { isAccept: s, uploadStatus: i, loading: v, errored: y, beforeupload: (a) => {
    var M, $, D;
    if (e.maxSize) {
      const { finalSize: c, sizeText: u } = r(e.maxSize);
      if (a.size > c) {
        const C = e.acceptAudio || e.acceptVideo ? "das-ui-import-upload-key4" : "das-ui-import-upload-key3", d = O.value === "zh-CN" ? "、" : ", ", k = A(
          C,
          u,
          ((D = ($ = (M = s.value) == null ? void 0 : M.toUpperCase()) == null ? void 0 : $.replace(/\./g, "")) == null ? void 0 : D.replace(/,/g, d)) ?? ""
        ).value, x = e.errorMessage || k;
        return I.warning(x), t("update:status", "normal"), i.value = "normal", !1;
      }
    }
    const f = () => (i.value = "uploading", t("update:status", "uploading"), t("image-upload", a), !1);
    if (e.customCheck) {
      const c = e.customCheck(a);
      if (c instanceof Promise)
        return new Promise(async (u, C) => {
          let d;
          try {
            d = await c.then();
          } catch {
            d = !1;
          }
          d ? f() : C(!1);
        });
      if (!c)
        return t("update:status", "normal"), i.value = "normal", !1;
    } else if (s.value) {
      const c = s.value.split(",").filter((l) => l.trim().startsWith(".")).map((l) => l.trim()), u = s.value.split(",").filter((l) => !l.trim().startsWith(".")).map((l) => l.trim()), C = Y(a.name), d = c.includes(C);
      let k = u.includes("image/*") && a.type.split("/")[0].trim() === "image", x = u.includes("video/*") && a.type.split("/")[0].trim() === "video", T = u.includes("audio/*") && a.type.split("/")[0].trim() === "audio", U = u.includes(a.type) || k || x || T;
      if (!d && !U) {
        const l = e.acceptAudio || e.acceptVideo ? "das-ui-import-upload-key2" : "das-ui-import-upload-key1", V = O.value === "zh-CN" ? "、" : ", ", j = e.errorMessage || A(l, s.value.toUpperCase().replace(/\./g, "").replace(/,/g, V)).value;
        return I.warning(j), !1;
      }
    }
    return f();
  } };
}, te = { class: "das-ui-image-upload" }, oe = { class: "das-ui-image-upload-dragger-context" }, ie = { class: "text" }, ne = { class: "das-ui-image-upload-error-container" }, se = { class: "tips-container" }, le = { class: "text" }, re = /* @__PURE__ */ R({
  __name: "Index",
  props: {
    maxCount: { default: 1 },
    status: { default: "normal" },
    message: { default: "" },
    errorMessage: { default: "" },
    accept: { default: ".png, .jpg, .jpeg, .gif, .svg" },
    acceptVideo: { type: Boolean },
    acceptAudio: { type: Boolean },
    maxSize: {},
    customCheck: {},
    autoContentType: { type: Boolean },
    openFileDialogOnClick: { type: Boolean, default: !0 }
  },
  emits: ["update:status", "image-upload"],
  setup(e, { emit: t }) {
    const i = e, r = t, { isAccept: n, uploadStatus: g, loading: s, errored: v, beforeupload: y } = ae(i, r);
    return (B, a) => (K(), W("div", te, [
      S(" 图片上传组件 "),
      b(p(o(G), {
        class: P([{ "loading-border": o(s) }]),
        type: "component",
        spinning: o(s)
      }, {
        default: m(() => [
          p(o(L), {
            class: "das-ui-image-upload-dragger",
            accept: o(n),
            "max-count": e.maxCount,
            "before-upload": o(y),
            openFileDialogOnClick: e.openFileDialogOnClick
          }, {
            itemRender: m(({}) => [...a[0] || (a[0] = [])]),
            default: m(() => [
              b(h(
                "div",
                oe,
                [
                  p(o(Q), { iconColor: "var(--upf-text-secondary)" }),
                  h(
                    "div",
                    ie,
                    w(e.message || o(z)("das-image-upload-text")),
                    1
                    /* TEXT */
                  )
                ],
                512
                /* NEED_PATCH */
              ), [
                [N, o(g) === "normal"]
              ])
            ]),
            _: 1
            /* STABLE */
          }, 8, ["accept", "max-count", "before-upload", "openFileDialogOnClick"])
        ]),
        _: 1
        /* STABLE */
      }, 8, ["class", "spinning"]), [
        [N, !o(v)]
      ]),
      S(" 错误状态 "),
      b(h(
        "div",
        ne,
        [
          h("div", se, [
            p(o(X), {
              iconColor: "var(--upf-color-error)",
              size: "small"
            }),
            h(
              "div",
              le,
              w(o(z)("das-image-upload-fail")),
              1
              /* TEXT */
            )
          ]),
          p(o(q), {
            accept: o(n),
            "before-upload": o(y),
            "max-count": e.maxCount,
            openFileDialogOnClick: e.openFileDialogOnClick
          }, {
            itemRender: m(({}) => [...a[1] || (a[1] = [])]),
            default: m(() => [
              p(o(J), {
                "btn-type": "primary-text",
                block: ""
              }, {
                default: m(() => [
                  H(
                    w(o(z)("das-image-upload-reupload")),
                    1
                    /* TEXT */
                  )
                ]),
                _: 1
                /* STABLE */
              })
            ]),
            _: 1
            /* STABLE */
          }, 8, ["accept", "before-upload", "max-count", "openFileDialogOnClick"])
        ],
        512
        /* NEED_PATCH */
      ), [
        [N, o(v)]
      ])
    ]));
  }
}), ce = (e, t) => {
  const i = e.__vccOpts || e;
  for (const [r, n] of t)
    i[r] = n;
  return i;
}, ue = /* @__PURE__ */ ce(re, [["__scopeId", "data-v-4eb3ab93"]]), de = {
  "das-ui-import-upload-key1": {
    "zh-CN": "上传的图片格式只能是$0",
    en: "The uploaded picture format can only be $0",
    "zh-MO": "上傳的圖片格式只能是$0",
    _appCode: "framework"
  },
  "das-ui-import-upload-key2": {
    "zh-CN": "上传的文件格式只能是$0",
    en: "The uploaded file format can only be $0",
    "zh-MO": "上傳的檔案格式只能是$0",
    _appCode: "framework"
  },
  "das-ui-import-upload-key3": {
    "zh-CN": "上传的图片大小不能超过$0的$1文件",
    en: "The uploaded picture format can only be $0",
    "zh-MO": "上傳的圖片大小不能超過$0的$1檔案",
    _appCode: "framework"
  },
  "das-ui-import-upload-key4": {
    "zh-CN": "上传的文件大小不能超过$0的$1文件",
    en: "The uploaded file format can only be $0",
    "zh-MO": "上傳的文件大小不能超過$0的$1檔案",
    _appCode: "framework"
  },
  "das-image-upload-text": { "zh-CN": "上传图片", en: "Upload Image", "zh-MO": "上傳圖片", _appCode: "framework" },
  "das-image-upload-reupload": { "zh-CN": "重新上传", en: "Re-upload", "zh-MO": "重新上傳", _appCode: "framework" },
  "das-image-upload-fail": { "zh-CN": "上传失败", en: "Uploaded Failed", "zh-MO": "上傳失敗", _appCode: "framework" }
}, be = F(ue);
E(de);
export {
  be as DasImageUpload,
  be as default
};
