import type { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api'
import type { PDFSrc } from '../types'

function toDocumentInitParameters(source: PDFSrc): DocumentInitParameters {
  if (typeof source === 'string' || source instanceof URL) {
    return { url: source }
  }

  if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) {
    return { data: source }
  }

  return source as DocumentInitParameters
}

async function createIframe(): Promise<HTMLIFrameElement> {
  return new Promise((resolve, reject) => {
    const iframe = document.createElement('iframe')

    iframe.width = '0px'
    iframe.height = '0px'
    iframe.style.cssText = 'position: absolute; top:0; left:0'
    iframe.style.display = 'none'

    iframe.onload = function () {
      resolve(iframe)
    }
    document.body.appendChild(iframe)
  })
}

function addStylesToIframe(content: Window, sizeX: number, sizeY: number) {
  const style = content.document.createElement('style')
  style.textContent = `
    @page {
      margin: 0;
      size: ${sizeX}pt ${sizeY}pt;
    }
    body {
      margin: 0;
      width: 100%;
    }
    canvas {
      page-break-after: always;
      page-break-before: avoid;
      page-break-inside: avoid;
    }
  `
  content.document.head.appendChild(style)
}

export {
  addStylesToIframe,
  createIframe,
  toDocumentInitParameters,
}
