/*!
 * KoliBri - The accessible HTML-Standard
 */
import { __decorate } from "tslib";
import { h } from "@stencil/core";
import clsx from "../../utils/clsx";
import KolFormFieldStateWrapperFc from "../../functional-component-wrappers/FormFieldStateWrapper/FormFieldStateWrapper";
import KolInputContainerFc from "../../functional-component-wrappers/InputContainerStateWrapper/InputContainerStateWrapper";
import KolSelectStateWrapperFc from "../../functional-component-wrappers/SelectStateWrapper/SelectStateWrapper";
import { createUniqueId } from "../../utils/dev.utils";
import { createCtaRef, directClick, directFocus } from "../../utils/element-interaction";
import { propagateSubmitEventToForm } from "../form/controller";
import { SelectController } from "./controller";
export class KolSelectWc {
    async getValue() {
        if (this._multiple) {
            return this.state._value;
        }
        else {
            return Array.isArray(this.state._value) && this.state._value.length > 0 ? this.state._value[0] : this.state._value;
        }
    }
    async focus(options) { }
    async click() { }
    getFormFieldProps() {
        return {
            state: this.state,
            class: clsx('kol-form-field-select', {
                'kol-form-field--has-value': this.state._hasValue,
            }),
            tooltipAlign: this._tooltipAlign,
            onClick: () => { var _a; return (_a = this.ctaRef.el) === null || _a === void 0 ? void 0 : _a.focus(); },
            alert: this.showAsAlert(),
        };
    }
    getSelectProps() {
        return Object.assign(Object.assign({ ref: this.ctaRef, state: this.state }, this.controller.onFacade), { onInput: this.onInput.bind(this), onChange: this.onChange.bind(this), onFocus: (event) => {
                this.controller.onFacade.onFocus(event);
                this.inputHasFocus = true;
            }, onBlur: (event) => {
                this.controller.onFacade.onBlur(event);
                this.inputHasFocus = false;
            } });
    }
    render() {
        return (h(KolFormFieldStateWrapperFc, Object.assign({ key: '1aa065d33eb61c13e5d46118820fb1517c116ef8' }, this.getFormFieldProps()), h(KolInputContainerFc, { key: '5c56852755b4dbf5f808d128bb221c7b9c50d59c', state: this.state }, h("form", { key: '6974fa1eef19077680e0825f50d576c7e8fb0ad1', onSubmit: (event) => {
                event.preventDefault();
                propagateSubmitEventToForm({
                    form: this.host,
                    ref: this.ctaRef.el,
                });
            } }, h("input", { key: 'dca06d50c09696120b037147e9df5d2d7167cc9e', type: "submit", hidden: true }), h(KolSelectStateWrapperFc, Object.assign({ key: 'c382d84c4c6c8098f850eeaa68acc5e03a65e66b' }, this.getSelectProps()))))));
    }
    constructor() {
        this.ctaRef = createCtaRef();
        this._disabled = false;
        this._hideMsg = false;
        this._hideLabel = false;
        this._hint = '';
        this._multiple = false;
        this._required = false;
        this._tooltipAlign = 'top';
        this._touched = false;
        this.state = {
            _hasValue: false,
            _hideMsg: false,
            _id: createUniqueId('select'),
            _label: '',
            _multiple: false,
            _options: [],
            _value: [],
        };
        this.inputHasFocus = false;
        this.controller = new SelectController(this, 'select', this.host);
    }
    showAsAlert() {
        return Boolean(this.state._touched) && !this.inputHasFocus;
    }
    validateAccessKey(value) {
        this.controller.validateAccessKey(value);
    }
    validateDisabled(value) {
        this.controller.validateDisabled(value);
    }
    validateHideMsg(value) {
        this.controller.validateHideMsg(value);
    }
    validateHideLabel(value) {
        this.controller.validateHideLabel(value);
    }
    validateHint(value) {
        this.controller.validateHint(value);
    }
    validateIcons(value) {
        this.controller.validateIcons(value);
    }
    validateLabel(value) {
        this.controller.validateLabel(value);
    }
    validateMsg(value) {
        this.controller.validateMsg(value);
    }
    validateMultiple(value) {
        this.controller.validateMultiple(value);
    }
    validateName(value) {
        this.controller.validateName(value);
    }
    validateOn(value) {
        this.controller.validateOn(value);
    }
    validateOptions(value) {
        this.controller.validateOptions(value);
    }
    validateRequired(value) {
        this.controller.validateRequired(value);
    }
    validateRows(value) {
        this.controller.validateRows(value);
    }
    validateShortKey(value) {
        this.controller.validateShortKey(value);
    }
    validateSyncValueBySelector(value) {
        this.controller.validateSyncValueBySelector(value);
    }
    validateTabIndex(value) {
        this.controller.validateTabIndex(value);
    }
    validateTouched(value) {
        this.controller.validateTouched(value);
    }
    validateValue(value) {
        this.controller.validateValue(value);
    }
    validateVariant(value) {
        this.controller.validateVariant(value);
    }
    componentWillLoad() {
        this._touched = this._touched === true;
        this.controller.componentWillLoad();
        this.state._hasValue = !!this.state._value;
        this.controller.addValueChangeListener((v) => (this.state._hasValue = !!v));
    }
    onInput(event) {
        var _a;
        const selectedValues = Array.from(((_a = this.ctaRef.el) === null || _a === void 0 ? void 0 : _a.options) || [])
            .filter((option) => option.selected)
            .map((option) => { var _a; return (_a = this.controller.getOptionByKey(option.value)) === null || _a === void 0 ? void 0 : _a.value; });
        if (this._multiple) {
            this._value = selectedValues;
            this.controller.onFacade.onInput(event, true, selectedValues);
        }
        else {
            const singleValue = selectedValues.length > 0 ? selectedValues[0] : undefined;
            this._value = singleValue;
            this.controller.onFacade.onInput(event, true, singleValue);
        }
    }
    onChange(event) {
        if (this._multiple) {
            this.controller.onFacade.onChange(event, this._value);
        }
        else {
            this.controller.onFacade.onChange(event, this._value);
        }
    }
    static get is() { return "kol-select-wc"; }
    static get properties() {
        return {
            "_accessKey": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines the key combination that can be used to trigger or focus the component\u2019s interactive element."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_access-key"
            },
            "_disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "TODO",
                            "text": ": Change type back to `DisabledPropType` after Stencil#4663 has been resolved."
                        }],
                    "text": "Makes the element not focusable and ignore all events."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_disabled",
                "defaultValue": "false"
            },
            "_hideMsg": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "TODO",
                            "text": ": Change type back to `HideMsgPropType` after Stencil#4663 has been resolved."
                        }],
                    "text": "Hides the error message but leaves it in the DOM for the input's aria-describedby."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_hide-msg",
                "defaultValue": "false"
            },
            "_hideLabel": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "TODO",
                            "text": ": Change type back to `HideLabelPropType` after Stencil#4663 has been resolved."
                        }],
                    "text": "Hides the caption by default and displays the caption text with a tooltip when the\ninteractive element is focused or the mouse is over it."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_hide-label",
                "defaultValue": "false"
            },
            "_hint": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines the hint text."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_hint",
                "defaultValue": "''"
            },
            "_icons": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "IconsHorizontalPropType",
                    "resolved": "string | undefined | { right?: IconOrIconClass | undefined; left?: IconOrIconClass | undefined; }",
                    "references": {
                        "IconsHorizontalPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::IconsHorizontalPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines the icon classnames (e.g. `_icons=\"fa-solid fa-user\"`)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_icons"
            },
            "_label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "LabelWithExpertSlotPropType",
                    "resolved": "string",
                    "references": {
                        "LabelWithExpertSlotPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::LabelWithExpertSlotPropType"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Defines the visible or semantic label of the component (e.g. aria-label, label, headline, caption, summary, etc.). Set to `false` to enable the expert slot."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_label"
            },
            "_msg": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "Stringified<MsgPropType>",
                    "resolved": "Omit<AlertProps, \"_on\" | \"_label\" | \"_level\" | \"_variant\" | \"_hasCloser\"> & { _description: string; } | string | undefined",
                    "references": {
                        "Stringified": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::Stringified"
                        },
                        "MsgPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::MsgPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines the properties for a message rendered as Alert component."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_msg"
            },
            "_multiple": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "TODO",
                            "text": ": Change type back to `MultiplePropType` after Stencil#4663 has been resolved."
                        }],
                    "text": "Makes the input accept multiple inputs."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_multiple",
                "defaultValue": "false"
            },
            "_name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "NamePropType",
                    "resolved": "string | undefined",
                    "references": {
                        "NamePropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::NamePropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines the technical name of an input field."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_name"
            },
            "_on": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "InputTypeOnDefault",
                    "resolved": "InputTypeOnBlur & InputTypeOnClick & InputTypeOnChange & InputTypeOnFocus & InputTypeOnInput & InputTypeOnKeyDown | undefined",
                    "references": {
                        "InputTypeOnDefault": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::InputTypeOnDefault"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Gibt die EventCallback-Funktionen f\u00FCr das Input-Event an."
                },
                "getter": false,
                "setter": false
            },
            "_options": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "OptionsWithOptgroupPropType",
                    "resolved": "(Option<StencilUnknown> | Optgroup<StencilUnknown>)[] | string",
                    "references": {
                        "OptionsWithOptgroupPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::OptionsWithOptgroupPropType"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Options the user can choose from, also supporting Optgroup."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_options"
            },
            "_required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "TODO",
                            "text": ": Change type back to `RequiredPropType` after Stencil#4663 has been resolved."
                        }],
                    "text": "Makes the input element required."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_required",
                "defaultValue": "false"
            },
            "_shortKey": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "ShortKeyPropType",
                    "resolved": "string | undefined",
                    "references": {
                        "ShortKeyPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::ShortKeyPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Adds a visual shortcut hint after the label and instructs the screen reader to read the shortcut aloud."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_short-key"
            },
            "_rows": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "RowsPropType",
                    "resolved": "number | undefined",
                    "references": {
                        "RowsPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::RowsPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines how many rows of options should be visible at the same time."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_rows"
            },
            "_syncValueBySelector": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "SyncValueBySelectorPropType",
                    "resolved": "string | undefined",
                    "references": {
                        "SyncValueBySelectorPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::SyncValueBySelectorPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "internal",
                            "text": undefined
                        }],
                    "text": "Selector for synchronizing the value with another input element."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_sync-value-by-selector"
            },
            "_tabIndex": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines which tab-index the primary element of the component has. (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex)"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_tab-index"
            },
            "_tooltipAlign": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "TooltipAlignPropType",
                    "resolved": "\"bottom\" | \"left\" | \"right\" | \"top\" | undefined",
                    "references": {
                        "TooltipAlignPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::TooltipAlignPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines where to show the Tooltip preferably: top, right, bottom or left."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_tooltip-align",
                "defaultValue": "'top'"
            },
            "_touched": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "TODO",
                            "text": ": Change type back to `TouchedPropType` after Stencil#4663 has been resolved."
                        }],
                    "text": "Shows if the input was touched by a user."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "_touched",
                "defaultValue": "false"
            },
            "_value": {
                "type": "any",
                "mutable": true,
                "complexType": {
                    "original": "Stringified<StencilUnknown[]> | Stringified<StencilUnknown>",
                    "resolved": "StencilUnknown[] | boolean | null | number | object | string | undefined",
                    "references": {
                        "Stringified": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::Stringified"
                        },
                        "StencilUnknown": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::StencilUnknown"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines the value of the input."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "_value"
            },
            "_variant": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "VariantClassNamePropType",
                    "resolved": "string | undefined",
                    "references": {
                        "VariantClassNamePropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::VariantClassNamePropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines which variant should be used for presentation."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_variant"
            }
        };
    }
    static get states() {
        return {
            "state": {},
            "inputHasFocus": {}
        };
    }
    static get methods() {
        return {
            "getValue": {
                "complexType": {
                    "signature": "() => Promise<StencilUnknown[] | StencilUnknown>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "StencilUnknown": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::StencilUnknown"
                        }
                    },
                    "return": "Promise<StencilUnknown | StencilUnknown[]>"
                },
                "docs": {
                    "text": "Returns the current value.",
                    "tags": []
                }
            },
            "focus": {
                "complexType": {
                    "signature": "(options?: KolFocusOptions) => Promise<void>",
                    "parameters": [{
                            "name": "options",
                            "type": "KolFocusOptions | undefined",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "KolFocusOptions": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::KolFocusOptions"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Sets focus on the internal element.",
                    "tags": []
                }
            },
            "click": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Clicks the primary interactive element inside this component.",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "host"; }
    static get watchers() {
        return [{
                "propName": "_accessKey",
                "methodName": "validateAccessKey"
            }, {
                "propName": "_disabled",
                "methodName": "validateDisabled"
            }, {
                "propName": "_hideMsg",
                "methodName": "validateHideMsg"
            }, {
                "propName": "_hideLabel",
                "methodName": "validateHideLabel"
            }, {
                "propName": "_hint",
                "methodName": "validateHint"
            }, {
                "propName": "_icons",
                "methodName": "validateIcons"
            }, {
                "propName": "_label",
                "methodName": "validateLabel"
            }, {
                "propName": "_msg",
                "methodName": "validateMsg"
            }, {
                "propName": "_multiple",
                "methodName": "validateMultiple"
            }, {
                "propName": "_name",
                "methodName": "validateName"
            }, {
                "propName": "_on",
                "methodName": "validateOn"
            }, {
                "propName": "_options",
                "methodName": "validateOptions"
            }, {
                "propName": "_required",
                "methodName": "validateRequired"
            }, {
                "propName": "_rows",
                "methodName": "validateRows"
            }, {
                "propName": "_shortKey",
                "methodName": "validateShortKey"
            }, {
                "propName": "_syncValueBySelector",
                "methodName": "validateSyncValueBySelector"
            }, {
                "propName": "_tabIndex",
                "methodName": "validateTabIndex"
            }, {
                "propName": "_touched",
                "methodName": "validateTouched"
            }, {
                "propName": "_value",
                "methodName": "validateValue"
            }, {
                "propName": "_variant",
                "methodName": "validateVariant"
            }];
    }
}
__decorate([
    directFocus('ctaRef')
], KolSelectWc.prototype, "focus", null);
__decorate([
    directClick('ctaRef')
], KolSelectWc.prototype, "click", null);
//# sourceMappingURL=component.js.map
