import {
  BASE_PDF_PAGE_WIDTH,
  FLOATING_ZOOM_STEP,
  FULLSCREEN_ZOOM_STEP,
  MAX_FLOATING_ZOOM,
  MAX_FULLSCREEN_ZOOM,
  MIN_FLOATING_ZOOM,
  MIN_FULLSCREEN_ZOOM,
  ROTATION_STEP,
  ROTATION_TOTAL,
  ZOOM_SCALES,
} from './constants';

export const getScale = (zoom?: number) => {
  if (!zoom) return 1;

  return ZOOM_SCALES[zoom] || 1;
};

export const getThreshold = (zoom: number) => {
  if (zoom >= 2.5) {
    return 0.2;
  }
  if (zoom >= 1.5) {
    return 0.3;
  }
  if (zoom >= 1) {
    return 0.5;
  }
};

export const isSecureFile = (fileUrl: string) => {
  if (!fileUrl) return false;
  return fileUrl.startsWith('https://secure-files');
};

export const getFile = (fileUrl: string) => {
  if (!fileUrl) return null;

  if (isSecureFile(fileUrl)) {
    return {
      url: fileUrl,
      withCredentials: true,
      crossOrigin: 'use-credentials',
    };
  }

  return fileUrl;
};

export const getPageWidth = (
  zoom: number,
  containerWidth: number,
  baseWidth: number = BASE_PDF_PAGE_WIDTH,
) => {
  const zoomedWidth = baseWidth * zoom;
  return containerWidth ? Math.min(containerWidth, zoomedWidth) : zoomedWidth;
};

export const rotate = (rotation: number) => {
  return (rotation + ROTATION_STEP) % ROTATION_TOTAL;
};

export const getZoomPercentage = (zoom: number) => {
  return `${Math.round(zoom * 100)}%`;
};

export const getZoomValues = (variant: string) => {
  if (variant === 'floating') {
    return {
      zoomStep: FLOATING_ZOOM_STEP,
      maxZoom: MAX_FLOATING_ZOOM,
      minZoom: MIN_FLOATING_ZOOM,
    };
  }

  return {
    zoomStep: FULLSCREEN_ZOOM_STEP,
    maxZoom: MAX_FULLSCREEN_ZOOM,
    minZoom: MIN_FULLSCREEN_ZOOM,
  };
};
