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: 'a9cf218fa410c2f68ee7af1281bb9ab96c401e11', 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: '9cd827f6d86754cabc6783fc92274e99af56bac6', class: "wrapper" }, h("slot", { key: '32d6f2128425b41e67a01f295d4f41c3dd452dd6' })), h("span", { key: '2221a2044eaa59fa3371ea0d3ad0e80790536897', class: "checkmark" }), h("div", { key: 'e5ea4a1f7ce5afc2f8f49fee9abda6fea71b5047', class: "actions" }, h("slot", { key: 'a62f43f5156be4975a80c7baf7883d4a68268cd9', 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
            }];
    }
}
