import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-ClISz4NF.js';
import { c as createID } from './string.utils-vN03FcXN.js';
import { v as validateProp } from './props-CJZgTDla.js';
import { g as govForm, h as FormSwitchClass, i as FormSwitchSizes } from './form-DvhJiKwA.js';
import { g as govHost, a as toBoolAttr, t as toBoolAttrIfDefined } from './template-CoD1xawt.js';
import { F as Fragment } from './Fragment-5itQzbON.js';
import { A as AttributeObserver, a as ariaAttributes } from './attribute-Bz_RzYFb.js';
import { i as inheritAriaAttributes } from './accessibility-wqNV8djF.js';
import './gov.log-BtqD9K8d.js';
import './utils-CQvUntGY.js';
import './sizes-BcQO4NH2.js';
import './constants-BegH8u6i.js';
import './colors-U40vKHrs.js';
import './constants-aqe7GX2n.js';
import './constants-CysDgoMA.js';

const GovFormSwitch = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.govFocus = createEvent(this, "gov-focus");
        this.govBlur = createEvent(this, "gov-blur");
        this.govChange = createEvent(this, "gov-change");
        this.hydrated = false;
        /**
         * When you can't use the form label.
         */
        this.noLabel = false;
        /**
         * Switch button state
         */
        this.checked = false;
        /**
         * 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.
         */
        this.required = false;
        /**
         * Makes the switch component disabled.
         */
        this.disabled = false;
        /**
         * Switch's size.
         */
        this.size = "m";
        this.inheritedAttributes = {};
        this.checkboxId = createID("GovCheckbox");
        this.h = govHost(this.host);
        this.f = govForm(this.h);
    }
    validateSize(newValue) {
        validateProp(FormSwitchSizes, newValue, FormSwitchClass.root);
    }
    watchDisabled() {
        this.passControlAttrs();
    }
    passControlAttrs() {
        this.f.passAttrToControl("size", this.size);
        this.f.passAttrToControl("invalid", toBoolAttr(this.invalid));
        this.f.passAttrToControl("disabled", toBoolAttr(this.disabled));
        this.f.passAttrToControl("type", "switch");
    }
    componentDidLoad() {
        this.attributeObserver = new AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(ariaAttributes);
    }
    componentWillLoad() {
        this.passControlAttrs();
        this.inheritedAttributes = inheritAriaAttributes(this.host);
    }
    disconnectedCallback() {
        var _a;
        (_a = this.attributeObserver) === null || _a === void 0 ? void 0 : _a.stopObserving();
    }
    async componentDidRender() {
        this.f.passAttrToLabel("identifier", this.identifier || this.checkboxId);
        this.f.passAttrToLabel("required", String(this.required));
        this.passControlAttrs();
    }
    connectedCallback() {
        this.hydrated = true;
    }
    onAriaAttributeChange(attributeName, newValue) {
        this.host.removeAttribute(attributeName);
        this.inheritedAttributes = Object.assign(Object.assign({}, this.inheritedAttributes), { [attributeName]: newValue });
    }
    render() {
        const wrapSwitch = (children) => {
            if (this.noLabel) {
                return (h("label", { class: FormSwitchClass.label }, children));
            }
            return children;
        };
        const noLabelSlot = !this.host.querySelector('[slot="label"]');
        const hostClasses = `${this.h.classes(FormSwitchClass.root)}${noLabelSlot ? ` ${FormSwitchClass.noLabel}` : ''}`;
        return (h(Host, { class: hostClasses, hydrated: this.hydrated, checked: this.checked, size: this.size, invalid: toBoolAttrIfDefined(this.invalid) }, wrapSwitch(h(Fragment, null, h("input", Object.assign({ class: FormSwitchClass.input, id: this.identifier || this.checkboxId, type: "checkbox", value: this.value, name: this.name, disabled: this.disabled, checked: this.checked, onClick: this.onClickHandler.bind(this), onChange: this.onChangeHandler.bind(this), onFocus: this.onFocusHandler.bind(this), onBlur: this.onBlurHandler.bind(this), ref: el => (this.inputRef = el), required: this.required }, this.inheritedAttributes, { "aria-checked": this.checked, "aria-required": toBoolAttrIfDefined(this.required), "aria-invalid": toBoolAttrIfDefined(this.invalid), "aria-disabled": toBoolAttrIfDefined(this.disabled) })), h("span", null, h("span", { class: FormSwitchClass.indicator }, this.checked && h("gov-icon", { name: "check-lg", type: "components" })), h("slot", { name: "label" }))))));
    }
    onClickHandler(e) {
        e.stopPropagation();
    }
    onFocusHandler(e) {
        e.stopPropagation();
        this.govFocus.emit({
            component: FormSwitchClass.root,
            checked: this.checked,
            value: this.value,
            originalEvent: e,
        });
    }
    onBlurHandler(e) {
        e.stopPropagation();
        this.govBlur.emit({
            component: FormSwitchClass.root,
            checked: this.checked,
            value: this.value,
            originalEvent: e,
        });
    }
    onChangeHandler(e) {
        this.checked = e.target.checked;
        e.stopPropagation();
        this.govChange.emit({
            component: FormSwitchClass.root,
            checked: this.checked,
            value: this.value,
            originalEvent: e,
        });
    }
    /**
     * Returns the reference of the native element
     */
    async getRef() {
        return this.inputRef;
    }
    get host() { return getElement(this); }
    static get watchers() { return {
        "size": ["validateSize", "watchDisabled"],
        "disabled": ["watchDisabled"],
        "invalid": ["watchDisabled"]
    }; }
};

export { GovFormSwitch as gov_form_switch };
//# sourceMappingURL=gov-form-switch.entry.js.map

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