import { ElUpload as se, ElButton as le, ElLink as ae, ElDivider as ne, ElTooltip as ie } from "element-plus/es";
import { defineComponent as re, mergeDefaults as de, useSlots as ue, ref as L, watch as ce, createElementBlock as l, openBlock as s, createBlock as _, createCommentVNode as n, mergeProps as pe, unref as y, createSlots as me, withCtx as c, renderSlot as C, Fragment as m, createElementVNode as a, toDisplayString as v, renderList as R, normalizeProps as fe, guardReactiveProps as ge, normalizeClass as S, createVNode as h, createTextVNode as B, normalizeStyle as D, nextTick as $ } from "vue";
import I from "./icons/icon-upload-btn.svg.js";
import T from "./icons/icon-upload-btn-hover.svg.js";
import K from "./icons/icon-upload-btn-disabled.svg.js";
import ye from "./icons/icon-loading-blue.svg.js";
import ve from "./icons/icon-upload-success.svg.js";
import ke from "./icons/icon-upload-retry.svg.js";
import we from "./icons/icon-upload-fail.svg.js";
import { ElMessageBox as he, ElMessage as E } from "element-plus";
import { useUpload as be } from "./hooks/useUpload.js";
import { useUploadRetry as Be } from "./hooks/useUploadRetry.js";
import { downloadFileWithAxios as _e } from "./utils/file.js";
import { getFileIconByName as Ce } from "./utils/file-icon-maps.js";
import { useExposeMethods as Se } from "./expose/index.js";
import { useCiUploadComputed as ze } from "./computed/index.js";
import { defaultProps as Le } from "./props/index.js";
const $e = { class: "CiUpload" }, Ee = {
  key: 0,
  class: "tip-slot-textInfo"
}, Ue = {
  key: 1,
  class: "default-tip-slot-textInfo"
}, Ne = { class: "upload-btn-text" }, Pe = {
  key: 0,
  class: "tip-slot-textInfo"
}, Re = {
  key: 1,
  class: "default-tip-slot-textInfo"
}, De = { class: "file-item-top" }, Ie = { class: "file-name-file-icon" }, Te = ["src"], Ke = { class: "top-right" }, Me = {
  key: 0,
  class: "file-size"
}, Ve = {
  key: 1,
  class: "file-operate-btn"
}, qe = { class: "file-status-icon-wrapper" }, Ae = { class: "uploading-percentage" }, He = ["onClick"], We = {
  key: 0,
  class: "file-item-bottom file-progress-bar"
}, Fe = {
  key: 0,
  class: "file-item-bottom file-progress-bar download-progress-bar"
}, Oe = {
  name: "CiUpload"
}, dt = /* @__PURE__ */ re({
  ...Oe,
  props: /* @__PURE__ */ de({
    action: {},
    downloadUrl: {},
    uploadBtnText: {},
    maxSize: {},
    multiple: { type: Boolean },
    limit: {},
    accept: {},
    fileList: {},
    beforeUpload: {},
    showUploadProgress: { type: Boolean },
    showDownloadProgress: { type: Boolean },
    showFileExtIcon: { type: Boolean },
    showFileSize: { type: Boolean },
    showPreviewBtn: { type: Boolean },
    showDownloadBtn: { type: Boolean },
    showDeleteBtn: { type: Boolean },
    drag: { type: Boolean },
    disabled: { type: Boolean },
    readonly: { type: Boolean },
    axiosInstance: {}
  }, Le),
  emits: ["update:fileList", "onSuccess", "onRemove", "onChange", "onError"],
  setup(M, { expose: V, emit: q }) {
    const A = ue(), U = L(), b = L(!1), k = L([]), { checkUploadStatus: H } = Se(k), r = M, { customHttpRequest: W } = be(r.axiosInstance), d = q;
    V({
      checkUploadStatus: H
    });
    const { retryUpload: F } = Be(k, d, r);
    ce(
      () => r.fileList,
      (e) => {
        k.value = e.map((t) => ({ ...t }));
      },
      { immediate: !0 }
    );
    const z = (e) => {
      var t;
      (t = U.value) == null || t.handleRemove(e);
    }, N = (e) => {
      e.downloadPercentage = 0, e.status = "downloading", _e(
        {
          fileKey: e.fileKey || "当前此条数据没有fileKey字段",
          fileName: e.fileName || "当前此条数据没有fileName字段"
        },
        r.downloadUrl,
        (t) => {
          e.downloadPercentage = t, t === 100 && (e.status = "success");
        },
        (t) => {
          e.status = "success";
        },
        r.axiosInstance
      );
    }, O = (e) => {
      const t = r.accept.split(",").map((i) => i.trim().toLowerCase()), u = e.type.toLowerCase(), f = e.name.toLowerCase();
      return t.some((i) => u === i || f.endsWith(i)) ? e.size <= r.maxSize ? !0 : (E.error(`文件大小超出限制，最大文件大小为${ee.value}`), b.value = !0, $(() => {
        const i = r.fileList.filter((o) => o.uid !== e.uid);
        d("update:fileList", i), b.value = !1;
      }), !1) : (E.error("不支持上传该类型文件"), b.value = !0, $(() => {
        const i = r.fileList.filter((o) => o.uid !== e.uid);
        d("update:fileList", i), b.value = !1;
      }), !1);
    }, j = (e, t) => b.value ? !0 : he.confirm(
      `确定删除 ${e.fileName || e.name} 文件？`,
      "确定删除",
      {
        // 是否可以点击遮罩层关闭
        closeOnClickModal: !1,
        type: "warning"
      }
    ).then(
      () => !0,
      // 用户点击“确定”按钮，允许删除
      () => !1
      // 用户点击“取消”按钮，取消删除
    ), x = (e, t) => {
      E.error(`文件数量超出限制，最大文件数量为${r.limit}个`);
    }, G = (e, t, u) => {
      t.status = "fail", t.percentage = 0, $(() => {
        d("update:fileList", [...u, t]), d("onError", t, u);
      });
    }, J = (e, t, u) => {
      var p, w, i;
      if (!e) return;
      const f = u.find((o) => o.uid === t.uid);
      f && (f.fileName = ((p = e == null ? void 0 : e.data) == null ? void 0 : p.fileName) || t.name, f.fileKey = ((w = e == null ? void 0 : e.data) == null ? void 0 : w.fileKey) || "当前接口没有返回fileKey字段", f.fileSize = ((i = e == null ? void 0 : e.data) == null ? void 0 : i.fileSize) || t.size), d("update:fileList", u), d("onSuccess", t, u);
    }, Q = (e, t) => {
      e.status === "success" && !e.response || (d("update:fileList", t), d("onChange", e, t));
    }, X = (e, t) => {
      d("update:fileList", t), d("onRemove", e, t);
    }, { hasRealDefaultSlot: Y, finalBeforeUpload: Z, maxSizeInMB: ee, defaultTipSlotTextInfo: P, otherSlots: te } = ze(r, A, O);
    return (e, t) => {
      const u = le, f = se, p = ae, w = ne, i = ie;
      return s(), l("div", $e, [
        e.readonly ? n("", !0) : (s(), _(f, pe({
          key: 0,
          class: ["ci-el-upload-component", { "is-disabled": e.disabled }],
          ref_key: "uploadRef",
          ref: U,
          action: e.action,
          "file-list": k.value,
          accept: e.accept,
          limit: e.limit,
          multiple: e.multiple,
          drag: e.drag,
          disabled: e.disabled,
          "before-upload": y(Z),
          "before-remove": j,
          "on-success": J,
          "on-change": Q,
          "on-remove": X,
          "on-exceed": x,
          "on-error": G,
          "http-request": y(W),
          "show-file-list": !1
        }, e.$attrs), me({
          default: c(() => [
            y(Y) ? C(e.$slots, "default", { key: 0 }, void 0, !0) : (s(), l(m, { key: 1 }, [
              e.drag ? (s(), l(m, { key: 0 }, [
                t[0] || (t[0] = a("img", {
                  class: "upload-btn-icon",
                  src: I,
                  alt: ""
                }, null, -1)),
                t[1] || (t[1] = a("img", {
                  class: "upload-btn-icon-hover",
                  src: T,
                  alt: ""
                }, null, -1)),
                t[2] || (t[2] = a("img", {
                  class: "upload-btn-icon-disabled",
                  src: K,
                  alt: ""
                }, null, -1)),
                t[3] || (t[3] = a("div", { class: "drag-text-info" }, [
                  a("span", { class: "drag-text-info-click" }, "点击上传"),
                  a("span", { class: "drag-text-info-black" }, "或将文件拖拽到这里上传")
                ], -1)),
                e.$slots.tip ? (s(), l("div", Ee, [
                  C(e.$slots, "tip", {}, void 0, !0)
                ])) : (s(), l("div", Ue, v(y(P)), 1))
              ], 64)) : (s(), _(u, {
                key: 1,
                class: "upload-btn",
                disabled: e.disabled
              }, {
                default: c(() => [
                  t[4] || (t[4] = a("img", {
                    class: "upload-btn-icon",
                    src: I,
                    alt: ""
                  }, null, -1)),
                  t[5] || (t[5] = a("img", {
                    class: "upload-btn-icon-hover",
                    src: T,
                    alt: ""
                  }, null, -1)),
                  t[6] || (t[6] = a("img", {
                    class: "upload-btn-icon-disabled",
                    src: K,
                    alt: ""
                  }, null, -1)),
                  a("span", Ne, v(e.uploadBtnText), 1)
                ]),
                _: 1,
                __: [4, 5, 6]
              }, 8, ["disabled"]))
            ], 64))
          ]),
          _: 2
        }, [
          e.drag ? void 0 : {
            name: "tip",
            fn: c(() => [
              e.$slots.tip ? (s(), l("div", Pe, [
                C(e.$slots, "tip", {}, void 0, !0)
              ])) : (s(), l("div", Re, v(y(P)), 1))
            ]),
            key: "0"
          },
          R(y(te), (o, g) => ({
            name: g,
            fn: c((oe) => [
              C(e.$slots, g, fe(ge(oe || {})), void 0, !0)
            ])
          }))
        ]), 1040, ["class", "action", "file-list", "accept", "limit", "multiple", "drag", "disabled", "before-upload", "http-request"])),
        k.value.length ? (s(), l("div", {
          key: 1,
          class: S(["custom-file-list-default", { "is-readonly": e.readonly }])
        }, [
          (s(!0), l(m, null, R(k.value, (o) => (s(), l("div", {
            class: "file-item-wrapper",
            key: o.uid
          }, [
            a("div", De, [
              a("div", Ie, [
                e.showFileExtIcon ? (s(), l("img", {
                  key: 0,
                  class: "file-icon",
                  src: y(Ce)(o.fileName || o.name)
                }, null, 8, Te)) : n("", !0),
                h(p, {
                  class: "top-left file-name",
                  disabled: o.status === "downloading" || o.status === "uploading",
                  onClick: (g) => N(o)
                }, {
                  default: c(() => [
                    B(v(o.fileName || o.name), 1)
                  ]),
                  _: 2
                }, 1032, ["disabled", "onClick"])
              ]),
              a("div", Ke, [
                e.showFileSize ? (s(), l("span", Me, v(o.fileSize || o.size ? ((o.fileSize || o.size) / 1024 / 1024).toFixed(2) + "MB" : "无文件大小"), 1)) : n("", !0),
                o.status !== "fail" ? (s(), l("div", Ve, [
                  o.status !== "uploading" ? (s(), l(m, { key: 0 }, [
                    e.showPreviewBtn ? (s(), l(m, { key: 0 }, [
                      h(p, {
                        onClick: (g) => z(o)
                      }, {
                        default: c(() => t[7] || (t[7] = [
                          B(" 在线预览 ")
                        ])),
                        _: 2,
                        __: [7]
                      }, 1032, ["onClick"]),
                      h(w, { direction: "vertical" })
                    ], 64)) : n("", !0),
                    e.showDownloadBtn ? (s(), l(m, { key: 1 }, [
                      h(p, {
                        disabled: o.status === "downloading",
                        onClick: (g) => N(o)
                      }, {
                        default: c(() => [
                          B(v(o.status === "downloading" ? `下载中...${o.downloadPercentage || 0}%` : "下载"), 1)
                        ]),
                        _: 2
                      }, 1032, ["disabled", "onClick"]),
                      e.showDeleteBtn && !e.readonly ? (s(), _(w, {
                        key: 0,
                        direction: "vertical"
                      })) : n("", !0)
                    ], 64)) : n("", !0),
                    e.showDeleteBtn && !e.readonly ? (s(), _(p, {
                      key: 2,
                      onClick: (g) => z(o)
                    }, {
                      default: c(() => t[8] || (t[8] = [
                        B("删除")
                      ])),
                      _: 2,
                      __: [8]
                    }, 1032, ["onClick"])) : n("", !0)
                  ], 64)) : n("", !0)
                ])) : n("", !0),
                a("div", qe, [
                  o.status === "uploading" ? (s(), l("div", {
                    key: 0,
                    class: S(o.status)
                  }, [
                    t[9] || (t[9] = a("img", {
                      class: "uploading-icon",
                      src: ye
                    }, null, -1)),
                    a("span", Ae, v(o.percentage) + "%", 1)
                  ], 2)) : n("", !0),
                  e.readonly ? n("", !0) : (s(), l(m, { key: 1 }, [
                    o.status === "success" || o.status === "downloading" ? (s(), l("span", {
                      key: 0,
                      class: S(o.status)
                    }, t[10] || (t[10] = [
                      a("img", {
                        class: "success-icon",
                        src: ve
                      }, null, -1)
                    ]), 2)) : n("", !0)
                  ], 64)),
                  o.status === "fail" ? (s(), l("div", {
                    key: 2,
                    class: S(["fail-icon-wrapper", o.status])
                  }, [
                    h(p, {
                      onClick: (g) => z(o)
                    }, {
                      default: c(() => t[11] || (t[11] = [
                        B(" 删除 ")
                      ])),
                      _: 2,
                      __: [11]
                    }, 1032, ["onClick"]),
                    h(i, {
                      effect: "dark",
                      content: "点击重新上传",
                      placement: "bottom"
                    }, {
                      default: c(() => [
                        a("img", {
                          class: "retry-icon",
                          src: ke,
                          onClick: (g) => y(F)(o)
                        }, null, 8, He)
                      ]),
                      _: 2
                    }, 1024),
                    t[12] || (t[12] = a("img", {
                      class: "fail-icon",
                      src: we
                    }, null, -1))
                  ], 2)) : n("", !0)
                ])
              ])
            ]),
            e.showUploadProgress ? (s(), l(m, { key: 0 }, [
              o.status === "uploading" ? (s(), l("div", We, [
                a("div", {
                  class: "file-progress-inner",
                  style: D({ width: (o.percentage || 0) + "%" })
                }, null, 4)
              ])) : n("", !0)
            ], 64)) : n("", !0),
            e.showDownloadProgress ? (s(), l(m, { key: 1 }, [
              o.status === "downloading" ? (s(), l("div", Fe, [
                a("div", {
                  class: "file-progress-inner",
                  style: D({ width: (o.downloadPercentage || 0) + "%" })
                }, null, 4)
              ])) : n("", !0)
            ], 64)) : n("", !0)
          ]))), 128))
        ], 2)) : n("", !0)
      ]);
    };
  }
});
export {
  dt as default
};
