'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-457520a1.js');

const BceElementRadio = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.handleFocus = (event) => {
            // const input = event.target as HTMLBceInputElement;
            // this.value = {
            //   index: this.template.options.length - 1,
            //   text: input.value as string
            // };
            // this.el.dispatchEvent(new Event('input', { bubbles: true }));
        };
        this.handleOption = (event) => {
            if (!this.template)
                return;
            event.cancelBubble = true;
            const input = event.target;
            const index = parseInt(input.value, 10);
            if (isNaN(index))
                return;
            const text = this.template.options[index].text;
            this.value = { index, text };
            if (this.template.other && index === this.template.options.length - 1) {
                const query = this.el.shadowRoot.querySelector('bce-option bce-input');
                const input = query;
                this.value.text = input.value;
                input.shadowRoot.querySelector('input').focus();
            }
            this.el.dispatchEvent(new Event('input', { bubbles: true }));
        };
        this.handleInput = (event) => {
            event.cancelBubble = true;
            this.value = {
                index: this.template.options.length - 1,
                text: event.target.value
            };
            this.el.dispatchEvent(new Event('input', { bubbles: true }));
        };
    }
    async validate(silent = false) {
        const root = this.el.shadowRoot;
        const query = root.querySelectorAll('bce-input, bce-select');
        const inputs = Array.from(query);
        const errors = await Promise.all(inputs.map(el => el.validate(silent)));
        return [].concat(...errors);
    }
    renderOther(option, index) {
        const isInput = this.value && this.value.index === index;
        const text = isInput ? this.value.text : '';
        const validation = isInput ? 'required' : '';
        return (index$1.h("bce-input", { placeholder: option.text, validation: validation, value: text || '', onFocus: this.handleFocus, onInput: this.handleInput }));
    }
    renderOption(option, index) {
        if (!this.template)
            return null;
        const other = this.template.other && index === this.template.options.length - 1;
        return (index$1.h("bce-option", { value: '' + index }, other ? this.renderOther(option, index) : option.text));
    }
    render() {
        if (!this.template)
            return null;
        return (index$1.h(index$1.Host, { orientation: this.template.orientation }, index$1.h("p", null, this.handler.t('question')), index$1.h("bce-select", { type: "radio", validation: this.template.required ? 'required' : '', value: this.value ? '' + this.value.index : undefined, onInput: this.handleOption }, this.handler
            .t('options')
            .map((o, i) => this.renderOption(o, i)))));
    }
    get el() { return index$1.getElement(this); }
    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}"; }
};

exports.bce_element_radio = BceElementRadio;
