import { Fragment } from 'react';
import type { FlooringPiece, SkirtingPiece } from '../lib/types';
import { cutMm, degree } from '../lib/format';
import {
dimensionedOutline,
flooringTemplate,
fullPreviewBox,
skirtingEndBoxes,
skirtingTemplate,
viewBox,
} from '../lib/cut-preview';
import type { EdgeDimension } from '../lib/cut-preview';
function Dimensions({ dimensions, font }: { dimensions: EdgeDimension[]; font: number }) {
return (
{dimensions.map((dimension) => {
const { start, end, lineStart, lineEnd, normal, label } = dimension;
const center = { x: (lineStart.x + lineEnd.x) / 2, y: (lineStart.y + lineEnd.y) / 2 };
const staggered =
Math.abs((label.x - center.x) * -normal.y + (label.y - center.y) * normal.x) > font * 0.1;
return (
{[
[start, lineStart],
[end, lineEnd],
].map(([point, projected], i) => (
))}
{[lineStart, lineEnd].map((point, i) => (
))}
{staggered && (
)}
);
})}
{dimensions.map((dimension) => (
{dimension.angle === undefined ? (
dimension.text
) : (
<>
{dimension.text}
{' '}
{degree(dimension.angle)}
>
)}
))}
);
}
export function FlooringPreview({
piece,
compact = false,
}: {
piece: FlooringPiece;
compact?: boolean;
}) {
const template = flooringTemplate(piece);
const dimensions = compact
? undefined
: dimensionedOutline(template, piece.shaped ? undefined : [0, 1]);
const Wrapper = compact ? 'span' : 'figure';
return (
{!compact && (
{cutMm(piece.length)} × {cutMm(piece.width)} mm
{dimensions?.dimensions.some((dimension) => dimension.angle !== undefined) &&
' · angles from horizontal'}
)}
);
}
export function SkirtingPreview({
piece,
thickness,
compact = false,
}: {
piece: SkirtingPiece;
thickness: number;
compact?: boolean;
}) {
const template = skirtingTemplate(piece, thickness);
const Wrapper = compact ? 'span' : 'div';
const EndWrapper = compact ? 'span' : 'figure';
const Grid = compact ? 'span' : 'div';
if (template.length !== 4) return Shape template unavailable;
const boxes = skirtingEndBoxes(template, thickness);
const dimensions = compact
? undefined
: dimensionedOutline(
[template[0], template[1], { x: piece.length, y: thickness }, { x: 0, y: thickness }],
[0, 2],
);
// The lower dimension measures the overall blank, between its actual longest points.
const overall = dimensions?.dimensions.find((dimension) => dimension.edge === 2);
if (overall) {
overall.start = template.reduce((a, b) => (a.x > b.x ? a : b));
overall.end = template.reduce((a, b) => (a.x < b.x ? a : b));
}
const points = template.map((point) => `${point.x},${point.y}`).join(' ');
const ends = [
{ side: 'left', back: template[0], front: template[3], box: boxes.left },
{ side: 'right', back: template[1], front: template[2], box: boxes.right },
] as const;
return (
{!compact && (
{cutMm(piece.length)} × {cutMm(thickness)} mm · wall side above
)}
{ends.map((end, index) => (
{!compact && (
{index === 0 ? 'Left' : 'Right'} ·{' '}
{degree(index === 0 ? piece.leftMitre : piece.rightMitre)}{' '}
{index === 0 ? piece.leftType : piece.rightType}
)}
{index === 0 && (
//
)}
))}
);
}