import{r as t,h as e,H as i,g as s}from"./p-aee8b1df.js";const n=class{constructor(e){t(this,e),this.options=[],this.handleOption=t=>{var e;if(!this.template)return;t.cancelBubble=!0;const i=t.target.value||[],s=this.el.shadowRoot.querySelector("bce-option bce-input"),n=this.template.options.length-1,r=null===(e=this.value)||void 0===e?void 0:e.find(t=>t.index===n),h=i.indexOf(""+n)>=0;this.template.other&&!r&&h&&s.shadowRoot.querySelector("input").focus(),this.value=i.map(t=>{const e=parseInt(t,10);return{index:e,text:s&&e===this.template.options.length-1?s.value:this.template.options[e].text}}).sort((t,e)=>t.index-e.index),this.el.dispatchEvent(new Event("input",{bubbles:!0,composed:!0}))},this.handleInput=t=>{t.cancelBubble=!0}}generateOptions(){const t=this.handler.t("options").map((t,e)=>({option:t,index:e}));if(this.shuffle)for(let e=t.length-1;e>0;e--){let i=Math.floor(Math.random()*(e+1));[t[e],t[i]]=[t[i],t[e]]}this.options=t}async validate(t=!1){const e=this.el.shadowRoot.querySelectorAll("bce-input, bce-select"),i=Array.from(e),s=await Promise.all(i.map(e=>e.validate(t)));return[].concat(...s)}componentWillLoad(){this.generateOptions()}renderOther(t,i){const s=this.value&&this.value[this.value.length-1],n=s&&s.index===i;return e("bce-input",{disabled:this.disabled,placeholder:t.text,validation:n?"required":"",value:(n?s&&s.text:"")||"",onInput:this.handleInput})}renderOption(t,i){return this.template?e("bce-option",{value:""+i},this.template.other&&i===this.template.options.length-1?this.renderOther(t,i):t.text):null}render(){if(!this.template)return null;const t=this.template.required?["required",this.template.validation].filter(t=>!!t).join("|"):this.template.validation;return e(i,{orientation:this.template.orientation},e("p",null,this.handler.t("question")),e("bce-select",{disabled:this.disabled,type:"checkbox",value:this.value&&this.value.map(t=>t.index+""),validation:t,onInput:this.handleOption},this.options.map(t=>this.renderOption(t.option,t.index))))}get el(){return s(this)}static get watchers(){return{language:["generateOptions"],shuffle:["generateOptions"],template:["generateOptions"]}}static get style(){return":host p{font-weight:500;text-align:center}\@media screen and (min-width: 1024px){:host p{text-align:left}}:host bce-input{margin:0;padding:0 0 8px}:host([orientation=horizontal]) bce-option{display:-ms-inline-flexbox;display:inline-flex;margin:0 10px}"}};export{n as bce_element_checkbox};