'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 GovFormRadio = 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;
        /**
         * When you can't use the form label.
         */
        this.noLabel = false;
        /**
         * Radio 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 radio component disabled.
         * This prevents users from being able to interact with the radio, and conveys its inactive state to assistive technologies.
         */
        this.disabled = false;
        /**
         * Radio's size.
         */
        this.size = "m";
        this.inheritedAttributes = {};
        this.radioId = string_utils.createID("GovRadio");
        this.h = template.govHost(this.host);
        this.f = form.govForm(this.h);
    }
    validateSize(newValue) {
        props.validateProp(form.FormRadioSizes, newValue, form.FormRadioClass.root);
    }
    watchControlAttrs() {
        this.passControlAttrs();
    }
    passControlAttrs() {
        this.f.passAttrToControl("size", this.size);
        this.f.passAttrToControl("invalid", template.toBoolAttr(this.invalid));
        this.f.passAttrToControl("disabled", template.toBoolAttr(this.disabled));
        this.f.passAttrToControl("type", "radio");
    }
    componentWillLoad() {
        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.radioId);
        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 wrapRadio = (children) => {
            if (this.noLabel) {
                return index.h("label", { class: form.FormRadioClass.label }, children);
            }
            return children;
        };
        return (index.h(index.Host, { class: form.FormRadioClass.root, hydrated: this.hydrated, checked: this.checked, size: this.size, invalid: template.toBoolAttrIfDefined(this.invalid) }, wrapRadio(index.h(Fragment.Fragment, null, index.h("input", Object.assign({ class: form.FormRadioClass.input, id: this.identifier || this.radioId, type: "radio", value: this.value, name: this.name, checked: this.checked, disabled: this.disabled, 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": 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" }))))));
    }
    onClickHandler(e) {
        e.stopPropagation();
    }
    onFocusHandler(e) {
        e.stopPropagation();
        this.govFocus.emit({
            component: form.FormRadioClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    onBlurHandler(e) {
        e.stopPropagation();
        this.govBlur.emit({
            component: form.FormRadioClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    onChangeHandler(e) {
        this.checked = e.target.checked;
        e.stopPropagation();
        this.govChange.emit({
            component: form.FormRadioClass.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_radio = GovFormRadio;
//# sourceMappingURL=gov-form-radio.entry.cjs.js.map

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