import { Host, h } from "@stencil/core";
import { cls, getId } from "../../utils/helpers";
export class XInputRadio {
    constructor() {
        /**
         * Element ID for internal reference
         */
        this.inputId = getId('radio');
        this.name = undefined;
        this.required = undefined;
        this.bordered = undefined;
        this.loading = undefined;
        this.autofocus = undefined;
        this.validate = undefined;
        this.inverted = undefined;
        this.disabled = undefined;
        this.checked = undefined;
        this.value = null;
        this.isFocused = undefined;
        this.isLoading = undefined;
        this.isValid = undefined;
    }
    /**
     * Element on click event toogle state
     */
    onClick() {
        this.checked = true;
        document.querySelectorAll(`[name="${this.name}"]`).forEach((el) => {
            if (el.tagName.toLowerCase() === 'input' && this.inputId != el.id) {
                el.closest('x-input-radio').checked = false;
            }
            if (el.tagName.toLowerCase() === 'x-input-radio' && this.inputId != el.querySelector('input').id) {
                el.checked = false;
            }
        });
    }
    /**
     * Element on mouse up event
     */
    onMouseUp() {
        this.onBlurInput();
    }
    /**
     * Element change state
     */
    onChangeState(checked) {
        this.internals.setFormValue(checked ? this.value.toString() : null);
        this.el.dispatchEvent(new CustomEvent('input'));
        this.onValidate();
    }
    /**
     * Validate the input and get the validation result
    */
    async checkValidity() {
        this.onValidate();
        return this.isValid;
    }
    /**
     * This is called when the 'disabled' attribute of the element or or an ancestor <fieldset> is updated
     *
     * @param disabled
     */
    formDisabledCallback(disabled) {
        this.disabled = disabled;
    }
    /**
     * This is called when the parent/owner form is reset
     */
    formResetCallback() {
        this.checked = false;
    }
    /**
     * This is called on component did load
     */
    componentWillLoad() {
        this.onValidate();
    }
    /**
     * Validate the field input
     */
    onValidate() {
        if (this.name.length > 0) {
            let valid = false;
            let required = false;
            document.querySelectorAll(`x-input-radio[name="${this.name}"]`).forEach((el) => {
                if (el.checked) {
                    valid = true;
                }
                if (el.required) {
                    required = true;
                }
            });
            this.isValid = valid || !required;
        }
        else {
            this.isValid = this.required && !this.checked;
        }
        const input = this.el.querySelector('input') || document.getElementById(this.inputId);
        if (input) {
            this.validate && this.onValidateInputEvent.emit(input);
        }
    }
    /**
     * This called on input field is unfocused
     */
    onBlurInput() {
        this.isFocused = false;
        setTimeout(() => this.onValidate(), 50);
    }
    /**
     * This called on input field is focused
     */
    onFocusInput() {
        this.isFocused = true;
    }
    /**
     * Render component
     */
    render() {
        return h(Host, { key: 'abd051122a0c29ad93b03bac12b87bbf7be46223', class: cls({
                "focus": this.isFocused,
                "loading": this.loading || this.isLoading,
                "disabled": this.disabled,
                "required": this.required,
                "bordered": this.bordered,
                "checked": this.checked,
                "inverted": this.inverted,
            }), "data-for": this.name, tabindex: "0", onBlur: () => this.onBlurInput(), onFocus: () => this.onFocusInput() }, h("label", { key: '7f155347931f9d111b171d6e8815a8efc79a9457' }, h("span", { key: '42ba268f4cb747786826ed540001e739173574ab', class: cls({
                "radio": true,
                "checked": this.checked,
            }) }), this.loading && h("div", { key: 'e17edd8a30e7d380d0117483771aa8c70ae1f9c6', class: "loading-indicator" })), h("div", { key: '5836e85fe52dea7969f788e3062c2965e61ca9e7' }, h("slot", { key: '04af37ce3d80c8b9ac2df7803295cadb54539095' })), h("input", { key: 'ceecfdabfd240ed30bec105a4d836e27166c5cbd', type: "radio", id: this.inputId, required: this.required && !this.isValid }));
    }
    static get is() { return "x-input-radio"; }
    static get formAssociated() { return true; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input name for forms keys"
                },
                "attribute": "name",
                "reflect": false
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input required for forms validations"
                },
                "attribute": "required",
                "reflect": false
            },
            "bordered": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input bordered"
                },
                "attribute": "bordered",
                "reflect": false
            },
            "loading": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element show loading and block for edit"
                },
                "attribute": "loading",
                "reflect": false
            },
            "autofocus": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element autofocus on render"
                },
                "attribute": "autofocus",
                "reflect": false
            },
            "validate": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element enable internal validation"
                },
                "attribute": "validate",
                "reflect": false
            },
            "inverted": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element suffle to right position"
                },
                "attribute": "inverted",
                "reflect": false
            },
            "disabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element disabled state"
                },
                "attribute": "disabled",
                "reflect": false
            },
            "checked": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element is active, selected or checked"
                },
                "attribute": "checked",
                "reflect": false
            },
            "value": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string | null",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input value"
                },
                "attribute": "value",
                "reflect": false,
                "defaultValue": "null"
            }
        };
    }
    static get states() {
        return {
            "isFocused": {},
            "isLoading": {},
            "isValid": {}
        };
    }
    static get events() {
        return [{
                "method": "onValidateInputEvent",
                "name": "validated",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On validate input event"
                },
                "complexType": {
                    "original": "HTMLInputElement",
                    "resolved": "HTMLInputElement",
                    "references": {
                        "HTMLInputElement": {
                            "location": "global",
                            "id": "global::HTMLInputElement"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "checkValidity": {
                "complexType": {
                    "signature": "() => Promise<boolean>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<boolean>"
                },
                "docs": {
                    "text": "Validate the input and get the validation result",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "checked",
                "methodName": "onChangeState"
            }];
    }
    static get listeners() {
        return [{
                "name": "click",
                "method": "onClick",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "mouseup",
                "method": "onMouseUp",
                "target": undefined,
                "capture": false,
                "passive": true
            }];
    }
    static get attachInternalsMemberName() { return "internals"; }
}
