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