import { Host, h } from "@stencil/core";
import { NativeValidationStrategy } from '../../utils/native-validation-strategy';
let toggleId = 0;
export class Toggle {
    constructor() {
        this.inputId = `anj-tg-${toggleId++}`;
        /**
         * Is it checked
         */
        this.checked = false;
        /**
         * Make the control read only
         */
        this.disabled = false;
        /**
         * The input name name
         */
        this.name = this.inputId;
        /**
         * @internal
         * The validation strategy to use
         */
        this.validationStrategy = new NativeValidationStrategy();
    }
    handleChange(checked) {
        this.anjChange.emit({
            checked,
            value: this.value
        });
    }
    /**
     * Checks the native element validity use native validators
     */
    async checkValidity() {
        this.nativeCheck.checkValidity();
        return this.validationStrategy.onValidChange(this.element, this.nativeCheck);
    }
    onChange() {
        this.checked = !this.checked;
        return this.validationStrategy.onValidChange(this.element, this.nativeCheck);
    }
    onFocus() {
        this.focused = true;
    }
    async onBlur() {
        this.focused = false;
        this.anjBlur.emit();
        return this.checkValidity();
    }
    render() {
        return (h(Host, { class: { 'anj-focused': this.focused } },
            h("label", { class: "anj-label", htmlFor: this.inputId },
                h("div", { class: "anj-toggle" },
                    h("input", { id: this.inputId, type: "checkbox", class: "anj-hidden-control", name: this.name, value: this.value, checked: this.checked, disabled: this.disabled, required: this.required, onChange: () => this.onChange(), onFocus: () => this.onFocus(), onBlur: () => this.onBlur(), onClick: e => e.stopPropagation(), onInvalid: () => this.validationStrategy.onValidChange(this.element, this.nativeCheck), ref: r => this.nativeCheck = r }),
                    h("span", { class: "anj-slide" }),
                    h("span", { class: "anj-indicator" })),
                h("slot", null))));
    }
    static get is() { return "anj-toggle"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() { return {
        "$": ["toggle.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["toggle.css"]
    }; }
    static get properties() { return {
        "checked": {
            "type": "boolean",
            "mutable": true,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Is it checked"
            },
            "attribute": "checked",
            "reflect": true,
            "defaultValue": "false"
        },
        "disabled": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Make the control read only"
            },
            "attribute": "disabled",
            "reflect": true,
            "defaultValue": "false"
        },
        "invalid": {
            "type": "boolean",
            "mutable": true,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Whether the control is valid"
            },
            "attribute": "invalid",
            "reflect": true
        },
        "name": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "The input name name"
            },
            "attribute": "name",
            "reflect": false,
            "defaultValue": "this.inputId"
        },
        "required": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Is it required"
            },
            "attribute": "required",
            "reflect": false
        },
        "stacked": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Whether inline (default) or stacked)"
            },
            "attribute": "stacked",
            "reflect": true
        },
        "validationStrategy": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "NativeValidationStrategy",
                "resolved": "NativeValidationStrategy",
                "references": {
                    "NativeValidationStrategy": {
                        "location": "import",
                        "path": "../../utils/native-validation-strategy"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [{
                        "text": "The validation strategy to use",
                        "name": "internal"
                    }],
                "text": ""
            },
            "defaultValue": "new NativeValidationStrategy()"
        },
        "value": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "The value of the control."
            },
            "attribute": "value",
            "reflect": false
        }
    }; }
    static get states() { return {
        "focused": {}
    }; }
    static get events() { return [{
            "method": "anjBlur",
            "name": "anjBlur",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Fires when the control is blurred"
            },
            "complexType": {
                "original": "void",
                "resolved": "void",
                "references": {}
            }
        }, {
            "method": "anjChange",
            "name": "anjChange",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Fires when the checked state is changed"
            },
            "complexType": {
                "original": "CheckedInputChangeEvent",
                "resolved": "CheckedInputChangeEvent",
                "references": {
                    "CheckedInputChangeEvent": {
                        "location": "import",
                        "path": "../../types/types"
                    }
                }
            }
        }]; }
    static get methods() { return {
        "checkValidity": {
            "complexType": {
                "signature": "() => Promise<boolean>",
                "parameters": [],
                "references": {
                    "Promise": {
                        "location": "global"
                    }
                },
                "return": "Promise<boolean>"
            },
            "docs": {
                "text": "Checks the native element validity use native validators",
                "tags": []
            }
        }
    }; }
    static get elementRef() { return "element"; }
    static get watchers() { return [{
            "propName": "checked",
            "methodName": "handleChange"
        }]; }
}
