import { defineComponent as $, ref as h, shallowRef as y, onMounted as I, nextTick as L, onBeforeUnmount as M, createElementBlock as b, openBlock as g, computed as T, createElementVNode as x, renderSlot as C, createBlock as k, createCommentVNode as _ } from "vue";
import { log as E } from "../index.js";
import { TextLayer as R } from "pdfjs-dist";
const B = /* @__PURE__ */ $({
  __name: "textLayer",
  props: {
    page: {},
    pageInfo: {}
  },
  setup(m) {
    const e = m, n = h(), s = y(), t = h(), i = async () => {
      if (n.value) {
        s.value && s.value.cancel();
        const { viewport: l } = e.pageInfo, r = await e.page.getTextContent({
          includeMarkedContent: !0,
          disableNormalization: !0
        });
        s.value = new R({
          textContentSource: r,
          container: n.value,
          viewport: l.clone({ dontFlip: !0 })
        }), await s.value.render(), p(l), t.value = document.createElement("div"), t.value.classList.add("endOfContent"), n.value.appendChild(t.value);
      }
    }, p = (l) => {
      const { scale: r, rotation: d, width: f, height: c } = l;
      let v = l.width / l.height, w = "", u = n.value.style;
      u.setProperty("--scale-factor", `${r}`), u.setProperty("--total-scale-factor", `${r}`), Math.abs(d) == 90 && (w += `translateY(${100 / v}%)`), Math.abs(d) == 180 && (w += "translate(100%, 100%)"), Math.abs(d) == 270 && (w += `translateX(${100 * v}%)`), Math.abs(d) == 0 || Math.abs(d) == 180 ? (u.setProperty("width", `${f}px`), u.setProperty("height", `${c}px`)) : (Math.abs(d) == 90 || Math.abs(d) == 270) && (u.setProperty("width", `${c}px`), u.setProperty("height", `${f}px`)), w += `rotate(${d}deg)`, [
        "webkitTransform",
        "mozTransform",
        "msTransform",
        "oTransform",
        "transform"
      ].forEach((P) => {
        u.hasOwnProperty(P) && Object.assign(u, { [P]: w });
      });
    }, a = () => {
      t.value.style.width = n.value?.style.width, t.value.style.height = n.value?.style.height, n.value?.classList.add("selecting");
    }, o = () => {
      t.value.style.width = "", t.value.style.height = "", n.value?.classList.remove("selecting");
    };
    return I(() => {
      L(() => {
        i();
      });
    }), M(() => {
      s.value?.cancel();
    }), (l, r) => (g(), b("div", {
      ref_key: "_textLayer",
      ref: n,
      class: "vpdf:textLayer",
      onMousedown: a,
      onMouseup: o
    }, null, 544));
  }
}), S = ["width", "height"], O = /* @__PURE__ */ $({
  __name: "canvas",
  props: {
    pdf: {},
    pageInfo: {}
  },
  emits: ["render", "error"],
  setup(m, { emit: e }) {
    const n = m, s = e, t = h(!1), i = h(), p = T(
      () => i.value?.getContext("2d", {
        alpha: !1,
        willReadFrequently: !0
      })
    ), a = y(), o = y(), l = async () => {
      if (i.value && p.value) {
        t.value = !0;
        const { viewport: r, page: d } = n.pageInfo, { promise: f } = n.pdf, c = await f;
        try {
          p.value.fillStyle = "white", p.value.fillRect(0, 0, r.width, r.height), o.value = await c.getPage(d);
          const v = {
            canvasContext: p.value,
            canvas: i.value,
            viewport: r
          };
          a.value = o.value.render(v), await a.value.promise, i.value && p.value && s("render", {
            page: o.value,
            canvas: i.value,
            context: p.value,
            viewport: r,
            doc: c
          });
        } catch (v) {
          s("error", v), v.name != "RenderingCancelledException";
        } finally {
          t.value = !1, o.value?.cleanup(!0);
        }
      }
    };
    return I(async () => {
      await l();
    }), M(async () => {
      t.value && a.value?.cancel(), o.value?.cleanup();
    }), (r, d) => (g(), b("canvas", {
      ref_key: "canva",
      ref: i,
      class: "vpdf:box-border vpdf:h-full vpdf:w-full vpdf:border vpdf:border-background-300 vpdf:bg-white vpdf:outline-none",
      width: r.pageInfo.viewport.width,
      height: r.pageInfo.viewport.height
    }, null, 8, S));
  }
}), F = { class: "vpdf:leading-none" }, N = ["width", "height"], V = {
  key: 2,
  class: "vpdf:pointer-events-none vpdf:absolute vpdf:inset-0 vpdf:flex vpdf:items-center vpdf:bg-foreground/5"
}, q = /* @__PURE__ */ $({
  __name: "vPdfPageRenderer",
  props: {
    pdf: {},
    pageInfo: {},
    page: {},
    scale: { default: 1 },
    rotation: { default: 0 },
    textLayer: { type: Boolean, default: !1 },
    render: { type: Boolean, default: !0 },
    onRender: { type: Function, default: () => {
    } },
    onError: {},
    visible: { type: Boolean, default: !0 }
  },
  setup(m) {
    const e = m, n = h(!0), s = y(), t = h(
      Object.assign({}, e.pageInfo, {
        scale: e.scale,
        rotation: e.rotation
      })
    ), i = async (a) => {
      const { docId: o, promise: l } = e.pdf, f = (await (await l).getPage(a)).getViewport({
        scale: e.scale,
        rotation: e.rotation
      }), c = {
        top: 0,
        bottom: f.height,
        left: 0,
        right: f.width
      };
      return {
        id: `${o}_pdf_page_${a}_${e.scale}_${e.rotation}`,
        page: a,
        scale: e.scale,
        rotation: e.rotation,
        viewport: f,
        pos: { x: 0, y: 0 },
        bounds: {
          inner: c,
          outer: c
        }
      };
    }, p = (a) => {
      e.onRender?.(a), s.value = a.page, n.value = !1;
    };
    return I(async () => {
      !e.pageInfo && !e.page ? E("Page not found!") : !e.pageInfo && e.page && (t.value = await i(e.page));
    }), (a, o) => (g(), b("div", F, [
      x("div", {
        class: "vpdf:relative vpdf:h-full vpdf:w-full vpdf:bg-white vpdf:leading-none",
        width: t.value?.viewport.width,
        height: t.value?.viewport.height
      }, [
        C(a.$slots, "prepend"),
        t.value && a.visible ? (g(), k(O, {
          key: 0,
          pdf: a.pdf,
          pageInfo: t.value,
          onError: o[0] || (o[0] = (l) => e.onError?.(l)),
          onRender: p
        }, null, 8, ["pdf", "pageInfo"])) : _("", !0),
        t.value && a.textLayer && s.value ? (g(), k(B, {
          key: 1,
          page: s.value,
          pageInfo: t.value
        }, null, 8, ["page", "pageInfo"])) : _("", !0),
        n.value ? (g(), b("div", V, [...o[1] || (o[1] = [
          x("div", { class: "vpdf:mx-auto" }, [
            x("span", {
              class: "vpdf:realtive vpdf:block vpdf:h-[1em] vpdf:w-[1em] vpdf:animate-mltShdSpin vpdf:overflow-hidden vpdf:rounded-full vpdf:-indent-[9999em] vpdf:text-sm vpdf:leading-none vpdf:text-foreground",
              style: { transform: "translateZ(0)" }
            })
          ], -1)
        ])])) : _("", !0),
        C(a.$slots, "append")
      ], 8, N)
    ]));
  }
});
export {
  q as _
};
