import { h, Host } from "@stencil/core";
import { createID } from "../../../utils/string.utils";
import { validateProp } from "../../../helpers/Validate/props";
import { FormRadioClass, FormRadioSizes } from "./constants";
import { govHost, toBoolAttr, toBoolAttrIfDefined } from "../../../helpers/Dom/template";
import { govForm } from "../../../helpers/Dom/form";
import Fragment from "../../../helpers/Fragment";
import { AttributeObserver } from "../../../helpers/Dom/attribute";
import { inheritAriaAttributes } from "../../../helpers/Accessibility/accessibility";
import { ariaAttributes } from "../../../helpers/Accessibility/attributes";
export class GovFormRadio {
    constructor() {
        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 = createID("GovRadio");
        this.h = govHost(this.host);
        this.f = govForm(this.h);
    }
    validateSize(newValue) {
        validateProp(FormRadioSizes, newValue, FormRadioClass.root);
    }
    watchControlAttrs() {
        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", "radio");
    }
    componentWillLoad() {
        this.inheritedAttributes = inheritAriaAttributes(this.host);
    }
    componentDidLoad() {
        this.attributeObserver = new AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(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 h("label", { class: FormRadioClass.label }, children);
            }
            return children;
        };
        return (h(Host, { class: FormRadioClass.root, hydrated: this.hydrated, checked: this.checked, size: this.size, invalid: toBoolAttrIfDefined(this.invalid) }, wrapRadio(h(Fragment, null, h("input", Object.assign({ class: 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": toBoolAttrIfDefined(this.required), "aria-invalid": toBoolAttrIfDefined(this.invalid), "aria-disabled": toBoolAttrIfDefined(this.disabled) })), h("span", null, h("slot", { name: "label" }))))));
    }
    onClickHandler(e) {
        e.stopPropagation();
    }
    onFocusHandler(e) {
        e.stopPropagation();
        this.govFocus.emit({
            component: FormRadioClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    onBlurHandler(e) {
        e.stopPropagation();
        this.govBlur.emit({
            component: FormRadioClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    onChangeHandler(e) {
        this.checked = e.target.checked;
        e.stopPropagation();
        this.govChange.emit({
            component: FormRadioClass.root,
            value: this.value,
            checked: this.checked,
            originalEvent: e,
        });
    }
    /**
     * Returns the reference of the native element
     */
    async getRef() {
        return this.inputRef;
    }
    static get is() { return "gov-form-radio"; }
    static get properties() {
        return {
            "value": {
                "type": "string",
                "attribute": "value",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Value of radio"
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "noLabel": {
                "type": "boolean",
                "attribute": "no-label",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "When you can't use the form label."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "false"
            },
            "checked": {
                "type": "boolean",
                "attribute": "checked",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Radio button state"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "defaultValue": "false"
            },
            "required": {
                "type": "boolean",
                "attribute": "required",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "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.\r\nWhen using this property you need to also set \u201Cnovalidate\u201D attribute to your form element to prevent browser from displaying its own validation errors."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "false"
            },
            "disabled": {
                "type": "boolean",
                "attribute": "disabled",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Makes the radio component disabled.\r\nThis prevents users from being able to interact with the radio, and conveys its inactive state to assistive technologies."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "defaultValue": "false"
            },
            "name": {
                "type": "string",
                "attribute": "name",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Name of the radio."
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "size": {
                "type": "string",
                "attribute": "size",
                "mutable": false,
                "complexType": {
                    "original": "FormRadioSizesType",
                    "resolved": "\"l\" | \"m\" | \"s\" | \"xl\" | \"xs\"",
                    "references": {
                        "FormRadioSizesType": {
                            "location": "import",
                            "path": "./constants",
                            "id": "../../../packages/core/src/components/gov-form/radio/constants.ts::FormRadioSizesType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Radio's size."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "\"m\""
            },
            "identifier": {
                "type": "string",
                "attribute": "identifier",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Custom radio identifier."
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "invalid": {
                "type": "boolean",
                "attribute": "invalid",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Indicates the entered value does not conform to the format expected by the application."
                },
                "getter": false,
                "setter": false,
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "inheritedAttributes": {}
        };
    }
    static get events() {
        return [{
                "method": "govFocus",
                "name": "gov-focus",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the radio has focus."
                },
                "complexType": {
                    "original": "FormRadioEvent",
                    "resolved": "FormRadioEvent",
                    "references": {
                        "FormRadioEvent": {
                            "location": "import",
                            "path": "./gov-form-radio.types",
                            "id": "../../../packages/core/src/components/gov-form/radio/gov-form-radio.types.ts::FormRadioEvent"
                        }
                    }
                }
            }, {
                "method": "govBlur",
                "name": "gov-blur",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the radio loses focus."
                },
                "complexType": {
                    "original": "FormRadioEvent",
                    "resolved": "FormRadioEvent",
                    "references": {
                        "FormRadioEvent": {
                            "location": "import",
                            "path": "./gov-form-radio.types",
                            "id": "../../../packages/core/src/components/gov-form/radio/gov-form-radio.types.ts::FormRadioEvent"
                        }
                    }
                }
            }, {
                "method": "govChange",
                "name": "gov-change",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the radio change value."
                },
                "complexType": {
                    "original": "FormRadioEvent",
                    "resolved": "FormRadioEvent",
                    "references": {
                        "FormRadioEvent": {
                            "location": "import",
                            "path": "./gov-form-radio.types",
                            "id": "../../../packages/core/src/components/gov-form/radio/gov-form-radio.types.ts::FormRadioEvent"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "getRef": {
                "complexType": {
                    "signature": "() => Promise<HTMLInputElement>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "HTMLInputElement": {
                            "location": "global",
                            "id": "global::HTMLInputElement"
                        }
                    },
                    "return": "Promise<HTMLInputElement>"
                },
                "docs": {
                    "text": "Returns the reference of the native element",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "host"; }
    static get watchers() {
        return [{
                "propName": "size",
                "methodName": "validateSize"
            }, {
                "propName": "disabled",
                "methodName": "watchControlAttrs"
            }, {
                "propName": "invalid",
                "methodName": "watchControlAttrs"
            }, {
                "propName": "size",
                "methodName": "watchControlAttrs"
            }];
    }
}
//# sourceMappingURL=gov-form-radio.js.map
