import { h } from "@stencil/core";
import getAssetPath from "../../utils/assetPath";
export class EclSelect {
    constructor() {
        this.theme = 'ec';
        this.styleClass = undefined;
        this.eclScript = false;
        this.disabled = false;
        this.required = false;
        this.invalid = false;
        this.width = 'm';
        this.selectId = undefined;
        this.name = undefined;
        this.multiple = false;
        this.multiplePlaceholder = undefined;
        this.multipleSearchText = undefined;
        this.multipleSearchNoResultsText = undefined;
        this.multipleAllText = undefined;
        this.multipleClearAllText = undefined;
        this.multipleCloseText = undefined;
        this.inputValue = undefined;
        this.selectValue = undefined;
        this.hasChanged = false;
        this.isFocused = false;
    }
    getClass() {
        const styleClasses = [
            `ecl-select__container`,
            `ecl-select__container--${this.width}`,
            this.styleClass
        ];
        if (this.disabled) {
            styleClasses.push('ecl-select__container--disabled');
        }
        if (this.invalid) {
            styleClasses.push('ecl-select__container--invalid');
        }
        return styleClasses.join(' ');
    }
    componentWillLoad() {
        this.selectValue = JSON.parse(this.inputValue || '[]');
    }
    componentDidLoad() {
        if (this.selectId) {
            const group = this.el.closest('.ecl-form-group');
            if (group) {
                const label = group.querySelector('.ecl-form-label');
                if (label) {
                    label.setAttribute('id', `${this.selectId}-label`);
                }
                const helper = group.querySelector('.ecl-help-block');
                if (helper) {
                    helper.setAttribute('id', `${this.selectId}-helper`);
                }
            }
        }
        if (this.eclScript && this.multiple) {
            // Load the ECL vanilla js if not already present.
            const src = getAssetPath('./build/scripts/ecl-select-vanilla.js');
            if (document.querySelector(`script[src="${src}"]`)) {
                document.querySelector(`script[src="${src}"]`).remove();
            }
            const script = document.createElement('script');
            script.src = src;
            script.onload = () => {
                const select = new ECL.Select(this.el.getElementsByTagName('select')[0]);
                select.init();
            };
            document.body.appendChild(script);
        }
        if (this.inputValue) {
            this.handleSelection();
        }
    }
    handleFocus(event) {
        this.inputFocus.emit(event);
        this.isFocused = true;
    }
    handleChange(event) {
        const selectElement = event.target;
        const selectedOptions = Array.from(selectElement.selectedOptions).map(option => option.value);
        this.selectValue = selectedOptions;
        this.inputChange.emit(this.selectValue);
    }
    handleSelection() {
        ;
        const options = this.el.querySelectorAll('option');
        options.forEach((option) => {
            const optionValue = String(option.value);
            option.selected = this.selectValue.some(val => String(val) === optionValue);
        });
    }
    handleBlur(event) {
        this.inputBlur.emit(event);
        this.isFocused = false;
    }
    render() {
        let attributes = {
            class: 'ecl-select',
            id: this.selectId,
            name: this.name,
            required: this.required,
            disabled: this.disabled,
            value: this.inputValue,
        };
        if (this.multiple) {
            attributes['multiple'] = 'data-ecl-select-multiple';
            attributes['data-ecl-select-default'] = this.multiplePlaceholder;
            attributes['data-ecl-select-no-results'] = this.multipleSearchNoResultsText;
            attributes['data-ecl-select-all'] = this.multipleAllText;
            attributes['data-ecl-select-clear-all'] = this.multipleClearAllText;
            attributes['data-ecl-select-close'] = this.multipleCloseText;
            attributes['data-ecl-select-search'] = this.multipleSearchText;
        }
        return (h("div", { class: this.getClass(), role: "application" }, h("select", Object.assign({}, attributes, { onFocus: ev => this.handleFocus(ev), onBlur: ev => this.handleBlur(ev), onChange: ev => this.handleChange(ev), onInput: ev => this.handleChange(ev) }), h("slot", null)), h("div", { class: "ecl-select__icon" }, h("ecl-icon", { theme: this.theme, "style-class": `ecl-select__icon-shape sc-ecl-select-${this.theme}`, icon: "corner-arrow", size: "s", transform: "rotate-180" }))));
    }
    static get is() { return "ecl-select"; }
    static get originalStyleUrls() {
        return {
            "ec": ["./build/styles/ecl-select-ec.css"],
            "eu": ["./build/styles/ecl-select-eu.css"]
        };
    }
    static get styleUrls() {
        return {
            "ec": ["build/styles/ecl-select-ec.css"],
            "eu": ["build/styles/ecl-select-eu.css"]
        };
    }
    static get assetsDirs() { return ["build"]; }
    static get properties() {
        return {
            "theme": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "theme",
                "reflect": false,
                "defaultValue": "'ec'"
            },
            "styleClass": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "style-class",
                "reflect": false
            },
            "eclScript": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "ecl-script",
                "reflect": false,
                "defaultValue": "false"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "disabled",
                "reflect": false,
                "defaultValue": "false"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "required",
                "reflect": false,
                "defaultValue": "false"
            },
            "invalid": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "invalid",
                "reflect": false,
                "defaultValue": "false"
            },
            "width": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "width",
                "reflect": false,
                "defaultValue": "'m'"
            },
            "selectId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "select-id",
                "reflect": false
            },
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "name",
                "reflect": false
            },
            "multiple": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple",
                "reflect": false,
                "defaultValue": "false"
            },
            "multiplePlaceholder": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple-placeholder",
                "reflect": false
            },
            "multipleSearchText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple-search-text",
                "reflect": false
            },
            "multipleSearchNoResultsText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple-search-no-results-text",
                "reflect": false
            },
            "multipleAllText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple-all-text",
                "reflect": false
            },
            "multipleClearAllText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple-clear-all-text",
                "reflect": false
            },
            "multipleCloseText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple-close-text",
                "reflect": false
            },
            "inputValue": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "input-value",
                "reflect": false
            },
            "hasChanged": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "has-changed",
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "selectValue": {},
            "isFocused": {}
        };
    }
    static get events() {
        return [{
                "method": "inputChange",
                "name": "inputChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "inputFocus",
                "name": "inputFocus",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "FocusEvent",
                    "resolved": "FocusEvent",
                    "references": {
                        "FocusEvent": {
                            "location": "global",
                            "id": "global::FocusEvent"
                        }
                    }
                }
            }, {
                "method": "inputBlur",
                "name": "inputBlur",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "FocusEvent",
                    "resolved": "FocusEvent",
                    "references": {
                        "FocusEvent": {
                            "location": "global",
                            "id": "global::FocusEvent"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
}
//# sourceMappingURL=ecl-select.js.map
