import { Host, h } from "@stencil/core";
import { cls, getId } from "../../utils/helpers";
import { Lang } from "../../services/lang";
export class XInputCheckbox {
    constructor() {
        /**
         * Element ID for internal reference
         */
        this.inputId = getId('switch');
        this.name = undefined;
        this.required = undefined;
        this.bordered = undefined;
        this.loading = undefined;
        this.autofocus = 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
     */
    handleClick() {
        if (this.disabled) {
            return;
        }
        this.checked = !this.checked;
    }
    /**
     * Element on mouse up event
     */
    handleMouseUp() {
        this.onBlurInput();
    }
    /**
     * Listen for keyboard events for confirmation
     */
    handleKeyDown(ev) {
        if ([' ', 'Enter'].includes(ev.key)) {
            this.el.click();
        }
    }
    /**
     * Element change state
     */
    watchChecked(checked) {
        var _a;
        this.internals.setFormValue(checked ? this.getInputValue() : null, checked ? 'checked' : null);
        this.el.dispatchEvent(new InputEvent('input', { bubbles: false }));
        this.formValidateCallback(true);
        (_a = this.internals.form) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new InputEvent('input', { bubbles: false }));
    }
    /**
     * Compute the input value
     */
    getInputValue() {
        return this.value ? this.value.toString() : this.el.textContent.trim();
    }
    /**
     * This is called on component did load
     */
    componentDidLoad() {
        setTimeout(() => this.formValidateCallback(), 0);
    }
    /**
     * 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 when the input is validated
     */
    formValidateCallback(dispatchValidation) {
        if (this.required && !this.checked) {
            this.internals.setValidity({ valueMissing: true }, Lang.get('fields.validation.value-missing'));
        }
        else {
            this.internals.setValidity({});
        }
        if (dispatchValidation) {
            this.isValid = this.internals.validity.valid;
            this.onValidateInputEvent.emit(this.internals);
        }
    }
    /**
     * This called on input field is unfocused
     */
    onBlurInput() {
        this.isFocused = false;
        setTimeout(() => this.formValidateCallback(true), 50);
    }
    /**
     * This called on input field is focused
     */
    onFocusInput() {
        this.isFocused = true;
    }
    /**
     * Render component
     */
    render() {
        return h(Host, { key: '3023caa862c563e5d26cd2ee469a0d5e03320376', 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: '27652eaeeafa68c7a138e20fe578f10711fea313' }, h("span", { key: 'b9548888b66f26d154e6f77a9514f87ffe7637f0', class: cls({ "checked": this.checked }) }), this.loading && h("div", { key: '9ef6334552f58a891d8208bc80b57dce68fcbc41', class: "loading-indicator" })), h("div", { key: '3b372cf68d825c1b4fb3be8c8e80765592f65627' }, h("slot", { key: 'f0ac1cbc6fb94bf305b7af7b3909bc5678b92951' })), h("input", { key: '2cacd2f0c2772c625a25559f5276cdc9851234c8', type: "switch", id: this.inputId }));
    }
    static get is() { return "x-switch"; }
    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
            },
            "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": true
            },
            "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": false,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On validate input event"
                },
                "complexType": {
                    "original": "ElementInternals",
                    "resolved": "ElementInternals",
                    "references": {
                        "ElementInternals": {
                            "location": "global",
                            "id": "global::ElementInternals"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "checked",
                "methodName": "watchChecked"
            }];
    }
    static get listeners() {
        return [{
                "name": "click",
                "method": "handleClick",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "mouseup",
                "method": "handleMouseUp",
                "target": undefined,
                "capture": false,
                "passive": true
            }, {
                "name": "keydown",
                "method": "handleKeyDown",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
    static get attachInternalsMemberName() { return "internals"; }
}
