import { Host, h } from "@stencil/core";
import { cls, isEmpty } from "../../utils/helpers";
export class XSelectOption {
    constructor() {
        /**
         * Element has not empty actions slot
         */
        this.hasActionsSlot = false;
        this.value = undefined;
        this.selected = undefined;
        this.disabled = undefined;
        this.image = undefined;
        this.block = undefined;
        this.stretch = undefined;
        this.skew = undefined;
    }
    /**
     * On component will be loaded hook
     */
    componentWillLoad() {
        var _a, _b;
        this.hasActionsSlot = !!this.el.querySelector('[slot="actions"]');
        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();
        }
    }
    /**
     * 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-item').forEach(item => {
                item.selected = false;
            });
            this.selected = true;
        }
        parent.dispatchEvent(new Event('changed', { bubbles: false }));
    }
    /**
     * Element render
     */
    render() {
        return h(Host, { key: 'b2c04cce4909f50a24edf8c3aec4e72191225d5f', class: cls({
                'selected': this.selected,
                'disabled': this.disabled,
                'block': this.block,
                'skew': this.skew,
                'with-actions': this.hasActionsSlot,
            }), style: { backgroundImage: this.image ? `url(${this.image})` : 'none' }, tabindex: "0" }, h("div", { key: '0c0cbc62bfcbf54d85d04c0c1f5c39950fd5a424', class: "wrapper" }, h("slot", { key: 'bd5cdbdd38a202b0dc9634d62d37474370b98f78' })), h("span", { key: 'c0440b1310bd6e9344efa987b3d6eb68bbdf3fe5', class: "checkmark" }), h("div", { key: '9b2837ae43be4cbf1e023daf962e940037d16a20', class: "actions" }, h("slot", { key: '77db828e6c1cf6196c1776500619a1f118643bbb', name: "actions" })));
    }
    static get is() { return "x-selector-item"; }
    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
            },
            "image": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element image"
                },
                "attribute": "image",
                "reflect": false
            },
            "block": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element block"
                },
                "attribute": "block",
                "reflect": false
            },
            "stretch": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element stretch"
                },
                "attribute": "stretch",
                "reflect": false
            },
            "skew": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element skew image background style"
                },
                "attribute": "skew",
                "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
            }];
    }
}
