import { LitElement, css, unsafeCSS } from "lit";
import { property } from "lit/decorators.js";
import { IPdfDocument } from "../../../../../src/api/pdf-viewer-manager/pdf-viewer-manager";
import { createUrlFromBase64 } from "../utils";
import styles from "./styles.css?inline";
import { template } from "./template";

export class PdfSelector extends LitElement {
  render() {
    return template(this);
  }

  static styles = css`
    ${unsafeCSS(styles)}
  `;

  @property({ type: Array })
  pdfList: IPdfDocument[];

  @property({ type: Array })
  activePdfs: IPdfDocument[] = [];

  removePdf(id: string) {
    this.dispatchEvent(
      new CustomEvent("pdf-removed", { detail: id, bubbles: true, composed: true }),
    );
  }

  setActivePdf(id: string) {
    this.dispatchEvent(
      new CustomEvent("active-pdf-changed", { detail: id, bubbles: true, composed: true }),
    );
  }

  openInNewWindow(pdf: IPdfDocument) {
    const url = pdf.data.url ? pdf.data.url : createUrlFromBase64(pdf.data.b64 as string);
    url && window.open(url);
  }
}
