import{r as t,d as i,h as s,g as e}from"./p-fdd3ce28.js";var n;const h=class{constructor(s){t(this,s),this.value=[],n.set(this,window.BCE.file),this.handleInput=(t,i)=>{const s=t.target;if(!s)return;const e=[...this.value],n=Object.assign(Object.assign({},this.value[i]),{text:s.value});e.splice(i,1,n),this.updateValue(e),t.stopPropagation()},this.handleKeyDown=async(t,i)=>{switch(t.keyCode){case 8:return void(t.target.value||0==i||(this.focusOption(i-1),this.handleRemove(i)));case 13:return this.addOption()}},this.handleOther=()=>{this.other=!this.other,this.value=this.other?[...this.value,{text:this.label.labelOCOther||""}]:this.value.slice(0,-1),this.onOther.emit(this.other)},this.handleRemove=t=>{const i=[...this.value];i.splice(t,1),this.updateValue(i)},this.handleColor=()=>{},this.handleImage=t=>{const i=this.value[t];"image"in i&&i.image?this.deleteImage(i,t):this.uploadImage(t)},this.onOther=i(this,"other",7)}watchValue(t){t.length||this.addOption(!1)}async addOption(t=!0){const i={text:""},s=this.other?[...this.value.slice(0,-1),i,...this.value.slice(-1)]:[...this.value,i];this.updateValue(s),t&&this.focusLast(!0)}async focusLast(t){return this.focusOption(-(this.other?2:1),t)}async focusOption(t,i=!1){i&&await new Promise(t=>setTimeout(t,100));const s=this.el.shadowRoot.querySelectorAll("[data-option] bce-input"),e=s[t<0?s.length+t:t],n=e&&e.shadowRoot.querySelector("input");n&&n.focus()}async uploadImage(t){const i=document.querySelector("bce-root"),s=document.createElement("bce-upload");s.accept="image/*",s.active=!0,s.dialog=!0;const e=await i.execute(s,t=>{s.addEventListener("input",i=>{t(i.target.value[0])})},{display:!0}),n=[...this.value];n.splice(t,1,Object.assign(Object.assign({},this.value[t]),{image:e})),console.log(n)}deleteImage(t,i){if(!("image"in t&&t.image))return;(function(t,i){if(!i.has(t))throw new TypeError("attempted to get private field on non-instance");return i.get(t)})(this,n).delete(t.image,{});const s=[...this.value],e=Object.assign({},this.value[i]);"image"in e&&delete e.image,s.splice(i,1,e),this.updateValue(s)}updateValue(t){this.value=t;const i=new globalThis.Event("input",{bubbles:!0,composed:!0});this.el.dispatchEvent(i)}componentWillLoad(){this.watchValue(this.value)}renderTooltip(t,i){return i?s("bce-tooltip",{placement:"left",icon:t},i):null}renderMetadata(t){if("image"in t)return s("div",{"data-meta":!0},this.renderTooltip("fas:image",t.image))}renderOption(t,i,e=!1){return t.hasOwnProperty("text")?s("div",{key:i,"data-option":!0},s("bce-input",{value:t.text,onInput:t=>this.handleInput(t,i),onKeyDown:t=>this.handleKeyDown(t,i),placeholder:this.placeholder&&`${this.placeholder} ${i+1}`}),this.renderMetadata(t),s("bce-menu",null,s("slot",{name:"option"}),this.allowColor&&s("bce-button",{icon:"fas:palette",onClick:()=>this.handleColor(i)},"Color"),this.allowImage&&!e&&s("bce-button",{icon:"fas:image",onClick:()=>this.handleImage(i)},"image"in t&&t.image?this.label.labelOCImageDelete:this.label.labelOCImageUpload),this.value.length>1&&s("bce-button",{icon:"fas:trash",onClick:()=>this.handleRemove(i)},this.label.labelOCOptionRemove))):null}render(){const t=this.other?this.value.slice(0,-1):this.value,i=this.other&&this.value[this.value.length-1];return[t.map((t,i)=>this.renderOption(t,i)),i&&this.renderOption(i,this.value.length-1,!0),s("slot",null),s("div",null,s("bce-button",{design:"text",icon:"fas:plus",onClick:()=>this.addOption()},this.label.labelOCOptionAdd),this.allowOther&&s("bce-button",{design:"text",icon:"fas:font",onClick:this.handleOther},this.other?this.label.labelOCOtherRemove:this.label.labelOCOtherAdd),s("slot",{name:"action"}))]}get el(){return e(this)}static get watchers(){return{value:["watchValue"]}}static get style(){return":host{position:relative;display:block;margin-bottom:40px}:host bce-icon,:host bce-input{--bce-input-height:40px;z-index:2}:host bce-button{text-transform:none}:host>:last-child{position:absolute;left:8px;bottom:-34px}:host [data-option]{position:relative}:host [data-option]>bce-input{margin:5px 0}:host [data-option]>bce-input input{padding:13px 38px}:host [data-option]>bce-icon{position:absolute;top:50%;left:10px;opacity:0.6;-webkit-transform:translateY(-50%);transform:translateY(-50%)}:host [data-option]>[data-meta]{position:absolute;right:30px;z-index:3;bottom:0;padding:4px}:host [data-option]>[data-meta] bce-icon{font-size:14px;opacity:0.5}:host [data-option]>bce-menu{position:absolute;right:0;top:0;z-index:3}\@media screen and (min-width: 1024px){:host [data-option]>bce-input:not([compact]) input{padding:6px 38px}}"}};n=new WeakMap;export{h as bce_option_creator};