import { h, Host } from "@stencil/core";
export class BceElementRadio {
    constructor() {
        this.handleFocus = (event) => {
            const input = event.target;
            this.value = {
                index: this.template.options.length - 1,
                text: input.value
            };
            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);
            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.querySelector('bce-option bce-input');
                const input = query;
                this.value.text = input.value;
                input.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 }));
        };
    }
    renderOther(option) {
        return (h("bce-input", { placeholder: option.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 (h("bce-option", { value: '' + index }, other ? this.renderOther(option) : 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: "radio", validation: this.template.required ? 'required' : '', value: '' + (this.value ? this.value.index : -1), onInput: this.handleOption }, this.template.options.map((o, i) => this.renderOption(o, i)))));
    }
    static get is() { return "bce-element-radio"; }
    static get originalStyleUrls() { return {
        "$": ["bce-element-radio.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["bce-element-radio.css"]
    }; }
    static get properties() { return {
        "template": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "Block.ElementRadio.Template",
                "resolved": "undefined | { readonly metadata: ElementMetadata; readonly answer: number | null; readonly question: string; readonly required: boolean; readonly orientation: \"horizontal\" | \"vertical\"; readonly options: SelectElementOption[]; readonly other: boolean; }",
                "references": {
                    "Block": {
                        "location": "import",
                        "path": "../../../models/block"
                    }
                }
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            }
        },
        "value": {
            "type": "unknown",
            "mutable": true,
            "complexType": {
                "original": "{ index: number; text: string }",
                "resolved": "undefined | { index: number; text: string; }",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            }
        }
    }; }
    static get elementRef() { return "el"; }
}
