import { Host, h } from "@stencil/core";
import { cls, getId } from "../../utils/helpers";
import IMask from "imask";
export class XTextarea {
    constructor() {
        /**
         * Typing timer instance
         */
        this.typingTimer = null;
        /**
         * Typing input event instance
         */
        this.typingEvent = null;
        /**
         * Element ID for internal reference
         */
        this.inputId = getId('textarea');
        this.label = undefined;
        this.name = undefined;
        this.placeholder = undefined;
        this.info = undefined;
        this.typingtimeout = 1200;
        this.required = undefined;
        this.loading = undefined;
        this.readonly = undefined;
        this.cleanable = undefined;
        this.autofocus = undefined;
        this.capitalize = 'off';
        this.autocorrect = 'off';
        this.autocomplete = 'off';
        this.enterkeyhint = undefined;
        this.inputmode = undefined;
        this.maxlength = undefined;
        this.minlength = undefined;
        this.rows = undefined;
        this.mask = undefined;
        this.value = '';
        this.isValid = true;
        this.isTyping = undefined;
        this.isFocused = undefined;
        this.isLoading = undefined;
        this.isDisabled = undefined;
    }
    /**
     * On watch mask pattern/options
     */
    watchMask() {
        if (this.mask && !this.maskInstance) {
            this.maskInstance = IMask(this.el.querySelector('input'), this.mask);
        }
        this.maskInstance.updateOptions(this.mask);
    }
    /**
     * This is called when the 'disabled' attribute of the element or or an ancestor <fieldset> is updated
     *
     * @param disabled
     */
    formDisabledCallback(disabled) {
        this.isDisabled = disabled;
    }
    /**
     * This is called when the parent/owner form is reset
     */
    formResetCallback() {
        this.isValid = true;
        this.internals.setFormValue('');
    }
    /**
     * This is called when the input is validated
     */
    formValidateCallback() {
        this.isValid = this.input.validity.valid;
        this.onValidateInputEvent.emit(this.input);
    }
    /**
     * This is called on component did load
     */
    componentDidLoad() {
        if (this.mask && !this.maskInstance) {
            this.maskInstance = IMask(this.el.querySelector('input'), this.mask);
        }
    }
    /**
     * Set element value by event
     *
     * @param event
     */
    onInput(event) {
        var _a;
        this.value = event.target.value;
        this.internals.setFormValue(event.target.value);
        this.el.dispatchEvent(new InputEvent('input', { bubbles: false }));
        this.formValidateCallback();
        (_a = this.internals.form) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new InputEvent('input', { bubbles: false }));
    }
    /**
     * This called on input field is unfocused
     */
    onBlurInput() {
        this.isFocused = false;
        setTimeout(() => this.formValidateCallback(), 50);
    }
    /**
     * This called on input field is focused
     */
    onFocusInput() {
        this.isFocused = true;
    }
    /**
     * This called on input field was changed
     *
     * @param event
     */
    onKeyUpInput(event) {
        this.isTyping = true;
        this.typingEvent = event;
        clearTimeout(this.typingTimer);
        this.typingTimer = setTimeout(this.onDoneTypingInput.bind(this), this.typingtimeout);
        if (this.value.toString().length >= this.maxlength) {
            this.el.querySelector('.field-counter').classList.remove('limit-error');
            setTimeout(() => {
                this.el.querySelector('.field-counter').classList.add('limit-error');
            }, 50);
        }
    }
    /**
     * This called on input field detect key is down
     */
    onKeyDownInput() {
        clearTimeout(this.typingTimer);
    }
    /**
     * This called on input field finished typing
     */
    onDoneTypingInput() {
        this.formValidateCallback();
        this.isTyping = false;
        this.onDoneTypingEvent.emit(this.typingEvent);
    }
    /**
     * Return validation of showing the input label
     *
     * @returns
     */
    showLabel() {
        return (this.label && this.placeholder) || (this.label && (((this.value || '').toString()).length > 0 || this.isFocused));
    }
    /**
     * Compute the placeholder value
     *
     * @returns
     */
    placeholderValue() {
        if (this.label) {
            if (this.placeholder) {
                return this.placeholder;
            }
            else if (!this.showLabel()) {
                return this.label;
            }
        }
        return this.placeholder;
    }
    /**
     * Clears the input value
     */
    clearInput() {
        this.value = '';
        if (this.typingTimer != null) {
            clearTimeout(this.typingTimer);
        }
        this.onClearInputEvent.emit(this.typingEvent);
    }
    /**
     * Render component
     */
    render() {
        return h(Host, { key: '8f248c034dc361f08eba37f9d7b485701f3fc1ab', class: cls({
                "focus": this.isFocused,
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "invalid": !this.isValid,
                "required": this.required,
            }), "data-for": this.name }, h("div", { key: '44c674c131ebdd3525cda0b29b5e5b4c9faac702', class: "indicator" }, h("slot", { key: '8eddc094222e31615824acc2083c751246bf6574', name: "indicator" })), h("div", { key: '7c019bc4e5313f596451b8c862d12942daf7b07b', class: "separator" }), h("div", { key: '681ce7dd1519c40a899fe6d2fbb7923262e2c426', class: "wrap" }, this.showLabel() && h("label", { key: 'e567208cb175f349ef357ac4f452ba0782eb2d03', htmlFor: this.inputId }, this.label), h("textarea", { key: '48bc05312d404c6376366eb121c0ea4fee2521b6', id: this.inputId, ref: el => this.input = el, required: this.required, disabled: this.isDisabled || this.loading || this.isLoading, readOnly: this.readonly, autoCorrect: this.autocorrect, autoComplete: this.autocomplete, autoFocus: this.autofocus, enterKeyHint: this.enterkeyhint, inputMode: this.inputmode, minLength: this.minlength, maxLength: this.maxlength, rows: this.rows, placeholder: this.placeholderValue(), value: this.value, onInput: (e) => this.onInput(e), onBlur: () => this.onBlurInput(), onFocus: () => this.onFocusInput(), onKeyUp: (e) => this.onKeyUpInput(e), onKeyDown: () => this.onKeyDownInput() })), h("slot", { key: 'eda37688f653fbb17ad76120208b1218390208d3' }), h("div", { key: '31882b2aba7a912820299a0cda9db29ed6823e22', class: "field-float-components" }, this.loading && h("div", { key: '7d1eacf430eca99052b783d5138153ba80cc562f', class: "loading-indicator" }), !!this.maxlength && h("small", { key: 'd67f3c075291165fb68b3d6aac02e0bd7c06c10a', class: "field-counter" }, h("span", { key: '511397ec4c855e6ac12ed3531dbeba0a40ceee5a' }, this.maxlength - this.value.toString().length)), h("div", { key: '502e1c06b31b1b85a3eeb17cbfdd744c52e43089', class: "buttons" }, this.cleanable && (this.value.toString()).length > 0 && h("x-button", { key: 'ec08ee58b24c70b406be44b5b661b99b86eecae9', icon: "backclear", onClick: () => this.clearInput() }), h("slot", { key: '5e46075710e5c08f8b51edce05341f5488259248', name: "buttons" })), this.info && h("div", { key: '65738d2c821c013d22bca256027a8a1b07e66d56', class: "info" }, h("x-tooltip", { key: '6b35a0598a84a0aff942d9851a42a9f62ef9398c', content: this.info }, h("x-icon", { key: '2248371cbcc5e36290d19bf018ebff44764c96f7', name: "info" })))));
    }
    static get is() { return "x-textarea"; }
    static get formAssociated() { return true; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input label text"
                },
                "attribute": "label",
                "reflect": false
            },
            "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
            },
            "placeholder": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input placeholder text"
                },
                "attribute": "placeholder",
                "reflect": false
            },
            "info": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input information block"
                },
                "attribute": "info",
                "reflect": false
            },
            "typingtimeout": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input debounce time"
                },
                "attribute": "typingtimeout",
                "reflect": false,
                "defaultValue": "1200"
            },
            "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
            },
            "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
            },
            "readonly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input block for edit"
                },
                "attribute": "readonly",
                "reflect": false
            },
            "cleanable": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input clear button"
                },
                "attribute": "cleanable",
                "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
            },
            "capitalize": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "InputAutocapitalizeTypes",
                    "resolved": "\"characters\" | \"none\" | \"off\" | \"on\" | \"sentences\" | \"words\"",
                    "references": {
                        "InputAutocapitalizeTypes": {
                            "location": "import",
                            "path": "../x-input/component",
                            "id": "src/components/x-input/component.tsx::InputAutocapitalizeTypes"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input autocapitalize options"
                },
                "attribute": "capitalize",
                "reflect": false,
                "defaultValue": "'off'"
            },
            "autocorrect": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'on' | 'off'",
                    "resolved": "\"off\" | \"on\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input auto correction should be enabled"
                },
                "attribute": "autocorrect",
                "reflect": false,
                "defaultValue": "'off'"
            },
            "autocomplete": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "InputAutocompleteTypes",
                    "resolved": "\"name\" | \"on\" | \"off\" | \"tel\" | \"url\" | \"email\" | \"honorific-prefix\" | \"given-name\" | \"additional-name\" | \"family-name\" | \"honorific-suffix\" | \"nickname\" | \"username\" | \"new-password\" | \"current-password\" | \"one-time-code\" | \"organization-title\" | \"organization\" | \"street-address\" | \"address-line1\" | \"address-line2\" | \"address-line3\" | \"address-level4\" | \"address-level3\" | \"address-level2\" | \"address-level1\" | \"country\" | \"country-name\" | \"postal-code\" | \"cc-name\" | \"cc-given-name\" | \"cc-additional-name\" | \"cc-family-name\" | \"cc-number\" | \"cc-exp\" | \"cc-exp-month\" | \"cc-exp-year\" | \"cc-csc\" | \"cc-type\" | \"transaction-currency\" | \"transaction-amount\" | \"language\" | \"bday\" | \"bday-day\" | \"bday-month\" | \"bday-year\" | \"sex\" | \"tel-country-code\" | \"tel-national\" | \"tel-area-code\" | \"tel-local\" | \"tel-extension\" | \"impp\" | \"photo\"",
                    "references": {
                        "InputAutocompleteTypes": {
                            "location": "import",
                            "path": "../x-input/component",
                            "id": "src/components/x-input/component.tsx::InputAutocompleteTypes"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input value of the control can be automatically completed by the browser"
                },
                "attribute": "autocomplete",
                "reflect": false,
                "defaultValue": "'off'"
            },
            "enterkeyhint": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "InputEnterKeyHintType",
                    "resolved": "\"done\" | \"enter\" | \"go\" | \"next\" | \"previous\" | \"search\" | \"send\"",
                    "references": {
                        "InputEnterKeyHintType": {
                            "location": "import",
                            "path": "../x-input/component",
                            "id": "src/components/x-input/component.tsx::InputEnterKeyHintType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input hint to the browser for which enter key to display"
                },
                "attribute": "enterkeyhint",
                "reflect": false
            },
            "inputmode": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "InputModeType",
                    "resolved": "\"decimal\" | \"email\" | \"none\" | \"numeric\" | \"search\" | \"tel\" | \"text\" | \"url\"",
                    "references": {
                        "InputModeType": {
                            "location": "import",
                            "path": "../x-input/component",
                            "id": "src/components/x-input/component.tsx::InputModeType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input hint to the browser for which keyboard to display"
                },
                "attribute": "inputmode",
                "reflect": false
            },
            "maxlength": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input maximun count of characters"
                },
                "attribute": "maxlength",
                "reflect": false
            },
            "minlength": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input minimun count of characters"
                },
                "attribute": "minlength",
                "reflect": false
            },
            "rows": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input rows"
                },
                "attribute": "rows",
                "reflect": false
            },
            "mask": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string | Object",
                    "resolved": "Object | string",
                    "references": {
                        "Object": {
                            "location": "global",
                            "id": "global::Object"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input imaks pattern/options"
                },
                "attribute": "mask",
                "reflect": false
            },
            "value": {
                "type": "any",
                "mutable": true,
                "complexType": {
                    "original": "string | number | null",
                    "resolved": "number | string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input value"
                },
                "attribute": "value",
                "reflect": false,
                "defaultValue": "''"
            }
        };
    }
    static get states() {
        return {
            "isValid": {},
            "isTyping": {},
            "isFocused": {},
            "isLoading": {},
            "isDisabled": {}
        };
    }
    static get events() {
        return [{
                "method": "onDoneTypingEvent",
                "name": "doneTyping",
                "bubbles": false,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On done input typing finish event"
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "onClearInputEvent",
                "name": "clearInput",
                "bubbles": false,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On clear input event"
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "onValidateInputEvent",
                "name": "validated",
                "bubbles": false,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On validate input event"
                },
                "complexType": {
                    "original": "HTMLTextAreaElement",
                    "resolved": "HTMLTextAreaElement",
                    "references": {
                        "HTMLTextAreaElement": {
                            "location": "global",
                            "id": "global::HTMLTextAreaElement"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "mask",
                "methodName": "watchMask"
            }];
    }
    static get attachInternalsMemberName() { return "internals"; }
}
