import { r as registerInstance, h, H as Host, g as getElement } from './core-095fd691.js';

const BceElementCheckbox = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.handleOption = (event) => {
            if (!this.template)
                return;
            event.cancelBubble = true;
            const input = event.target;
            const value = (input.value || []);
            const query = this.el.querySelector('bce-option bce-input');
            const otherEl = query;
            this.value = value.map(value => {
                const index = parseInt(value, 10);
                const text = otherEl && index === this.template.options.length - 1
                    ? otherEl.value
                    : this.template.options[index].text;
                return { index, text };
            });
            // const index = -1;
            // if (otherEl && index === this.template!.options.length - 1)
            this.el.dispatchEvent(new Event('input', { bubbles: true }));
        };
        this.handleInput = (event) => {
            event.cancelBubble = true;
            const options = Array.from(this.el.querySelectorAll('bce-option') || []);
            const last = options[options.length - 1];
            if (!last.checked)
                return;
            const input = event.target;
            this.value = [
                ...this.value.slice(0, -1),
                { index: parseInt(last.value, 10), text: input.value }
            ];
            this.el.dispatchEvent(new Event('input', { bubbles: true }));
        };
    }
    renderOther(option, index) {
        const value = this.value && this.value[this.value.length - 1];
        const text = value && value.index === index ? value.text : '';
        return (h("bce-input", { placeholder: option.text, value: text || '', onInput: this.handleInput }));
    }
    renderOption(option, index) {
        if (!this.template)
            return null;
        const other = this.template.other && index === this.template.options.length - 1;
        return (h("bce-option", { value: '' + index }, other ? this.renderOther(option, index) : option.text));
    }
    render() {
        if (!this.template)
            return null;
        return (h(Host, { orientation: this.template.orientation }, h("p", null, this.template.question), h("bce-input", { type: "checkbox", value: this.value && this.value.map(value => value.index + ''), validation: this.template.required ? 'required' : '', onInput: this.handleOption }, this.template.options.map((o, i) => this.renderOption(o, i)))));
    }
    get el() { return getElement(this); }
    static get style() { return "bce-element-checkbox p{font-weight:500;text-align:center}\@media screen and (min-width:1024px){bce-element-checkbox p{text-align:left}}bce-element-checkbox bce-option[type=checkbox][checked] bce-input[focus] input{border-color:var(--bce-color-n)}bce-element-checkbox bce-option[type=checkbox][checked] bce-input input{background-color:var(--bce-color-light-l);border:1px solid var(--bce-color-light-d)}bce-element-checkbox bce-option[type=checkbox] bce-input{margin:0;width:auto;min-width:200px}bce-element-checkbox bce-option[type=checkbox] bce-input input{margin:0;min-width:200px;border-radius:4px}"; }
};

export { BceElementCheckbox as bce_element_checkbox };
