import { Host, h, } from "@stencil/core";
import IMask from "imask";
import { FormControlErrorText } from "../../ui-components";
import { input, inputDisabled, inputFocused, inputInvalid, label } from "../../styles/parts";
export class FormControlNumberMasked {
    constructor() {
        this.imask = null;
        this.isFocused = false;
        this.inputHandler = () => { };
        this.disabled = false;
        this.initializeIMask = () => {
            if (!this.textInput)
                return;
            this.imask = IMask(this.textInput, {
                mask: this.mask,
            });
            this.imask.on('accept', () => {
                var _a;
                const rawValue = (_a = this.imask) === null || _a === void 0 ? void 0 : _a.unmaskedValue;
                if (rawValue !== undefined) {
                    this.inputHandler(this.name, rawValue);
                }
            });
            this.textInput.addEventListener('blur', () => this.formControlBlur.emit());
        };
        this.handleFormControlInput = (event) => {
            var _a;
            const target = event.target;
            const name = target.getAttribute('name');
            const rawValue = ((_a = this.imask) === null || _a === void 0 ? void 0 : _a.unmaskedValue) || target.value;
            this.formControlInput.emit({ name: name, value: rawValue });
        };
        this.updateInput = (newValue) => {
            if (this.imask && newValue) {
                this.imask.value = String(newValue);
            }
        };
    }
    handleDefaultValueChange(newValue) {
        this.initializeIMask();
        this.updateInput(newValue);
    }
    disconnectedCallback() {
        var _a;
        (_a = this.imask) === null || _a === void 0 ? void 0 : _a.destroy();
    }
    componentDidLoad() {
        if (this.textInput) {
            this.initializeIMask();
            this.updateInput(this.defaultValue);
        }
    }
    get part() {
        if (this.errorText) {
            return inputInvalid;
        }
        if (this.disabled) {
            return inputDisabled;
        }
        if (this.isFocused) {
            return inputFocused;
        }
        return input;
    }
    render() {
        return (h(Host, { key: '0ff6ce9901d9e804d1b82f9e8830a6d2a4cab67f' }, h("div", { key: '1846069b3d0682c1c1059a9a6192610dc6ac7e66', class: "form-group d-flex flex-column" }, h("div", { key: '2739a42086660392c2c508c37cdb387303efe592', class: "d-flex align-items-start gap-2" }, h("label", { key: 'e2e8149f7284cdfc0548131afa0c0a1d8f2e9385', part: label, class: "form-label", htmlFor: this.name }, this.label), h("form-control-tooltip", { key: 'ed0552645fd9929fa10ccd56dae495f27283c168', helpText: this.helpText })), h("input", { key: '1f52622e4724ac43f8b430f4742b6f9ed0a5ccf6', ref: el => (this.textInput = el), id: this.name, name: this.name, onFocus: () => (this.isFocused = true), onBlur: () => {
                this.isFocused = false;
                this.formControlBlur.emit();
            }, onInput: this.handleFormControlInput, part: this.part, class: this.errorText ? 'form-control is-invalid' : 'form-control', type: "text", disabled: this.disabled }), h(FormControlErrorText, { key: '2cce6d31bf2ff20a8c32e1e3d28e1cb8040b192b', errorText: this.errorText, name: this.name }))));
    }
    static get is() { return "form-control-number-masked"; }
    static get properties() {
        return {
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "label"
            },
            "name": {
                "type": "any",
                "mutable": false,
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "name"
            },
            "helpText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "help-text"
            },
            "errorText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "error-text"
            },
            "defaultValue": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "default-value"
            },
            "inputHandler": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "(name: string, value: string) => void",
                    "resolved": "(name: string, value: string) => void",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "defaultValue": "() => { }"
            },
            "mask": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "mask"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disabled",
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "isFocused": {}
        };
    }
    static get events() {
        return [{
                "method": "formControlInput",
                "name": "formControlInput",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "formControlBlur",
                "name": "formControlBlur",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "defaultValue",
                "methodName": "handleDefaultValueChange"
            }];
    }
}
