import { defineComponent as I, ref as z, useSlots as P, computed as p, toRef as y, unref as t, openBlock as n, createBlock as T, mergeProps as j, withCtx as i, createElementVNode as b, normalizeClass as V, createElementBlock as m, createVNode as s, createCommentVNode as f, renderSlot as D, createSlots as M, toDisplayString as k, createTextVNode as U, withModifiers as E } from "vue";
import { LoadingOutlined as R } from "@ant-design/icons-vue";
import { Button as G, Tooltip as h } from "@aplus-frontend/antdv";
import { IconApAdLineDownload as W } from "@aplus-frontend/icon";
import { isArray as $, omit as q } from "lodash-unified";
import "../../config-provider/index.mjs";
import { ApFilePreview as H } from "../ap-file-preview/index.mjs";
import { useOssInit as J } from "./hooks/index.mjs";
import { useStyle as K } from "./hooks/useStyle.mjs";
import { getFileInfo as u } from "./utils/getFileInfo.mjs";
import { useLocale as Q } from "../../config-provider/hooks/use-locale.mjs";
import { useGlobalConfig as A } from "../../config-provider/hooks/use-global-config.mjs";
const X = {
  key: 0,
  class: "spin"
}, Y = {
  key: 1,
  class: "icon"
}, Z = { class: "text" }, me = /* @__PURE__ */ I({
  name: "ApDownload",
  __name: "ap-download",
  props: {
    prefixCls: {},
    type: { default: "link" },
    htmlType: {},
    shape: {},
    size: { default: "middle" },
    loading: { type: [Boolean, Object] },
    disabled: { type: Boolean, default: !1 },
    ghost: { type: Boolean },
    block: { type: Boolean },
    danger: { type: Boolean },
    icon: {},
    href: {},
    target: {},
    title: {},
    onClick: {},
    onMousedown: {},
    autoInsertSpace: { type: Boolean },
    iconPosition: {},
    color: {},
    variant: {},
    text: {},
    fileName: {},
    needName: { type: [Boolean, Object], default: void 0 },
    getOssAccess: {},
    preview: { type: Boolean, default: !1 }
  },
  emits: ["downloadCallback"],
  setup(r, { emit: B }) {
    const g = B, d = z(!1), { lang: O, t: v } = Q(), _ = P(), x = p(() => O.value === "zh-cn" ? "zh_CN" : "en_US"), e = r, l = A("apUpload"), w = A(
      "downloadCenterTriggerConfig"
    ), c = p(
      () => e.preview && ($(e.fileName) ? e.fileName.length === 1 : !0)
    ), { getInnerWrapperClass: F } = K(
      y(e, "size"),
      y(e, "disabled")
    ), L = p(
      () => e.getOssAccess || l.value?.getOssAccess
    ), S = () => {
      const a = u(
        e.needName ?? l?.value?.needName,
        e.fileName
      )?.[0];
      H.createFilePreviewModal({
        src: a.path,
        fileName: a.fileName
      });
    }, N = async () => {
      if (!e.disabled) {
        d.value = !0;
        try {
          if (w.value?.trigger) {
            let o = u(
              e.needName ?? l?.value?.needName,
              e.fileName
            ).map((C) => ({
              objectName: C.path,
              fileName: C.fileName
            }));
            await w.value.trigger(o), g("downloadCallback", o);
          } else {
            const o = await (await J(
              L.value,
              t(x)
            )).downloadFile(
              u(
                e.needName ?? l?.value?.needName,
                e.fileName
              )
            );
            g("downloadCallback", o);
          }
        } finally {
          d.value = !1;
        }
      }
    };
    return (a, o) => (n(), T(t(G), j(t(q)(e, ["getOssAccess", "fileName", "needName"]), {
      disabled: r.disabled,
      onClick: o[0] || (o[0] = () => (c.value ? S : N)())
    }), {
      default: i(() => [
        b("div", {
          class: V(t(F))
        }, [
          d.value ? (n(), m("span", X, [
            s(t(R), { spin: !0 })
          ])) : f("", !0),
          t(_).icon ? (n(), m("span", Y, [
            D(a.$slots, "icon")
          ])) : f("", !0),
          s(t(h), null, M({
            default: i(() => [
              b("span", Z, k(r.text), 1)
            ]),
            _: 2
          }, [
            c.value ? {
              name: "title",
              fn: i(() => [
                U(k(t(v)("ap.apAppendix.previewFile")), 1)
              ]),
              key: "0"
            } : void 0
          ]), 1024),
          c.value ? (n(), m("span", {
            key: 2,
            class: "preview-icon",
            onClick: E(N, ["stop"])
          }, [
            s(t(h), {
              title: t(v)("ap.apAppendix.downloadFile")
            }, {
              default: i(() => [
                s(t(W))
              ]),
              _: 1
            }, 8, ["title"])
          ])) : f("", !0)
        ], 2)
      ]),
      _: 3
    }, 16, ["disabled"]));
  }
});
export {
  me as default
};
