var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return privateMap.get(receiver);
};
var _id;
import { library } from '@fortawesome/fontawesome-svg-core';
import { faCheck } from '@fortawesome/free-solid-svg-icons';
import { h, Host } from "@stencil/core";
library.add(faCheck);
export class Option {
    constructor() {
        this.type = 'checkbox';
        // #endregion
        this._hasLabel = false;
        _id.set(this, window.BCE.generateId());
        this.handleClick = () => {
            var _a;
            const query = this.name
                ? `bce-option[type='${this.type}'][name='${this.name}']`
                : `bce-option[type='${this.type}']`;
            const elements = (_a = this.el.parentNode) === null || _a === void 0 ? void 0 : _a.querySelectorAll(query);
            const options = Array.from(elements || []);
            switch (this.type) {
                case 'checkbox':
                    this.checked = !this.checked;
                    const value = options
                        .filter(c => !!c.checked)
                        .map(c => c.value);
                    this.onOption.emit(value);
                    return;
                case 'dropdown':
                case 'radio':
                    for (const option of options)
                        option.checked = this.el === option;
                    this.onOption.emit(this.value);
                    return;
            }
        };
        this.handleSlotChange = () => {
            this._hasLabel = !!this.el.childNodes.length;
        };
        this.ignoreEvent = (event) => {
            event.stopPropagation();
        };
    }
    componentWillLoad() {
        this.handleSlotChange();
    }
    componentDidLoad() {
        const slot = this.el.shadowRoot.querySelector('slot');
        slot === null || slot === void 0 ? void 0 : slot.addEventListener('slotchange', this.handleSlotChange);
    }
    renderInner() {
        return [
            h("input", { id: __classPrivateFieldGet(this, _id), checked: !!this.checked, name: this.name, role: this.a11yRole, type: this.type === 'dropdown' ? 'checkbox' : this.type, "aria-selected": this.type === 'dropdown' && this.checked, onInput: this.ignoreEvent }),
            this.type === 'checkbox' && this.checked && (h("bce-icon", { raw: "fas:check", "fixed-width": true })),
            h("label", { htmlFor: __classPrivateFieldGet(this, _id), onClick: this.ignoreEvent, "data-empty": !this._hasLabel },
                h("slot", null))
        ];
    }
    renderLi() {
        return h("li", null, this.renderInner());
    }
    render() {
        return (h(Host, { onClick: this.handleClick }, this.type === 'dropdown' ? this.renderLi() : this.renderInner()));
    }
    static get is() { return "bce-option"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() { return {
        "$": ["bce-option.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["bce-option.css"]
    }; }
    static get properties() { return {
        "checked": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "checked",
            "reflect": true
        },
        "disabled": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "disabled",
            "reflect": true
        },
        "name": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "name",
            "reflect": true
        },
        "type": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "OptionType",
                "resolved": "\"checkbox\" | \"dropdown\" | \"radio\"",
                "references": {
                    "OptionType": {
                        "location": "import",
                        "path": "../../models/option-type"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "type",
            "reflect": true,
            "defaultValue": "'checkbox'"
        },
        "value": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "value",
            "reflect": true
        },
        "a11yRole": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "a-1-1y-role",
            "reflect": false
        }
    }; }
    static get states() { return {
        "_hasLabel": {}
    }; }
    static get events() { return [{
            "method": "onOption",
            "name": "bce-core:option",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "complexType": {
                "original": "SelectValue",
                "resolved": "string | string[]",
                "references": {
                    "SelectValue": {
                        "location": "import",
                        "path": "../../models/select-value"
                    }
                }
            }
        }]; }
    static get elementRef() { return "el"; }
}
_id = new WeakMap();
