'use strict';

var index = require('./index-BEF5cZxd.js');
var string_utils = require('./string.utils-BG4pUfEi.js');
var props = require('./props-CPENhBnM.js');
var form = require('./form-GgXzZZ-E.js');
var template = require('./template-Cj6PcLmR.js');
var Fragment = require('./Fragment-D4qSkFah.js');
var attribute = require('./attribute-CH1-zDDl.js');
var accessibility = require('./accessibility-BQJmIVWV.js');
require('./gov.log-C5EH15b_.js');
require('./utils-NnC78QR3.js');
require('./sizes-D5zsw2g5.js');
require('./constants-DcKnOyW4.js');
require('./colors-DgrrxjsX.js');
require('./constants-CFAhiwqu.js');
require('./constants-357ehBFU.js');

const GovFormCheckbox = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.govFocus = index.createEvent(this, "gov-focus");
        this.govBlur = index.createEvent(this, "gov-blur");
        this.govChange = index.createEvent(this, "gov-change");
        this.hydrated = false;
        /**
         * Checkbox 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.
         * 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 checkbox component disabled.
         */
        this.disabled = false;
        /**
         * Same as original parameter (https://developer.mozilla.org/en-US/docs/Web/CSS/:indeterminate)
         */
        this.indeterminate = false;
        /**
         * Checkboxes size.
         */
        this.size = "m";
        /**
         * When you can't use the form label.
         */
        this.noLabel = false;
        this.inheritedAttributes = {};
        this.checkboxId = string_utils.createID("GovCheckbox");
        this.h = template.govHost(this.host);
        this.f = form.govForm(this.h);
    }
    validateSize(newValue) {
        props.validateProp(form.FormCheckboxSizes, newValue, form.FormCheckboxClass.root);
    }
    watchControlAttrs() {
        this.passControlAttrs();
    }
    passControlAttrs() {
        this.f.passAttrToControl("size", this.size);
        this.f.passAttrToControl("disabled", template.toBoolAttr(this.disabled));
        this.f.passAttrToControl("invalid", template.toBoolAttr(this.invalid));
        this.f.passAttrToControl("type", "checkbox");
    }
    componentWillLoad() {
        this.passControlAttrs();
        this.inheritedAttributes = accessibility.inheritAriaAttributes(this.host);
    }
    componentDidLoad() {
        this.attributeObserver = new attribute.AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(attribute.ariaAttributes);
    }
    async componentDidRender() {
        this.f.passAttrToLabel("identifier", this.identifier || this.checkboxId);
        this.f.passAttrToLabel("required", String(this.required));
        this.passControlAttrs();
    }
    connectedCallback() {
        this.hydrated = true;
    }
    disconnectedCallback() {
        var _a;
        (_a = this.attributeObserver) === null || _a === void 0 ? void 0 : _a.stopObserving();
    }
    onAriaAttributeChange(attributeName, newValue) {
        this.host.removeAttribute(attributeName);
        this.inheritedAttributes = Object.assign(Object.assign({}, this.inheritedAttributes), { [attributeName]: newValue });
    }
    render() {
        const wrapCheckbox = (children) => {
            if (this.noLabel) {
                return (index.h("label", { class: form.FormCheckboxClass.label }, children));
            }
            return children;
        };
        return (index.h(index.Host, { class: form.FormCheckboxClass.root, hydrated: this.hydrated, checked: this.checked, size: this.size, invalid: template.toBoolAttrIfDefined(this.invalid) }, wrapCheckbox(index.h(Fragment.Fragment, null, index.h("input", Object.assign({ id: this.identifier || this.checkboxId, type: "checkbox", value: this.value, name: this.name, checked: this.checked, onClick: this.onClickHandler.bind(this), onChange: this.onChangeHandler.bind(this), onFocus: this.onFocusHandler.bind(this), onBlur: this.onBlurHandler.bind(this), required: this.required, disabled: this.disabled, indeterminate: this.indeterminate, ref: el => (this.inputRef = el) }, this.inheritedAttributes, { "aria-checked": template.toBoolAttr(this.checked), "aria-required": template.toBoolAttrIfDefined(this.required), "aria-invalid": template.toBoolAttrIfDefined(this.invalid), "aria-disabled": template.toBoolAttrIfDefined(this.disabled) })), index.h("span", null, index.h("slot", { name: "label" }), index.h("gov-icon", { class: form.FormCheckboxClass.check, name: "check-lg", type: "components" }), index.h("gov-icon", { class: form.FormCheckboxClass.indeterminate, name: "dash-lg", type: "components" }))))));
    }
    onFocusHandler(e) {
        e.stopPropagation();
        this.govFocus.emit({
            component: form.FormCheckboxClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    onBlurHandler(e) {
        e.stopPropagation();
        this.govBlur.emit({
            component: form.FormCheckboxClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    onClickHandler(e) {
        e.stopPropagation();
    }
    onChangeHandler(e) {
        this.checked = e.target.checked;
        e.stopPropagation();
        this.govChange.emit({
            component: form.FormCheckboxClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    /**
     * Returns the reference of the native element
     */
    async getRef() {
        return this.inputRef;
    }
    get host() { return index.getElement(this); }
    static get watchers() { return {
        "size": ["validateSize", "watchControlAttrs"],
        "disabled": ["watchControlAttrs"],
        "invalid": ["watchControlAttrs"]
    }; }
};

exports.gov_form_checkbox = GovFormCheckbox;
//# sourceMappingURL=gov-form-checkbox.entry.cjs.js.map

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