import { useId } from 'react';
import { useTranslation } from 'react-i18next';
import type { LabelPageSize, LabelSheetLayout } from '../api/client';
import {
MAX_COLS,
MAX_ROWS,
PAGE_SIZES_MM,
formatMm,
parseDraft,
type LayoutDraft,
type LayoutNumberField,
type LayoutProblem,
} from '../utils/labelSheetLayout';
// The form for a custom label sheet, and a to-scale drawing of it (#3296).
const NUMBER_INPUTS: { field: LayoutNumberField; i18nKey: string; fallback: string; unit: boolean }[] = [
{ field: 'label_w_mm', i18nKey: 'labelWidth', fallback: 'Label width', unit: true },
{ field: 'label_h_mm', i18nKey: 'labelHeight', fallback: 'Label height', unit: true },
{ field: 'cols', i18nKey: 'columns', fallback: 'Columns', unit: false },
{ field: 'rows', i18nKey: 'rows', fallback: 'Rows', unit: false },
{ field: 'top_mm', i18nKey: 'topMargin', fallback: 'Top margin', unit: true },
{ field: 'left_mm', i18nKey: 'leftMargin', fallback: 'Left margin', unit: true },
{ field: 'col_gap_mm', i18nKey: 'columnGap', fallback: 'Gap between columns', unit: true },
{ field: 'row_gap_mm', i18nKey: 'rowGap', fallback: 'Gap between rows', unit: true },
];
/** Every label of ``layout`` drawn on its page. Labels that run off the page
* are red; the positions a starting position leaves blank are faded. */
export function LabelSheetDiagram({
layout,
startingPosition = 1,
}: {
layout: LabelSheetLayout;
startingPosition?: number;
}) {
const { t } = useTranslation();
const [pageW, pageH] = PAGE_SIZES_MM[layout.page];
const cells = [];
for (let row = 0; row < layout.rows; row++) {
for (let col = 0; col < layout.cols; col++) {
const x = layout.left_mm + col * (layout.label_w_mm + layout.col_gap_mm);
const y = layout.top_mm + row * (layout.label_h_mm + layout.row_gap_mm);
const offPage = x + layout.label_w_mm > pageW + 0.01 || y + layout.label_h_mm > pageH + 0.01;
const skipped = row * layout.cols + col < startingPosition - 1;
cells.push(