/** A4 page sizing for SWI document editor, preview, and print (96 CSS px per inch). */
export const MM_TO_PX = 96 / 25.4;

export function mmToPx(mm) {
  return Math.round(mm * MM_TO_PX);
}

export const A4_WIDTH_MM = 210;
export const A4_HEIGHT_MM = 297;
export const A4_WIDTH_PX = mmToPx(A4_WIDTH_MM);
export const A4_HEIGHT_PX = mmToPx(A4_HEIGHT_MM);

/** Typical print margin (each edge) when using @page margin. */
export const A4_PRINT_MARGIN_MM = 10;

/**
 * Main content body height (materials + rows).
 * ~122mm — fits on one A4 sheet with header, footer, and margins.
 */
export const PAGE_CONTENT_HEIGHT_MM = 122;
export const PAGE_CONTENT_HEIGHT_PX = mmToPx(PAGE_CONTENT_HEIGHT_MM);
export const PAGE_VIEW_HEIGHT_PX = PAGE_CONTENT_HEIGHT_PX;

/** Minimum height for each content row band (text + pictures). Used in edit and print preview. */
export const MIN_ROW_BAND_HEIGHT_PX = 180;

/** @deprecated Use MIN_ROW_BAND_HEIGHT_PX */
export const ROW_PICTURE_EDIT_MIN_HEIGHT_PX = MIN_ROW_BAND_HEIGHT_PX;

export const DEFAULT_ROW_COUNT = 1;
export const ROW_BORDER_PX = 1;
export const MIN_ROW_HEIGHT_PX = 0;
export const DEFAULT_TEXT_BLOCK_HEIGHT = 0;

export const ROW_TEXT_CHROME_PX = 0;

export function defaultRowHeightForCount() {
  return MIN_ROW_HEIGHT_PX;
}

export function rowMinHeightPx(block) {
  const h = Number(block?.height_px);
  if (!Number.isFinite(h) || h < 0) return MIN_ROW_HEIGHT_PX;
  return h;
}
