import { css, html, LitElement, nothing, type PropertyValues } from "lit";
import { uid, type Building, type PlanTopology } from "../../model.ts";
import { addTopologyOutside, enableTopology, isOutdoorFloor, outsideFloor, outsideSelection, type OutsideOwner } from "../../topology.ts";
import { tokens } from "../../styles.ts";
import { hint } from "./fields.ts";
import type { EditorScope } from "./editor-context.ts";
/** Metadata only. Picking a row never reassigns floors or creates a storey. */
class HierarchyManager extends LitElement {
static properties = { doc:{attribute:false}, admin:{type:Boolean}, suggestedName:{attribute:false},
locationId:{state:true}, branchId:{state:true}, buildingId:{state:true} };
declare doc: Building;
declare admin: boolean;
declare suggestedName: string;
private declare locationId: string;
private declare branchId: string;
private declare buildingId: string;
constructor(){super();this.admin=false;this.suggestedName="Gebäude 1";this.locationId="";this.branchId="";this.buildingId="";}
protected willUpdate(_changed:PropertyValues){
const t=this.doc?.topology;if(!t)return;
if(!t.locations.some((l)=>l.id===this.locationId))this.locationId=t.locations[0]?.id??"";
const branches=t.branches.filter((n)=>n.location_id===this.locationId);
if(!branches.some((n)=>n.id===this.branchId))this.branchId=branches[0]?.id??"";
const buildings=t.buildings.filter((g)=>g.branch_id===this.branchId);
if(!buildings.some((g)=>g.id===this.buildingId))this.buildingId=buildings[0]?.id??"";
}
private edit(change:(b:Building,t:PlanTopology)=>void){
if(!this.admin||!this.doc?.topology)return;
const next=structuredClone(this.doc);change(next,next.topology!);this.publish(next);
}
private publish(next:Building){this.doc=next;this.dispatchEvent(new CustomEvent("hierarchy-change",{detail:{building:next},bubbles:true,composed:true}));}
private open(scope:EditorScope){this.dispatchEvent(new CustomEvent("hierarchy-open",{detail:scope,bubbles:true,composed:true}));}
private outsideButton(owner:OutsideOwner){
const found=!!outsideFloor(this.doc,outsideSelection(owner));
return html``;
}
private name(label:string,value:string,save:(name:string)=>void){
return html``;
}
render(){
const t=this.doc?.topology;
if(!t)return html`
${hint("Vorhandene Etagen werden einem ersten Ort, einer Niederlassung und einem Gebäude zugeordnet. Geometrie, Raumverknüpfungen und Benutzerrechte bleiben erhalten.")}
${hint("Hier pflegst du Ort, Niederlassung und Gebäude. Die Listen wechseln nur die Auswahl. Etagen legst du anschließend oben im Editor im gewählten Gebäude an. Räume und ihre Symcon-Bereichskategorie bearbeitest du rechts auf der ausgewählten Etage.")}
Orte
${t.locations.map((s)=>html``)}
${l?html`
${this.name("Name des Ortes",l.name,(name)=>{if(this.locationId===l.id)this.edit((_,p)=>{p.locations.find((s)=>s.id===l.id)!.name=name;});})}
${this.outsideButton({kind:"location",id:l.id})}
${this.name("Name der Niederlassung",n.name,(name)=>{if(this.branchId===n.id)this.edit((_,p)=>{p.branches.find((s)=>s.id===n.id)!.name=name;});})}
${this.outsideButton({kind:"branch",id:n.id})}
${this.name("Name des Gebäudes",g.name,(name)=>{if(this.buildingId===g.id)this.edit((_,p)=>{p.buildings.find((s)=>s.id===g.id)!.name=name;});})}
${this.outsideButton({kind:"building",id:g.id})}