'use strict';

var index = require('./index-BEF5cZxd.js');
var form = require('./form-GgXzZZ-E.js');
var template = require('./template-Cj6PcLmR.js');
var string_utils = require('./string.utils-BG4pUfEi.js');
var helpers_types = require('./helpers.types-vap8QfRH.js');
require('./sizes-D5zsw2g5.js');
require('./utils-NnC78QR3.js');
require('./constants-DcKnOyW4.js');
require('./colors-DgrrxjsX.js');
require('./constants-CFAhiwqu.js');
require('./constants-357ehBFU.js');

const GovFormControl = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        /**
         * Indicates the entered value of a child form element does not conform to the format expected by the application.
         */
        this.invalid = false;
        /**
        /**
        * Indicates the form control is disabled
         */
        this.disabled = false;
        /**
        /**
        * Indicates the entered value of a child form element does conform to the format expected by the application.
         */
        this.success = false;
        /**
         * Use control as a fieldset for better accessibility.
         * Note: Remember to set the legend attribute of the gov-form-label component
         */
        this.fieldset = false;
        /**
         * Form element size.
         */
        this.size = "m";
        this.h = template.govHost(this.host);
        this.f = form.govForm(this.h);
    }
    componentDidRender() {
        this.syncControlMessages();
        this.formElementDescriptionControl();
    }
    disconnectedCallback() {
        var _a;
        (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
    }
    formElementDescriptionControl() {
        const bottomSlot = this.host.querySelector("." + form.FormControlClass.bottom);
        if (!bottomSlot)
            return;
        this.observer = new MutationObserver(function () {
            this.syncControlMessages();
        }.bind(this));
        this.observer.observe(bottomSlot, { childList: true });
    }
    syncControlMessages() {
        Promise.all(this.f.messageElements.map((message) => message.identifier())).then((ids) => {
            this.f.mainElementsInControl.forEach((element) => {
                const currentValue = string_utils.splitByWhitespace(element.getAttribute("aria-describedby"));
                element.removeAttribute("aria-describedby");
                if (Array.isArray(currentValue)) {
                    currentValue.forEach(id => (helpers_types.isString(id) && id.indexOf("GovFormMessage") === -1 ? ids.push(id) : null));
                }
                if (ids.length) {
                    element.setAttribute("aria-describedby", ids.join(" "));
                }
            });
        });
    }
    render() {
        const Tag = this.fieldset ? "fieldset" : "div";
        return (index.h(index.Host, { key: '04e4ab3533f5d22939573fa20a354a0b76319986', size: this.size, class: this.h.classes(form.FormControlClass.root) }, index.h(Tag, { key: '9851627baf138775fe24a673d6a095fc8980b6e9', class: form.FormControlClass.holder }, this.h.hasSlot("top") && index.h("slot", { key: 'beffe147db943648861b9775dee76ef6b20dffa3', name: "top" }), index.h("slot", { key: '6667b41d2720045a93b10a92ef300e2842ff57b4' }), this.h.hasSlot("bottom") && (index.h("div", { key: 'fe117fdc7d64b3c2b4e18e95d1f213c8a8fe3766', class: form.FormControlClass.bottom }, index.h("slot", { key: '07eabb48c2849d624bed7f59ba14811ed35732fb', name: "bottom" }))))));
    }
    get host() { return index.getElement(this); }
};

exports.gov_form_control = GovFormControl;
//# sourceMappingURL=gov-form-control.entry.cjs.js.map

//# sourceMappingURL=gov-form-control.cjs.entry.js.map