import{b as a}from"./index-b983ad9b.js";const g=new Map;class d{constructor(t){t&&this.setData(t)}setData(t){t.id&&(this.id=parseInt(t.id)),t.area_id&&(this.area_id=parseInt(t.area_id)),t.name&&(this.name=t.name),t.code&&(this.code=t.code),t.path&&(this.path=t.path)}get fullName(){const t=[];this.path&&t.push(...this.path.split("/")),t.push(this.name);for(let i=0;i<t.length-1;i++)t[i].charCodeAt(t[i].length-1)>128||t[i+1].charCodeAt(0)>128||(t[i]+=" ");return t.join("")}static async findAll(t){const i=a(),{items:e}=await i.get({path:"api/v1/buildings",query:{st:0,pp:-1,...t}})||{};return(e||[]).map(s=>d.get(s))}static get(t){let i=g.get(t.id);return i||(i=new d(t),g.set(t.id,i)),i}}const f=d,u=new Map;let r=new Map,c=0;async function m(h){if(c<Date.now()){const i=a(),{items:e}=await i.get("api/v1/buildings?pp=-1")||{};r.clear(),e.forEach(s=>{r.set(s.name,s.id)}),c=Date.now()+9e5}const t=[...r.entries()].find(i=>h.startsWith(i[0]));return t?[t[1],h.slice(t[0].length)]:[0,h]}class o{constructor(t){t&&this.setData(t)}setData(t){const{building:i,...e}=t;i&&(this.building=f.get(i)),Object.assign(this,e)}get codeOrName(){return this.code&&this.name&&this.name!==this.code?`${this.code} ${this.name}`:this.code||this.name}get fullName(){var i;const t=[];(i=this.building)!=null&&i.id&&(this.building.path&&t.push(...this.building.path.split("/")),t.push(this.building.name)),t.push(this.codeOrName);for(let e=0;e<t.length-1;e++)t[e].charCodeAt(t[e].length-1)>128||t[e+1].charCodeAt(0)>128||(t[e]+=" ");return t.join("")}relativeName(t){var e,s;if(!t)return this.fullName;let i=this.name;return((e=t.building)==null?void 0:e.id)===((s=this.building)==null?void 0:s.id)?i:this.fullName}static async findByPk(t,i){let e=u.get(t);return e||(e=new Promise(async(s,l)=>{try{const p=await a().get({path:`api/v1/room/${t}`,query:{includes:"building area,role/room_master",...i}});s(new o(p))}catch(n){l(n)}}),u.set(t,e)),e}static async findByBuilding(t,i){const e=a(),{items:s,...l}=await e.get({path:`api/v1/building/${t}/rooms`,query:{pp:25,includes:"building",order:"building",...i}})||{};return{...l,items:await Promise.all((s||[]).map(n=>o.get(n)))}}static async findAll(t){const{items:i}=await this.findAndCountAll(t);return i}static async findAndCountAll(t){const i=a(),e={};if(t&&(t.pp!==void 0&&(e.pp=t.pp),e.pp>0&&t.st?e.pg=Math.floor(t.st/e.pp):t.pg&&(e.pg=t.pg),t.buildingId&&(e.building=t.buildingId),t.permissions&&(e.room_permission$=`{${t.permissions.join(",")}}`),t.includes&&(e.includes=t.includes),t.operable&&(e.operable=t.operable),t.keywords&&t.keywords!==""))if(t.buildingId)e.keywords=t.keywords;else{const[n,p]=await m(t.keywords);n&&(e.building=n),e.keywords=p}const{items:s,...l}=await i.get({path:"api/v1/rooms",query:{pp:25,includes:"building",order:"building",...e,...t.customField}})||{};return{...l,items:await Promise.all((s||[]).map(n=>o.get(n)))}}static get(t){let i=t&&u.get(t.id);return i||(i=new o(t),t.id&&u.set(t.id,Promise.resolve(i))),i}}const w=o;export{f as B,w as R};
