import { PageViewport } from 'pdfjs-dist/legacy/build/pdf'

/**
 * @param {HTMLDivElement} div
 * @param {PageViewport} viewport
 * @param {boolean} mustFlip
 * @param {boolean} mustRotate
 */
function setLayerDimensions(
  div,
  viewport,
  mustFlip = false,
  mustRotate = true
) {
  if (viewport instanceof PageViewport) {
    const { pageWidth, pageHeight } = viewport.rawDims;
    const { style } = div;

    // TODO: Investigate if it could be interesting to use the css round
    // function (https://developer.mozilla.org/en-US/docs/Web/CSS/round):
    // const widthStr =
    //   `round(down, var(--scale-factor) * ${pageWidth}px, 1px)`;
    // const heightStr =
    //   `round(down, var(--scale-factor) * ${pageHeight}px, 1px)`;
    const widthStr = `calc(var(--scale-factor) * ${pageWidth}px)`;
    const heightStr = `calc(var(--scale-factor) * ${pageHeight}px)`;

    if (!mustFlip || viewport.rotation % 180 === 0) {
      style.width = widthStr;
      style.height = heightStr;
    } else {
      style.width = heightStr;
      style.height = widthStr;
    }
  }

  if (mustRotate) {
    div.setAttribute("data-main-rotation", viewport.rotation);
  }
}

export {
  setLayerDimensions
}