// Frontal-Ansicht (Elevation, SVG) eines Fensters/einer Tür: Rahmen, Flügel/
// Pfosten (mit Öffnungslinien je Flügel), Verglasungshinweis und Rollladen-
// kasten. Rein aus den Modellwerten gezeichnet — kein Renderer-Roundtrip,
// deterministisch. Ursprünglich Teil von `OpeningEditor` (dort
// mit einer echten Öffnungs-Instanz gefüttert); hierher extrahiert, damit
// auch die Typ-Bibliothek (ResourceManager, ohne konkrete Instanz — s.
// `WindowStylesTab`/`DoorStylesTab`) dieselbe Vorschau zeigen kann. Braucht
// dafür nur `opening.width`/`opening.height`, der Rest kommt aus `wt`/`dt`.
//
// Bezeichner englisch, Kommentare/UI-Text deutsch (CONVENTIONS.md).
import { useMemo } from "react";
import { t } from "../i18n";
import type { DoorType, Opening, WindowType } from "../model/types";
import { sashesOfWindowType, glazingPanesOf } from "../model/types";
export function ElevationPreview({
opening,
wt,
dt,
}: {
opening: Opening;
wt?: WindowType;
dt?: DoorType;
}) {
const geom = useMemo(() => {
const w = Math.max(0.1, opening.width);
const h = Math.max(0.1, opening.height);
const pad = 24;
const boxH = (!dt && wt?.shading?.create ? wt.shading.box.height : 0) || 0;
// Zeichenfläche auf ~260×300 skalieren, Seitenverhältnis erhalten.
const availW = 220;
const availH = 260;
const totalH = h + boxH;
const scale = Math.min(availW / w, availH / totalH);
const px = (m: number) => m * scale;
return { w, h, boxH, pad, scale, px, totalH };
}, [opening.width, opening.height, wt, dt]);
const { w, h, boxH, pad, px } = geom;
const svgW = px(w) + pad * 2;
const svgH = px(h + boxH) + pad * 2;
const frameW = px((!dt ? wt?.frameWidth : dt?.frameWidth) ?? 0.06);
const originY = pad + px(boxH);
// Flügel-/Pfostenaufteilung: gleichmässig (autoWidth) bzw. feste Breiten.
const sashes = !dt && wt ? sashesOfWindowType(wt) : [];
const fixedTotal = sashes.reduce(
(a, s) => a + (s.kind === "pfosten" ? s.postWidth ?? 0.06 : s.autoWidth ? 0 : s.width ?? 0),
0,
);
const autoCount = sashes.filter((s) => s.kind === "fluegel" && (s.autoWidth ?? true)).length;
const autoW = autoCount > 0 ? Math.max(0.05, (w - fixedTotal) / autoCount) : 0;
const panes = !dt && wt ? glazingPanesOf(wt) : 1;
// Segmentbreiten (Meter) je Sash.
const segW = sashes.map((s) =>
s.kind === "pfosten"
? s.postWidth ?? 0.06
: s.autoWidth ?? true
? autoW
: s.width ?? autoW,
);
const strokeMain = "var(--ink)";
const strokeMuted = "var(--muted)";
const glass = "rgba(120,180,220,0.18)";
let cursor = 0;
const segEls: JSX.Element[] = [];
sashes.forEach((s, i) => {
const x0 = pad + px(cursor);
const wSeg = px(segW[i]);
cursor += segW[i];
if (s.kind === "pfosten") {
segEls.push(
,
);
return;
}
// Flügelrahmen + Glas + Öffnungslinien.
const gx = x0 + frameW * 0.4;
const gy = originY + frameW * 0.4;
const gw = Math.max(1, wSeg - frameW * 0.8);
const gh = Math.max(1, px(h) - frameW * 0.8);
segEls.push(
{/* Verglasung: zusätzliche parallele Glaslinien = Scheibenzahl − 1. */}
{Array.from({ length: Math.max(0, panes - 1) }, (_, k) => (
))}
{/* Öffnungslinien (Dreieck) je nach Anschlag; nicht bei fest. */}
{s.opening !== "fest" &&
(s.hingeSide === "right" ? (
) : (
))}
,
);
});
return (
);
}