import { bounds, pointInPolygon, uid, type Building, type Floor, type Vec2 } from "../../../model.ts"; import { isOutdoorFloor } from "../../../topology.ts"; /** Offer a free spot next to the target plan; the dialog lets the user adjust both coordinates. */ export function roomMovePosition(target: Floor): Vec2 { const points=target.rooms.flatMap((r)=>r.points); if(!points.length)return [0,0]; const box=bounds(points);return [box.x1+1,box.z0]; } /** Move, never copy, the room and its contents. A rejected operation leaves the document untouched. */ export function moveRoomToFloor(b: Building, sourceId: string, roomId: string, targetId: string, position: Vec2): string | null { const source=b.floors.find((f)=>f.id===sourceId),target=b.floors.find((f)=>f.id===targetId); const room=source?.rooms.find((r)=>r.id===roomId); if(!source||!target||!room)return "Raum oder Zieletage ist nicht mehr vorhanden."; if(source===target||isOutdoorFloor(target))return "Bitte eine andere Gebäudeetage auswählen."; if(!position.every(Number.isFinite))return "Die Zielposition muss aus gültigen Zahlen bestehen."; const belongs=(x:number,z:number)=>source.rooms.find((r)=>pointInPolygon([x,z],r.points))?.id===roomId; const furniture=source.furniture.filter((p)=>belongs(p.x,p.z)); const placements=source.placements.filter((p)=>!p.outside&&belongs(p.x,p.z)); const notes=(source.notes??[]).filter((p)=>belongs(p.x,p.z)); const openings=source.openings.filter((o)=>o.room_id===roomId); if(target.rooms.some((r)=>r.id===roomId)||furniture.some((p)=>target.furniture.some((q)=>q.id===p.id))||placements.some((p)=>target.placements.some((q)=>q.entity_id===p.entity_id))||openings.some((o)=>target.openings.some((q)=>q.id===o.id))||notes.some((n)=>target.notes?.some((q)=>q.id===n.id)))return "Die Zieletage enthält bereits einen Raum oder ein Gerät mit derselben Kennung."; const originalPoints=room.points,box=bounds(originalPoints),dx=position[0]-box.x0,dz=position[1]-box.z0; const at=([x,z]:Vec2):Vec2=>[Math.round((x+dx)*1000)/1000,Math.round((z+dz)*1000)/1000]; const shift=(p:{x:number;z:number})=>[p.x,p.z]=at([p.x,p.z]); const movingWalls=new Set(),copiedWalls=new Map(); let wallConflict=false; const walls=(source.walls??[]).flatMap((w)=>{ const own=openings.some((o)=>o.wall===w.id),shared=source.openings.some((o)=>o.wall===w.id&&o.room_id!==roomId); if(!own&&(shared||!belongs(w.a[0],w.a[1])||!belongs(w.b[0],w.b[1])))return []; if(shared){const id=uid("wall");copiedWalls.set(w.id,id);return [{...structuredClone(w),id}];} if(target.walls?.some((q)=>q.id===w.id)){wallConflict=true;return [];} movingWalls.add(w.id);return [w]; }); if(wallConflict||openings.some((o)=>o.wall&&!movingWalls.has(o.wall)&&!copiedWalls.has(o.wall)))return "Eine zugehörige Wand kann nicht eindeutig auf die Zieletage verschoben werden."; source.rooms=source.rooms.filter((r)=>r!==room);target.rooms.push(room); const transfer=(items:T[],from:T[],to:T[]):T[]=>{const set=new Set(items);to.push(...items);return from.filter((p)=>!set.has(p));}; source.furniture=transfer(furniture,source.furniture,target.furniture); source.placements=transfer(placements,source.placements,target.placements); source.openings=transfer(openings,source.openings,target.openings); if(notes.length){target.notes??=[];source.notes=transfer(notes,source.notes??[],target.notes);} if(walls.length){source.walls=(source.walls??[]).filter((w)=>!movingWalls.has(w.id));target.walls??=[];target.walls.push(...walls);} room.points=room.points.map(at); if(room.start_view?.target)shift(room.start_view.target); for(const item of [...furniture,...placements,...notes])shift(item); for(const w of walls){w.a=at(w.a);w.b=at(w.b);} for(const o of openings)if(o.wall&&copiedWalls.has(o.wall))o.wall=copiedWalls.get(o.wall)!; for(const route of b.energy.routes??[])for(const p of route.points)if(p.floor_id===sourceId&&pointInPolygon([p.x,p.z],originalPoints)){p.floor_id=targetId;shift(p);} return null; }