'use client' import { type AnyNode, type AnyNodeId, DoorNode, useScene } from '@pascal-app/core' import { useViewer } from '@pascal-app/viewer' import { Copy, FlipHorizontal2, Move, Trash2, X } from 'lucide-react' import Image from 'next/image' import { useCallback } from 'react' import { sfxEmitter } from '@/lib/sfx-bus' import useEditor from '@/store/use-editor' import { NumberInput } from '@/components/ui/primitives/number-input' import { Switch } from '@/components/ui/primitives/switch' export function DoorPanel() { const selectedIds = useViewer((s) => s.selection.selectedIds) const setSelection = useViewer((s) => s.setSelection) const nodes = useScene((s) => s.nodes) const updateNode = useScene((s) => s.updateNode) const deleteNode = useScene((s) => s.deleteNode) const setMovingNode = useEditor((s) => s.setMovingNode) const selectedId = selectedIds[0] const node = selectedId ? (nodes[selectedId as AnyNode['id']] as DoorNode | undefined) : undefined const handleUpdate = useCallback( (updates: Partial) => { if (!selectedId) return updateNode(selectedId as AnyNode['id'], updates) useScene.getState().dirtyNodes.add(selectedId as AnyNodeId) }, [selectedId, updateNode], ) const handleClose = useCallback(() => { setSelection({ selectedIds: [] }) }, [setSelection]) const handleFlip = useCallback(() => { if (!node) return handleUpdate({ side: node.side === 'front' ? 'back' : 'front', rotation: [node.rotation[0], node.rotation[1] + Math.PI, node.rotation[2]], }) }, [node, handleUpdate]) const handleMove = useCallback(() => { if (!node) return sfxEmitter.emit('sfx:item-pick') setMovingNode(node) setSelection({ selectedIds: [] }) }, [node, setMovingNode, setSelection]) const handleDelete = useCallback(() => { if (!selectedId || !node) return sfxEmitter.emit('sfx:item-delete') deleteNode(selectedId as AnyNode['id']) if (node.parentId) useScene.getState().dirtyNodes.add(node.parentId as AnyNodeId) setSelection({ selectedIds: [] }) }, [selectedId, node, deleteNode, setSelection]) const handleDuplicate = useCallback(() => { if (!node || !node.parentId) return sfxEmitter.emit('sfx:item-pick') useScene.temporal.getState().pause() const duplicate = DoorNode.parse({ position: [...node.position] as [number, number, number], rotation: [...node.rotation] as [number, number, number], side: node.side, wallId: node.wallId, parentId: node.parentId, width: node.width, height: node.height, frameThickness: node.frameThickness, frameDepth: node.frameDepth, threshold: node.threshold, thresholdHeight: node.thresholdHeight, hingesSide: node.hingesSide, swingDirection: node.swingDirection, segments: node.segments.map(s => ({ ...s, columnRatios: [...s.columnRatios] })), handle: node.handle, handleHeight: node.handleHeight, handleSide: node.handleSide, doorCloser: node.doorCloser, panicBar: node.panicBar, panicBarHeight: node.panicBarHeight, metadata: { isNew: true }, }) useScene.getState().createNode(duplicate, node.parentId as AnyNodeId) setMovingNode(duplicate) setSelection({ selectedIds: [] }) }, [node, setMovingNode, setSelection]) const setSegmentHeightRatio = (segIdx: number, newVal: number) => { const numSegs = node!.segments.length const totalH = node!.segments.reduce((sum, s) => sum + s.heightRatio, 0) const normH = node!.segments.map(s => s.heightRatio / totalH) const clamped = Math.max(0.05, Math.min(0.95, newVal)) const neighborIdx = segIdx < numSegs - 1 ? segIdx + 1 : segIdx - 1 const delta = clamped - normH[segIdx]! const neighborVal = Math.max(0.05, normH[neighborIdx]! - delta) const newRatios = normH.map((v, i) => { if (i === segIdx) return clamped if (i === neighborIdx) return neighborVal return v }) const updated = node!.segments.map((s, idx) => ({ ...s, heightRatio: newRatios[idx]! })) handleUpdate({ segments: updated }) } const setSegmentColumnRatio = (segIdx: number, colIdx: number, newVal: number) => { const seg = node!.segments[segIdx]! const normRatios = (() => { const sum = seg.columnRatios.reduce((a, b) => a + b, 0) return seg.columnRatios.map(r => r / sum) })() const numCols = normRatios.length const clamped = Math.max(0.05, Math.min(0.95, newVal)) const neighborIdx = colIdx < numCols - 1 ? colIdx + 1 : colIdx - 1 const delta = clamped - normRatios[colIdx]! const neighborVal = Math.max(0.05, normRatios[neighborIdx]! - delta) const newRatios = normRatios.map((v, i) => { if (i === colIdx) return clamped if (i === neighborIdx) return neighborVal return v }) const updated = node!.segments.map((s, idx) => idx === segIdx ? { ...s, columnRatios: newRatios } : s, ) handleUpdate({ segments: updated }) } if (!node || node.type !== 'door' || selectedIds.length !== 1) return null const hSum = node.segments.reduce((s, seg) => s + seg.heightRatio, 0) const normHeights = node.segments.map(seg => seg.heightRatio / hSum) return (
{/* Header */}

{node.name || "Door"}

{/* Content */}
{/* Position */}
handleUpdate({ position: [v, node.position[1], node.position[2]] })} precision={2} />
{/* Dimensions */}
handleUpdate({ width: v })} min={0.5} precision={2} className="flex-1" /> m
handleUpdate({ height: v, position: [node.position[0], v / 2, node.position[2]] })} min={1.0} precision={2} className="flex-1" /> m
{/* Frame */}
handleUpdate({ frameThickness: v })} min={0.01} precision={3} step={0.01} className="flex-1" /> m
handleUpdate({ frameDepth: v })} min={0.01} precision={3} step={0.01} className="flex-1" /> m
{/* Content Padding */}
handleUpdate({ contentPadding: [v, node.contentPadding[1]] })} min={0} precision={3} step={0.005} className="flex-1" /> m
handleUpdate({ contentPadding: [node.contentPadding[0], v] })} min={0} precision={3} step={0.005} className="flex-1" /> m
{/* Swing */}
Hinges
{(['left', 'right'] as const).map((side) => ( ))}
Direction
{(['inward', 'outward'] as const).map((dir) => ( ))}
{/* Threshold */}
handleUpdate({ threshold: checked })} />
{node.threshold && (
handleUpdate({ thresholdHeight: v })} min={0.005} precision={3} step={0.005} className="flex-1" /> m
)}
{/* Handle */}
handleUpdate({ handle: checked })} />
{node.handle && (
handleUpdate({ handleHeight: v })} min={0.5} max={node.height - 0.1} precision={2} step={0.05} className="flex-1" /> m
Side
{(['left', 'right'] as const).map((side) => ( ))}
)}
{/* Hardware */}
Door Closer handleUpdate({ doorCloser: checked })} />
Panic Bar handleUpdate({ panicBar: checked })} />
{node.panicBar && (
handleUpdate({ panicBarHeight: v })} min={0.5} max={node.height - 0.1} precision={2} step={0.05} className="flex-1" /> m
)}
{/* Segments */}
{node.segments.map((seg, i) => { const numCols = seg.columnRatios.length const colSum = seg.columnRatios.reduce((a, b) => a + b, 0) const normCols = seg.columnRatios.map(r => r / colSum) return (
Segment {i + 1}
{(['panel', 'glass', 'empty'] as const).map((t) => ( ))}
setSegmentHeightRatio(i, v / 100)} min={5} max={95} precision={1} step={1} className="flex-1" /> %
{/* Columns */}
{ const n = Math.max(1, Math.min(8, Math.round(v))) const updated = node.segments.map((s, idx) => idx === i ? { ...s, columnRatios: Array(n).fill(1 / n) } : s, ) handleUpdate({ segments: updated }) }} min={1} max={8} precision={0} step={1} /> {numCols > 1 && (
{normCols.map((ratio, ci) => (
setSegmentColumnRatio(i, ci, v / 100)} min={5} max={95} precision={1} step={1} className="flex-1" /> %
))}
{ const updated = node.segments.map((s, idx) => idx === i ? { ...s, dividerThickness: v } : s, ) handleUpdate({ segments: updated }) }} min={0.005} precision={3} step={0.005} className="flex-1" /> m
)}
{seg.type === 'panel' && (
{ const updated = node.segments.map((s, idx) => idx === i ? { ...s, panelInset: v } : s, ) handleUpdate({ segments: updated }) }} min={0} precision={3} step={0.005} className="flex-1" /> m
{ const updated = node.segments.map((s, idx) => idx === i ? { ...s, panelDepth: v } : s, ) handleUpdate({ segments: updated }) }} min={0} precision={3} step={0.005} className="flex-1" /> m
)}
) })}
{node.segments.length > 1 && ( )}
{/* Action Buttons */}
) }