import { h, Host } from "@stencil/core";
import { govForm } from "../../../helpers/Dom/form";
import { govHost, toBoolAttr, toBoolAttrIfDefined } from "../../../helpers/Dom/template";
import { validateProp } from "../../../helpers/Validate/props";
import { createID } from "../../../utils/string.utils";
import { FormSelectClass, FormSelectSizes, } from "./constants";
import { propsValueToArray } from "../../../utils/array.utils";
import { tick } from "../../../utils/stencil.utils";
import { AttributeObserver } from "../../../helpers/Dom/attribute";
import { ariaAttributes } from "../../../helpers/Accessibility/attributes";
import { inheritAriaAttributes } from "../../../helpers/Accessibility/accessibility";
export class GovFormSelect {
    constructor() {
        /**
         * Select’s size.
         */
        this.size = 'm';
        /**
         * Set whether the input is required or not. Please note that this is necessary for accessible inputs when the user is required to fill them.
         * When using this property you need to also set “novalidate” attribute to your form element to prevent browser from displaying its own validation errors.
         */
        this.required = false;
        /**
         * Makes the select component disabled.
         * This prevents users from being able to interact with the select, and conveys its inactive state to assistive technologies.
         */
        this.disabled = false;
        this.optionsState = [];
        this.inheritedAttributes = {};
        this.h = govHost(this.host);
        this.f = govForm(this.h);
        this.selectId = createID('GovSelect');
    }
    validateSize(newValue) {
        validateProp(FormSelectSizes, newValue, FormSelectClass.root);
    }
    watchOptions(newValue) {
        this.optionsState = propsValueToArray(newValue);
    }
    watchControlAttrs() {
        this.passControlAttrs();
    }
    watchValue() {
        this.markSelectedValue();
    }
    passControlAttrs() {
        this.f.passAttrToControl('size', this.size);
        this.f.passAttrToControl('invalid', toBoolAttr(this.invalid));
        this.f.passAttrToControl('success', toBoolAttr(this.success));
        this.f.passAttrToControl('disabled', toBoolAttr(this.disabled));
        this.f.passAttrToControl('type', 'select');
    }
    componentDidLoad() {
        this.attributeObserver = new AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(ariaAttributes);
    }
    disconnectedCallback() {
        var _a;
        (_a = this.attributeObserver) === null || _a === void 0 ? void 0 : _a.stopObserving();
    }
    componentWillLoad() {
        this.validateSize(this.size);
        this.markSelectedValue();
        this.optionsState = propsValueToArray(this.options);
        this.inheritedAttributes = inheritAriaAttributes(this.host);
    }
    async componentDidRender() {
        this.passControlAttrs();
        this.f.passAttrToLabel('required', String(this.required));
        tick(() => this.markSelectedValue());
    }
    onAriaAttributeChange(attributeName, newValue) {
        this.host.removeAttribute(attributeName);
        this.inheritedAttributes = Object.assign(Object.assign({}, this.inheritedAttributes), { [attributeName]: newValue });
    }
    markSelectedValue() {
        this.host.querySelectorAll('option').forEach((option) => {
            if (option.value === this.value) {
                option.setAttribute('selected', 'selected');
            }
            else {
                option.removeAttribute('selected');
            }
        });
    }
    render() {
        return (h(Host, { key: '784373d222b67be86b39d3ed61bfdf81d9648a36', class: FormSelectClass.root, size: this.size }, this.h.hasSlot('prefix') && h("slot", { key: '23b99e07df8d0d135c93bdd8c14556de4b4a5433', name: "prefix" }), h("span", { key: 'f4c931f58a53db5cc2f8379b2dc6b688bbe84bc8', class: "element" }, h("select", Object.assign({ key: 'e259f55116a12a7b821fac43db61b9ed35457c17', id: this.identifier || this.selectId, onFocus: this.onFocusHandler.bind(this), onChange: this.onChangeHandler.bind(this), onBlur: this.onBlurHandler.bind(this), required: this.required, name: this.name, disabled: this.disabled, ref: el => (this.selectRef = el) }, this.inheritedAttributes, { "aria-disabled": toBoolAttrIfDefined(this.disabled), "aria-required": toBoolAttrIfDefined(this.required), "aria-invalid": toBoolAttrIfDefined(this.invalid) }), h("slot", { key: '223133e692d0c7a4c2c9c4706aee2d447f37c37b' }), Array.isArray(this.optionsState) && this.optionsState.map((option) => {
            var _a;
            return (h("option", { value: option.value, selected: option.value === this.value, disabled: option === null || option === void 0 ? void 0 : option.disabled }, (_a = option.label) !== null && _a !== void 0 ? _a : option.value));
        })), this.success && h("gov-icon", { key: '97ceacc7ec59f13a9d994dc6df3b58201b5f68ae', class: "validation-icon", name: "check-lg", type: "components" }), this.invalid && h("gov-icon", { key: 'bd9f33ef6da150e5a3f4a5711d41282fe35df49f', class: "validation-icon", name: "exclamation-lg", type: "components" }), h("gov-icon", { key: 'c5ca08441cb8f425a4e310e402d6f208c8a75e15', class: "arrow-icon", name: "chevron-down", type: "components" })), this.h.hasSlot('sufix') && h("slot", { key: 'af16a1bd05ed5d665888cea9a811eee2c0fe5749', name: "sufix" })));
    }
    onFocusHandler(e) {
        this.focused = true;
        this.govFocus.emit({
            component: FormSelectClass.root,
            originalEvent: e,
            value: this.value,
        });
    }
    onBlurHandler(e) {
        this.focused = false;
        this.govBlur.emit({
            component: FormSelectClass.root,
            originalEvent: e,
            value: this.value,
        });
    }
    onChangeHandler(e) {
        this.value = e.target.value;
        e.stopPropagation();
        this.govChange.emit({
            component: FormSelectClass.root,
            originalEvent: e,
            value: this.value,
        });
    }
    /**
     * Returns the current state of the component
     */
    async setFocus(options) {
        return this.selectRef.focus(options);
    }
    /**
     * Returns an instance of the native html select element
     */
    async getRef() {
        return this.selectRef;
    }
    static get is() { return "gov-form-select"; }
    static get properties() {
        return {
            "value": {
                "type": "string",
                "attribute": "value",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Value of select"
                },
                "getter": false,
                "setter": false,
                "reflect": true
            },
            "options": {
                "type": "string",
                "attribute": "options",
                "mutable": true,
                "complexType": {
                    "original": "string | FormSelectOption[]",
                    "resolved": "FormSelectOption[] | string",
                    "references": {
                        "FormSelectOption": {
                            "location": "import",
                            "path": "./gov-form-select.types",
                            "id": "../../../packages/core/src/components/gov-form/select/gov-form-select.types.ts::FormSelectOption"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Options of select"
                },
                "getter": false,
                "setter": false,
                "reflect": true
            },
            "identifier": {
                "type": "string",
                "attribute": "identifier",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Custom select identifier."
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "size": {
                "type": "string",
                "attribute": "size",
                "mutable": false,
                "complexType": {
                    "original": "FormSelectSizesType",
                    "resolved": "\"l\" | \"m\" | \"s\" | \"xl\" | \"xs\"",
                    "references": {
                        "FormSelectSizesType": {
                            "location": "import",
                            "path": "./constants",
                            "id": "../../../packages/core/src/components/gov-form/select/constants.ts::FormSelectSizesType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Select\u2019s size."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "'m'"
            },
            "name": {
                "type": "string",
                "attribute": "name",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Name of the select."
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "required": {
                "type": "boolean",
                "attribute": "required",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Set whether the input is required or not. Please note that this is necessary for accessible inputs when the user is required to fill them.\r\nWhen using this property you need to also set \u201Cnovalidate\u201D attribute to your form element to prevent browser from displaying its own validation errors."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "false"
            },
            "success": {
                "type": "boolean",
                "attribute": "success",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Indicates the entered value of child form element does conform to the format expected by the application."
                },
                "getter": false,
                "setter": false,
                "reflect": true
            },
            "disabled": {
                "type": "boolean",
                "attribute": "disabled",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Makes the select component disabled.\r\nThis prevents users from being able to interact with the select, and conveys its inactive state to assistive technologies."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "defaultValue": "false"
            },
            "invalid": {
                "type": "boolean",
                "attribute": "invalid",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Indicates the entered value does not conform to the format expected by the application."
                },
                "getter": false,
                "setter": false,
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "focused": {},
            "optionsState": {},
            "inheritedAttributes": {}
        };
    }
    static get events() {
        return [{
                "method": "govFocus",
                "name": "gov-focus",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the select has focus."
                },
                "complexType": {
                    "original": "FormSelectEvent",
                    "resolved": "FormSelectEvent",
                    "references": {
                        "FormSelectEvent": {
                            "location": "import",
                            "path": "./gov-form-select.types",
                            "id": "../../../packages/core/src/components/gov-form/select/gov-form-select.types.ts::FormSelectEvent"
                        }
                    }
                }
            }, {
                "method": "govBlur",
                "name": "gov-blur",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the select loses focus."
                },
                "complexType": {
                    "original": "FormSelectEvent",
                    "resolved": "FormSelectEvent",
                    "references": {
                        "FormSelectEvent": {
                            "location": "import",
                            "path": "./gov-form-select.types",
                            "id": "../../../packages/core/src/components/gov-form/select/gov-form-select.types.ts::FormSelectEvent"
                        }
                    }
                }
            }, {
                "method": "govChange",
                "name": "gov-change",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the select change value."
                },
                "complexType": {
                    "original": "FormSelectEvent",
                    "resolved": "FormSelectEvent",
                    "references": {
                        "FormSelectEvent": {
                            "location": "import",
                            "path": "./gov-form-select.types",
                            "id": "../../../packages/core/src/components/gov-form/select/gov-form-select.types.ts::FormSelectEvent"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "setFocus": {
                "complexType": {
                    "signature": "(options?: FocusOptions) => Promise<void>",
                    "parameters": [{
                            "name": "options",
                            "type": "FocusOptions",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "FocusOptions": {
                            "location": "global",
                            "id": "global::FocusOptions"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Returns the current state of the component",
                    "tags": []
                }
            },
            "getRef": {
                "complexType": {
                    "signature": "() => Promise<HTMLSelectElement>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "HTMLSelectElement": {
                            "location": "global",
                            "id": "global::HTMLSelectElement"
                        }
                    },
                    "return": "Promise<HTMLSelectElement>"
                },
                "docs": {
                    "text": "Returns an instance of the native html select element",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "host"; }
    static get watchers() {
        return [{
                "propName": "size",
                "methodName": "validateSize"
            }, {
                "propName": "options",
                "methodName": "watchOptions"
            }, {
                "propName": "disabled",
                "methodName": "watchControlAttrs"
            }, {
                "propName": "invalid",
                "methodName": "watchControlAttrs"
            }, {
                "propName": "success",
                "methodName": "watchControlAttrs"
            }, {
                "propName": "size",
                "methodName": "watchControlAttrs"
            }, {
                "propName": "value",
                "methodName": "watchValue"
            }];
    }
}
//# sourceMappingURL=gov-form-select.js.map
