import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-ClISz4NF.js';
import { g as govForm, j as FormSelectClass, k as FormSelectSizes } from './form-DvhJiKwA.js';
import { g as govHost, a as toBoolAttr, t as toBoolAttrIfDefined } from './template-CoD1xawt.js';
import { v as validateProp } from './props-CJZgTDla.js';
import { c as createID } from './string.utils-vN03FcXN.js';
import { p as propsValueToArray } from './array.utils-CJ0U_ONv.js';
import { t as tick } from './stencil.utils-Cjg0wpDG.js';
import { A as AttributeObserver, a as ariaAttributes } from './attribute-Bz_RzYFb.js';
import { i as inheritAriaAttributes } from './accessibility-wqNV8djF.js';
import './sizes-BcQO4NH2.js';
import './utils-CQvUntGY.js';
import './constants-BegH8u6i.js';
import './colors-U40vKHrs.js';
import './constants-aqe7GX2n.js';
import './constants-CysDgoMA.js';
import './gov.log-BtqD9K8d.js';
import './helpers.types-DNztiAH0.js';

const GovFormSelect = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.govFocus = createEvent(this, "gov-focus");
        this.govBlur = createEvent(this, "gov-blur");
        this.govChange = createEvent(this, "gov-change");
        /**
         * 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;
    }
    get host() { return getElement(this); }
    static get watchers() { return {
        "size": ["validateSize", "watchControlAttrs"],
        "options": ["watchOptions"],
        "disabled": ["watchControlAttrs"],
        "invalid": ["watchControlAttrs"],
        "success": ["watchControlAttrs"],
        "value": ["watchValue"]
    }; }
};

export { GovFormSelect as gov_form_select };
//# sourceMappingURL=gov-form-select.entry.js.map

//# sourceMappingURL=gov-form-select.entry.js.map