import { Host, h } from "@stencil/core";
import { cls, getId } from "../../utils/helpers";
export class XInputCheckbox {
    constructor() {
        /**
         * Element ID for internal reference
         */
        this.inputId = getId('checkbox');
        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.indeterminate = undefined;
        this.value = null;
        this.isFocused = undefined;
        this.isLoading = undefined;
        this.isValid = undefined;
    }
    /**
     * Element on click event toogle state
     */
    onClick() {
        this.checked = !this.checked;
    }
    /**
     * Element on mouse up event
     */
    onMouseUp() {
        this.onBlurInput();
    }
    /**
     * Element change state
     */
    onChangeState(checked) {
        this.internals.setFormValue(checked ? this.value.toString() : null, checked ? 'checked' : 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;
    }
    /**
     * Validate the field input
     */
    onValidate() {
        this.validate && this.onValidateInputEvent.emit(this.el.querySelector('input') || document.getElementById(this.inputId));
    }
    /**
     * 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: '97a59488fc2068f4396b83c7ab42e0222a859d4a', 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: '7d1647438c3241a68ff63796132ba3703626daf0' }, h("span", { key: 'f18e5224001a22d72361c88332dcdd6e91fdf08e', class: cls({
                "checkbox": true,
                "checked": this.checked,
                "intermediate": this.indeterminate,
            }) }), this.loading && h("div", { key: '72db4ec8b6ecf243d22f00d40bc5c66c5045ee70', class: "loading-indicator" })), h("div", { key: 'ec3ef26a9dccf14a852c6eb39f01bd4785972ba8' }, h("slot", { key: '267d361b9a277f2413d8c89ed57c7d088f550e80' })), h("input", { key: '5bf15fb8e351df8cbcbca0c8680b3c36284e9afb', type: "checkbox", id: this.inputId, required: this.required && !this.checked }));
    }
    static get is() { return "x-input-checkbox"; }
    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
            },
            "indeterminate": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element indeterminate state"
                },
                "attribute": "indeterminate",
                "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"; }
}
