import{proxyCustomElement as t,HTMLElement as e,createEvent as s,h as i,transformTag as o}from"@stencil/core/internal/client";import{d as r}from"./response-format-item.js";import{d as n}from"./property-selector.js";function l(t,e,s){if(null===s)return void t.push({path:e});const i=s,o="_alias"in i,r="#description"in i,n=Object.keys(i).filter((t=>"_alias"!==t&&"#description"!==t));if(0!==n.length)if(o||r){const s={};for(const t of n)s[t]=i[t];t.push(...a(s,e))}else t.push(...a(s,e));else{const s=i._alias,o=i["#description"];t.push(s||o?{path:e,alias:s,description:o}:{path:e})}}function a(t,e=""){if(!t)return[];const s=[];for(const[i,o]of Object.entries(t))"object"==typeof o&&l(s,e?`${e}.${i}`:i,o);return s}function c(t,e,s){const i=t[e];if(i&&"object"==typeof i&&!("_alias"in i)&&!("#description"in i))return;const o={};s.alias&&""!==s.alias.trim()&&(o._alias=s.alias),s.description&&""!==s.description.trim()&&(o["#description"]=s.description),t[e]=Object.keys(o).length>0?o:null}function m(t,e){const s=t[e];if(!s)return t[e]={},t[e];if("object"==typeof s){const t=s;return Object.keys(t).filter((t=>"_alias"!==t&&"#description"!==t)),t}return t[e]={},t[e]}const p=t(class extends e{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.change=s(this,"change",7),this.label="Properties",this.items=[],this.handleItemChange=t=>e=>{e.stopPropagation();const s=[...this.items];null===e.detail?s.splice(t,1):s[t]=e.detail,this.items=s,this.emitChange()},this.handleAddProperty=()=>{this.items=[...this.items,{path:""}],this.emitChange()}}componentWillLoad(){this.items=this.value?this.isEmptyResponseFormat(this.value)?[]:this.value.object?a(this.value.object):[]:[{path:"_id"}]}componentWillUpdate(){if(this.value)if(this.value.object&&!this.isEmptyResponseFormat(this.value)){const t=a(this.value.object);(t.length!==this.items.length||!t.every(((t,e)=>{const s=this.items[e];return s&&t.path===s.path&&t.alias===s.alias&&t.description===s.description})))&&(this.items=t)}else this.items.length>0&&(this.items=[])}render(){return this.limetype?0===this.items.length?i("div",{class:"response-format-editor"},i("h4",{class:"header"},this.label),i("div",{class:"empty-state"},i("p",{class:"empty-state-message"},"No properties selected. The response will return empty objects."),i("limel-button",{label:"Add Property",icon:"plus_math",onClick:this.handleAddProperty}))):i("div",{class:"response-format-editor"},i("limel-header",{subheading:this.label},this.renderPropertyCount()),i("div",{class:"property-list"},this.items.map(((t,e)=>this.renderItem(t,e))),i("limel-button",{label:"Property",disabled:this.items.some((t=>!t.path||""===t.path.trim())),icon:{name:"plus_math",title:"Add"},onClick:this.handleAddProperty}))):i("div",{class:"empty-state"},i("p",null,"Select a limetype to choose properties"))}renderPropertyCount(){return[i("limel-badge",{slot:"actions",id:"counter-badge",label:this.items.length.toString()}),i("limel-tooltip",{elementId:"counter-badge",slot:"actions",label:"Number of selected properties"})]}renderItem(t,e){return i("limebb-lime-query-response-format-item",{key:`${t.path}-${e}`,class:"property-item",platform:this.platform,context:this.context,limetype:this.limetype,item:t,onItemChange:this.handleItemChange(e)})}emitChange(){const t=function(t){const e={};for(const s of t){const t=s.path.split(".");let i=e;for(let e=0;e<t.length;e++){const o=t[e];e===t.length-1?c(i,o,s):i=m(i,o)}}return e}(this.items);this.change.emit({object:t})}isEmptyResponseFormat(t){return!(!t||0!==Object.keys(t).length&&(!t.object||0!==Object.keys(t.object).length||t.aggregates))}static get style(){return":host(limebb-lime-query-response-format-editor){display:block;width:100%}.response-format-editor{display:flex;flex-direction:column;padding:1rem 0}limel-badge[slot=actions]{--badge-background-color:rgb(var(--contrast-200));margin-right:0.25rem}.property-list{display:flex;flex-direction:column;border-radius:0 0 0.75rem 0.75rem;padding:0.25rem;background-color:rgb(var(--contrast-300))}.empty-state{padding:2rem;text-align:center;color:rgb(var(--contrast-700));font-style:italic}.empty-state p{margin:0}.empty-state .empty-state-message{margin-bottom:1rem;font-size:0.875rem}.empty-state limel-button{margin:0 auto;max-width:200px}"}},[1,"limebb-lime-query-response-format-editor",{platform:[16],context:[16],limetype:[1],value:[16],label:[1],items:[32]}]);function h(){"undefined"!=typeof customElements&&["limebb-lime-query-response-format-editor","limebb-lime-query-response-format-item","limebb-property-selector"].forEach((t=>{switch(t){case"limebb-lime-query-response-format-editor":customElements.get(o(t))||customElements.define(o(t),p);break;case"limebb-lime-query-response-format-item":customElements.get(o(t))||r();break;case"limebb-property-selector":customElements.get(o(t))||n()}}))}export{p as R,h as d}