import { defineComponent as E, ref as w, computed as c, watch as L, openBlock as r, createElementBlock as m, normalizeClass as C, unref as l, createBlock as i, Fragment as p, resolveDynamicComponent as x, h as B, createCommentVNode as R } from "vue";
import { Spin as W } from "@aplus-frontend/antdv";
import { createOssInstance as I } from "@aplus-frontend/oss";
import "../../config-provider/index.mjs";
import "./components/error-result/index.vue.mjs";
import $ from "./style/index.mjs";
import { defaultParserPlugins as j } from "./utils/default-parser-plugin.mjs";
import { useGlobalConfig as _ } from "../../config-provider/hooks/use-global-config.mjs";
import { useNamespace as D } from "../../config-provider/hooks/use-namespace.mjs";
import { useLocale as G } from "../../config-provider/hooks/use-locale.mjs";
import b from "./components/error-result/index.vue2.mjs";
const ee = /* @__PURE__ */ E({
  __name: "ap-file-preview",
  props: {
    src: {},
    fileName: {},
    format: {},
    parserPlugin: {},
    getOssAccess: { type: Function }
  },
  setup(k) {
    const e = k, P = _("apUpload"), N = _("apFilePreview"), { b: f } = D("ap-file-preview"), O = $("ap-file-preview"), { lang: S } = G(), U = {
      "zh-cn": "zh_CN",
      en: "en_US"
    }, t = w(), o = w(!1), v = c(
      () => e.getOssAccess || P.value?.getOssAccess
    ), s = c(() => e?.format ?? e?.src?.split(".").pop()), A = c(() => [
      ...j,
      ...N.value?.parserPlugin ?? [],
      ...e?.parserPlugin ?? []
    ]), g = c(() => s.value ? A.value?.findLast(
      (a) => s.value && a.format.includes(s.value)
    ) : void 0), F = async () => {
      try {
        if (o.value = !0, t.value = void 0, !e?.src)
          return;
        if (e.src?.startsWith("http") || e.src?.startsWith("data:") || e.src?.startsWith("blob:")) {
          t.value = e.src;
          return;
        }
        const a = e.src?.match(/^([^:]+):(.*)$/), u = a?.[1], h = a?.[2] ?? e.src, d = I();
        await d.initOssClient({
          getOssAccess: () => u ? v.value?.(u) : v.value?.(),
          locale: U[S.value] ?? "zh_CN",
          onFailure: (n) => {
            throw new Error(
              typeof n?.message == "string" ? n.message : "oss create error"
            );
          }
        });
        const y = await d.getSignatureUrl(
          h,
          3600,
          e?.fileName ?? h
        );
        if (y) {
          const n = await fetch(y).then((z) => z.blob());
          t.value = URL.createObjectURL(n);
        } else
          throw new Error("oss getSignatureUrl error");
      } finally {
        o.value = !1;
      }
    };
    return L(
      () => [e.src],
      () => {
        F();
      },
      {
        immediate: !0
      }
    ), (a, u) => (r(), m("div", {
      class: C([l(f)(), l(O)])
    }, [
      o.value ? (r(), i(l(W), {
        key: 0,
        class: C([l(f)("spin")])
      }, null, 8, ["class"])) : !o.value && t.value && e.src ? (r(), m(p, { key: 1 }, [
        g.value && s.value ? (r(), m(p, { key: 0 }, [
          t.value ? (r(), i(x(
            B(p, [
              g.value?.parser?.({
                src: t.value,
                originalSrc: e.src,
                format: s.value,
                fileName: e.fileName
              })
            ])
          ), { key: 0 })) : R("", !0)
        ], 64)) : (r(), i(b, {
          key: 1,
          type: "notPreview"
        }))
      ], 64)) : (r(), i(b, {
        key: 2,
        type: "notExist"
      }))
    ], 2));
  }
});
export {
  ee as default
};
