import { Fragment } from 'react';
import type { FlooringPiece, SkirtingPiece } from '../lib/types';
import { cutMm, degree } from '../lib/format';
import { skirtingEndLabel } from '../lib/cut-list';
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], {
length: piece.blankLength,
width: piece.blankWidth,
});
const Wrapper = compact ? 'span' : 'figure';
return (
{!compact && (
{cutMm(piece.length, piece.blankLength)} × {cutMm(piece.width, piece.blankWidth)} 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, piece.installedTemplate);
const dimensions = compact
? undefined
: dimensionedOutline(
[template[0], template[1], { x: piece.length, y: thickness }, { x: 0, y: thickness }],
[0, 2],
{ length: piece.blankLength, width: thickness },
);
// 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,
type: piece.leftType,
installedBack: piece.installedTemplate?.[0],
installedFront: piece.installedTemplate?.[3],
},
{
side: 'right',
back: template[1],
front: template[2],
box: boxes.right,
type: piece.rightType,
installedBack: piece.installedTemplate?.[1],
installedFront: piece.installedTemplate?.[2],
},
] as const;
return (
{!compact && (
{cutMm(piece.length, piece.blankLength)} × {cutMm(thickness, thickness)} mm · wall side
above
{(piece.leftType === 'cope' || piece.rightType === 'cope') &&
' · preparation before coping'}
)}
{ends.map((end, index) => (
{!compact && (
{index === 0 ? 'Left' : 'Right'} · {skirtingEndLabel(piece, end.side)}
)}
{index === 0 && (
//
)}
))}
);
}