import { Host, h } from "@stencil/core";
import { cls, isEmpty } from "../../utils/helpers";
export class XSelectOption {
    constructor() {
        this.value = undefined;
        this.selected = undefined;
        this.disabled = undefined;
    }
    /**
     * Listen for keyboard events for confirmation
     */
    handleKeyDown(ev) {
        if ([' ', 'Enter'].includes(ev.key)) {
            this.handleClick();
        }
    }
    /**
     * Element click event listener
     *
     * @returns
     */
    handleClick() {
        if (this.disabled) {
            return;
        }
        const parent = this.el.parentElement;
        if (parent.multiple) {
            this.selected = !this.selected;
        }
        else if (this.selected && parent.unselectable) {
            this.selected = false;
        }
        else {
            parent.querySelectorAll('x-selector-tag').forEach(item => {
                item.selected = false;
            });
            this.selected = true;
        }
        parent.dispatchEvent(new Event('changed', { bubbles: false }));
    }
    /**
     * This is called on component before load
     */
    componentWillLoad() {
        var _a, _b;
        if (isEmpty(this.value)) {
            this.value = (_b = (_a = this.el) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.trim();
        }
    }
    /**
     * Element render
     */
    render() {
        return h(Host, { key: '7510fa32728ab62fed9eccde728fe918969cac13', class: cls({
                'selected': this.selected,
                'disabled': this.disabled,
            }), tabindex: "0" }, h("div", { key: 'b8430630e6cdcc9b6ed938a2a5f03be2dedf81f3', class: "wrapper" }, h("slot", { key: 'd75f51729e0a8a50aa12bbb3c0fcfe55ed139ff5' })));
    }
    static get is() { return "x-selector-tag"; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "value": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element value"
                },
                "attribute": "value",
                "reflect": false
            },
            "selected": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element selected state"
                },
                "attribute": "selected",
                "reflect": false
            },
            "disabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element disabled state"
                },
                "attribute": "disabled",
                "reflect": false
            }
        };
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "keydown",
                "method": "handleKeyDown",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "click",
                "method": "handleClick",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
