const w = (l, e) => new Promise((t, n) => {
  let r = e.querySelector(l);
  if (r) {
    t(r);
    return;
  }
  new MutationObserver((h, s) => {
    Array.from(e.querySelectorAll(l)).forEach((o) => {
      t(o), s.disconnect();
    });
  }).observe(e, {
    childList: !0,
    subtree: !0
  });
}), g = {
  trailing: !0
};
function b(l, e = 25, t = {}) {
  if (t = { ...g, ...t }, !Number.isFinite(e))
    throw new TypeError("Expected `wait` to be a finite number");
  let n, r, h = [], s, o;
  const d = (a, c) => (s = y(l, a, c), s.finally(() => {
    if (s = null, t.trailing && o && !r) {
      const u = d(a, o);
      return o = null, u;
    }
  }), s);
  return function(...a) {
    return s ? (t.trailing && (o = a), s) : new Promise((c) => {
      const u = !r && t.leading;
      clearTimeout(r), r = setTimeout(() => {
        r = null;
        const f = t.leading ? n : d(this, a);
        for (const p of h)
          p(f);
        h = [];
      }, e), u ? (n = d(this, a), c(n)) : h.push(c);
    });
  };
}
async function y(l, e, t) {
  return await l.apply(e, t);
}
const i = {
  viewerPath: "/pdfjs",
  viewerEntry: "/web/viewer.html",
  src: "",
  page: "",
  search: "",
  phrase: "",
  zoom: "auto",
  pagemode: "none",
  locale: "",
  textLayer: ""
};
class m extends HTMLElement {
  constructor() {
    super(), this.debouncedRenderIframe = b(async () => {
      await w("iframe", this.shadowRoot), this.renderViewer(this.getIframeSrc());
    }, 0, { leading: !0 });
    const e = this.attachShadow({ mode: "open" }), t = document.createElement("template");
    t.innerHTML = `
      <iframe frameborder="0" width="100%"></iframe>
      <style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style>
    `, e.appendChild(t.content.cloneNode(!0));
  }
  static get observedAttributes() {
    return ["src", "viewer-path", "locale", "page", "search", "phrase", "zoom", "pagemode", "text-layer"];
  }
  connectedCallback() {
    this.iframe = this.shadowRoot.querySelector("iframe"), this.setEventListeners();
  }
  attributeChangedCallback() {
    this.debouncedRenderIframe();
  }
  getIframeSrc() {
    const e = this.getFullPath(this.getAttribute("src") || i.src), t = this.getFullPath(this.getAttribute("viewer-path") || i.viewerPath), n = this.getAttribute("page") || i.page, r = this.getAttribute("search") || i.search, h = this.getAttribute("phrase") || i.phrase, s = this.getAttribute("zoom") || i.zoom, o = this.getAttribute("pagemode") || i.pagemode, d = this.getAttribute("locale") || i.locale, a = this.getAttribute("text-layer") || i.textLayer, c = `${t}${i.viewerEntry}?file=${encodeURIComponent(e)}#page=${n}&zoom=${s}&pagemode=${o}&search=${r}&phrase=${h}&textLayer=${a}${d ? "&locale=" + d : ""}`;
    return c !== this.iframe.getAttribute("src") ? c : "";
  }
  renderViewer(e) {
    !e || !this.iframe || (this.shadowRoot.replaceChild(this.iframe.cloneNode(), this.iframe), this.iframe = this.shadowRoot.querySelector("iframe"), this.iframe.src = e);
  }
  setEventListeners() {
    document.addEventListener("webviewerloaded", () => {
      var e, t, n, r;
      this.getAttribute("src") !== i.src && ((e = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || e.set("defaultUrl", "")), (t = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || t.set("disablePreferences", !0), (n = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || n.set("pdfBugEnabled", !0), (r = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || r.set("eventBusDispatchToDOM", !0);
    });
  }
  getFullPath(e) {
    return e.startsWith("/") ? `${window.location.origin}${e}` : e;
  }
}
window.customElements.get("pdfjs-viewer-element") || (window.PdfjsViewerElement = m, window.customElements.define("pdfjs-viewer-element", m));
export {
  m as PdfjsViewerElement,
  m as default
};
