'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-acbe72ca.js');
const pdf = require('./pdf-5938706d.js');
require('./buffer-es6-6e2f3cca.js');

const akordPdfViewerSpaCss = "akord-pdf-viewer-spa{--cursor-dark:url(\"/assets/dark_zoom.svg\"), zoom-in;--cursor-light:url(\"/assets/light_zoom.svg\"), zoom-in}akord-pdf-viewer-spa .dark-mode{cursor:var(--cursor-dark)}akord-pdf-viewer-spa .light-mode{cursor:var(--cursor-light)}akord-pdf-viewer-spa canvas{margin:auto;display:block;width:100%;height:100%}";

pdf.pdf.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.5.207/pdf.worker.min.js';
let AkordPdfViewerSpa = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.pageRendered = index.createEvent(this, "pageRendered", 7);
    this.pageChange = index.createEvent(this, "pageChange", 7);
    this.pageNumPending = null;
    this.scale = 0.9; // hardcoded to scale the document to full canvas size
    this.pageNum = 1; // hardcoded to start at the page 1
    /**
     * Rotate the PDF in degrees
     * {number}
     */
    this.rotation = 0;
    this.darkMode = false;
  }
  /**
   * Listen for changes to src
   * @param newValue
   * @param oldValue
   */
  doSrc(newValue, oldValue) {
    if (newValue === oldValue) {
      return;
    }
    this.loadAndRender(newValue);
  }
  /**
   * Page forward
   * {MouseEvent} e
   */
  async pageNext(e) {
    if (e) {
      e.preventDefault();
    }
    if (this.pageNum >= this.pdfDocument.numPages) {
      return;
    }
    this.pageNum += 1;
    this.queueRenderPage(this.pageNum);
  }
  /**
   * Page backward
   * e
   */
  async pagePrev(e) {
    if (e) {
      e.preventDefault();
    }
    if (this.pageNum <= 1) {
      return;
    }
    this.pageNum -= 1;
    this.queueRenderPage(this.pageNum);
  }
  /**
   * Render the page based on pageNumber
   * {number} pageNumber
   */
  renderPage(pageNumber) {
    this.isPageRendering = true;
    this.pdfDocument
      .getPage(pageNumber)
      .then((page) => {
      const viewport = page.getViewport({ scale: this.scale, rotation: this.rotation });
      this.canvas.height = viewport.height;
      this.canvas.width = viewport.width;
      // Render PDF page into canvas context
      const renderContext = {
        viewport,
        canvasContext: this.ctx
      };
      // Render page method
      const renderTask = page.render(renderContext);
      // Wait for rendering to finish
      renderTask.promise
        .then(() => {
        this.isPageRendering = false;
        this.pageRendered.emit(this.pageNum);
        if (this.pageNumPending !== null) {
          this.renderPage(this.pageNumPending); // New page rendering is pending
          this.pageChange.emit(this.pageNumPending); // emit
          this.pageNumPending = null;
        }
      });
    });
  }
  queueRenderPage(pageNumber) {
    if (this.isPageRendering) {
      this.pageNumPending = pageNumber;
    }
    else {
      this.renderPage(pageNumber);
    }
  }
  componentDidLoad() {
    this.ctx = this.canvas.getContext('2d');
    if (this.src) {
      this.loadAndRender(this.src);
    }
  }
  loadAndRender(src) {
    pdf.pdf
      .getDocument(src).promise
      .then((pdfDocument) => {
      this.pdfDocument = pdfDocument;
      this.renderPage(this.pageNum);
    });
  }
  render() {
    return (index.h("div", { class: this.containerClass, style: { height: `${this.height}px` } }, index.h("button", { onClick: this.pagePrev.bind(this) }, "Prev"), index.h("button", { onClick: this.pageNext.bind(this) }, "Next"), index.h("canvas", { ref: e => this.canvas = e, class: this.darkMode ? 'dark-mode' : 'light-mode' })));
  }
  get component() { return index.getElement(this); }
  static get watchers() { return {
    "src": ["doSrc"]
  }; }
};
AkordPdfViewerSpa.style = akordPdfViewerSpaCss;

exports.akord_pdf_viewer_spa = AkordPdfViewerSpa;
