export type PageFormatId = "a4" | "letter" | "legal" | "a5" | "tabloid"

export type PageFormat = {
  id: PageFormatId
  label: string
  widthMm: number
  heightMm: number
}

/** 96 CSS px per inch, 25.4 mm per inch */
export const MM_TO_PX = 96 / 25.4

export const PAGE_FORMATS: PageFormat[] = [
  { id: "a4", label: "A4", widthMm: 210, heightMm: 297 },
  { id: "letter", label: "Letter", widthMm: 216, heightMm: 279 },
  { id: "legal", label: "Legal", widthMm: 216, heightMm: 356 },
  { id: "a5", label: "A5", widthMm: 148, heightMm: 210 },
  { id: "tabloid", label: "Tabloid", widthMm: 279, heightMm: 432 },
]

export const DEFAULT_PAGE_FORMAT_ID: PageFormatId = "a4"

export function getPageFormat(id: PageFormatId): PageFormat {
  return PAGE_FORMATS.find((f) => f.id === id) ?? PAGE_FORMATS[0]!
}

export function pageFormatWidthPx(format: PageFormat): number {
  return Math.round(format.widthMm * MM_TO_PX)
}

export function pageFormatHeightPx(format: PageFormat): number {
  return Math.round(format.heightMm * MM_TO_PX)
}

/** Approximate print margins (1 inch) */
export const PAGE_MARGIN_PX = Math.round(25.4 * MM_TO_PX)
