import { Host, h } from "@stencil/core";
import { cls, getId } from "../../utils/helpers";
import { Lang } from "../../services/lang";
import IMask from "imask";
export class XInput {
    constructor() {
        /**
         * Typing timer instance
         */
        this.typingTimer = null;
        /**
         * Typing input event instance
         */
        this.typingEvent = null;
        /**
         * Element ID for internal reference
         */
        this.inputId = getId('input');
        this.label = undefined;
        this.name = undefined;
        this.type = 'text';
        this.placeholder = undefined;
        this.info = undefined;
        this.typingtimeout = 1200;
        this.validate = undefined;
        this.required = undefined;
        this.loading = undefined;
        this.readonly = undefined;
        this.cleanable = undefined;
        this.autocorrect = 'off';
        this.pattern = undefined;
        this.autocomplete = 'off';
        this.enterkeyhint = undefined;
        this.inputmode = undefined;
        this.max = undefined;
        this.min = undefined;
        this.step = undefined;
        this.maxlength = undefined;
        this.minlength = undefined;
        this.mask = undefined;
        this.value = '';
        this.inputType = this.type;
        this.isValid = true;
        this.isTyping = undefined;
        this.isFocused = undefined;
        this.isLoading = undefined;
        this.isDisabled = undefined;
    }
    /**
     * On watch mask pattern/options
     */
    maskChanged() {
        if (this.mask && !this.maskInstance) {
            this.maskInstance = IMask(this.el.querySelector('input'), this.mask);
        }
        this.maskInstance.updateOptions(this.mask);
    }
    /**
     * On watch type property hook
     */
    typeChanged() {
        this.validateInputType();
    }
    /**
     * 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 on component did load
     */
    componentDidLoad() {
        this.validateInputType();
        if (this.mask && !this.maskInstance) {
            this.maskInstance = IMask(this.el.querySelector('input'), this.mask);
        }
    }
    /**
     * Set element value by event
     *
     * @param event
     */
    onInput(event) {
        this.value = event.target.value;
        this.internals.setFormValue(event.target.value);
    }
    /**
     * 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;
    }
    /**
     * 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);
    }
    /**
     * This called on input field detect key is down
     */
    onKeyDownInput() {
        clearTimeout(this.typingTimer);
    }
    /**
     * This called on input field finished typing
     */
    onDoneTypingInput() {
        this.onValidate();
        this.isTyping = false;
        this.onDoneTypingEvent.emit(this.typingEvent);
    }
    /**
     * Validate the field input
     *
     * @returns
     */
    onValidate() {
        if (!this.validate) {
            return;
        }
        const selectors = ["input", "select", "textarea"];
        let input = null;
        selectors.forEach((selector) => {
            if (input === null) {
                input = this.el.querySelector(selector);
            }
        });
        this.isValid = (input === null || input === void 0 ? void 0 : input.validity) ? input.validity.valid : true;
    }
    /**
     * Return validation of showing the input label
     *
     * @returns
     */
    showLabel() {
        var _a;
        return (this.label && this.placeholder) || (this.label && (((_a = this.value) === null || _a === void 0 ? void 0 : _a.toString()).length > 0 || this.isFocused)) || (this.label && this.type == 'color');
    }
    /**
     * 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;
    }
    /**
     * Show the content of input when type is `password`
     *
     * @returns
     */
    togglePasswordVisibility() {
        if (this.isDisabled) {
            return;
        }
        this.inputType = this.inputType == 'password' ? 'text' : 'password';
        setTimeout(() => {
            this.el.querySelector('input').focus();
        }, 100);
    }
    /**
     * Sumarize the actual value if type is `numeric`
     *
     * @returns
     */
    sumValue() {
        if (isNaN(parseInt(this.value.toString()))) {
            this.value = this.min ? this.min : 0;
        }
        if (typeof this.value !== 'number') {
            this.value = parseInt(this.value);
        }
        if (!isNaN(this.max) && this.value >= this.max) {
            return;
        }
        this.value = this.value + (this.step || 1);
    }
    /**
     * Substract the actual value if type is `numeric`
     *
     * @returns
     */
    subValue() {
        if (isNaN(parseInt(this.value.toString()))) {
            this.value = this.max ? this.max : 0;
        }
        if (typeof this.value !== 'number') {
            this.value = parseInt(this.value);
        }
        if (!isNaN(this.min) && this.value <= this.min) {
            return;
        }
        this.value = this.value - (this.step || 1);
    }
    /**
     * Validate the initials values of the input by type
     */
    validateInputType() {
        this.inputType = this.type;
        switch (this.type) {
            case 'email':
                this.inputmode = this.inputmode || 'email';
                break;
            case 'tel':
                this.inputmode = this.inputmode || 'tel';
                break;
            case 'url':
                this.inputmode = this.inputmode || 'url';
                break;
            case 'search':
                this.inputmode = this.inputmode || 'search';
                this.enterkeyhint = this.enterkeyhint || 'search';
                break;
            case 'number':
            case 'numeric':
                this.inputmode = this.inputmode || 'numeric';
                break;
            case 'amount':
                this.inputmode = this.inputmode || 'numeric';
                this.mask = {
                    mask: Number,
                    scale: 2,
                    radix: '.',
                    mapToRadix: ['.'],
                    thousandsSeparator: ',',
                    padFractionalZeros: false,
                    normalizeZeros: true,
                };
                break;
            default:
                this.inputmode = this.inputmode || 'text';
                break;
        }
        if (this.mask) {
            this.inputType = 'text';
        }
    }
    /**
     * 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: 'e83c4405b59216e330378496d6fc2ed4f8a02e2a', class: cls({
                "focus": this.isFocused,
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "invalid": !this.isValid,
                "required": this.required,
            }) }, h("div", { key: '306b792414885ebd822d36033fb12f75d9205e6f', class: "indicator" }, h("slot", { key: 'd696e99f3bd2c11904a4768ab23bd0faafea4a06', name: "indicator" })), h("div", { key: 'e178fe1e1a654c92596ae0c4e10e5612554f762d', class: "separator" }), h("div", { key: 'd2355d702fc8ef6aa0da4cddc63ba7b7855987a6', class: "buttons" }, this.inputType == 'numeric' && h("x-button", { key: 'a3f901d8681c8f45b522e74700109bb9efaf4b57', icon: "plus", onClick: () => this.sumValue() })), h("div", { key: 'e1fc2d358d74184aea2d15778b0091970c0a30b0', class: "wrap" }, this.showLabel() && h("label", { key: 'fff3f1c862204a9a6c5163568a90c143325cf26b', htmlFor: this.inputId }, this.label), h("input", { key: '0db16866ce2fb1bac6a5cd998612cd8e766843ad', "data-field": this.type, id: this.inputId, type: this.inputType, name: this.name, required: this.required, disabled: this.isDisabled || this.loading || this.isLoading, readOnly: this.readonly, autoCorrect: this.autocorrect, pattern: this.pattern, autoComplete: this.autocomplete, enterKeyHint: this.enterkeyhint, inputMode: this.inputmode, min: this.min, max: this.max, minLength: this.minlength, maxLength: this.maxlength, 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() }), this.inputType == 'color' && h("div", { key: '41d1052ec191a1851371d2543d244f92f86b165d', class: "color-indicator" }, h("i", { key: '0caf358ec7ac7e82ddfc43ab8a5cfb83792c9d02', style: { background: this.value.toString() } }), h("span", { key: '0a5e3c38f9428f3b412c549c4aaaf50b6db4fe85' }, this.value || this.placeholderValue() || Lang.get('fields.input.color')))), h("slot", { key: 'efd5e69f945615709074489015cdc2ac1bde0542' }), h("div", { key: '80eef2f94e22156aaf8fc47888b1b45c7131e9ca', class: "loading-indicator" }), h("div", { key: '119c04b868c7aea6c36561466aaa876c6cf90cba', class: "buttons" }, this.cleanable && (this.value.toString()).length > 0 && h("x-button", { key: 'af4b9329f992d453b0653c2927550eb521fc48eb', icon: "backclear", onClick: () => this.clearInput() }), this.type == 'password' && h("x-button", { key: '38c48c3950b2d8614f7cb3ec2b15115b02aaaad6', icon: this.inputType == 'password' ? 'visible' : 'invisible', onClick: () => this.togglePasswordVisibility() }), this.inputType == 'numeric' && h("x-button", { key: 'a86512e8d4846d84302a53d6cac1cc1de6dcfb5c', icon: "minus", onClick: () => this.subValue() }), h("slot", { key: '240c2bd14c57919a92dfbe2cc7c2010b3c9f9136', name: "buttons" })), this.info && h("div", { key: '5a7a4d2b4351ebffad692f6958816f16e4a56008', class: "info" }, h("x-tooltip", { key: '0a20db6b4bb2810e9a3ba9c6380644539b59ecc1', content: this.info }, h("x-icon", { key: 'b67b92685fc41fe630ad615cf567724408a889fc', name: "info" }))));
    }
    static get is() { return "x-input"; }
    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
            },
            "type": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "InputType",
                    "resolved": "\"amount\" | \"color\" | \"email\" | \"number\" | \"numeric\" | \"password\" | \"search\" | \"tel\" | \"text\" | \"url\"",
                    "references": {
                        "InputType": {
                            "location": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-input/component.tsx",
                            "id": "src/components/x-input/component.tsx::InputType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input type"
                },
                "attribute": "type",
                "reflect": false,
                "defaultValue": "'text'"
            },
            "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"
            },
            "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
            },
            "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
            },
            "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'"
            },
            "pattern": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input regular expression that the value is checked against"
                },
                "attribute": "pattern",
                "reflect": false
            },
            "autocomplete": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "InputAutocompleteTypes",
                    "resolved": "\"name\" | \"url\" | \"email\" | \"tel\" | \"on\" | \"off\" | \"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": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-input/component.tsx",
                            "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": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-input/component.tsx",
                            "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": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-input/component.tsx",
                            "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
            },
            "max": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input maximun value"
                },
                "attribute": "max",
                "reflect": false
            },
            "min": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input minimun value"
                },
                "attribute": "min",
                "reflect": false
            },
            "step": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input value in steps only for numeric|number type"
                },
                "attribute": "step",
                "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
            },
            "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 {
            "inputType": {},
            "isValid": {},
            "isTyping": {},
            "isFocused": {},
            "isLoading": {},
            "isDisabled": {}
        };
    }
    static get events() {
        return [{
                "method": "onDoneTypingEvent",
                "name": "doneTyping",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On done input typing finish event"
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "onClearInputEvent",
                "name": "clearInput",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On clear input event"
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "mask",
                "methodName": "maskChanged"
            }, {
                "propName": "type",
                "methodName": "typeChanged"
            }];
    }
    static get attachInternalsMemberName() { return "internals"; }
}
