'use strict';

var index = require('./index-p7cvUYAE.js');
var index$1 = require('./index-1MT3Z4XI.js');
var formInputMasks = require('./form-input-masks-BnNFJ3IJ.js');
var parts = require('./parts-BXshb_S-.js');
var formControlHelpText = require('./form-control-help-text-BdbTxD5g.js');
var formControlErrorText = require('./form-control-error-text-BO58Y6zF.js');
require('./event-types-YOY5TKUN.js');

const FormControlMonetaryProvisioning = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.formControlInput = index.createEvent(this, "formControlInput", 7);
        this.formControlBlur = index.createEvent(this, "formControlBlur", 7);
        this.imask = null;
        this.isFocused = false;
        this.errorText = '';
        this.defaultValue = '0';
        this.inputHandler = () => { };
        this.maskOptions = formInputMasks.CURRENCY_MASK.DECIMAL;
        this.disabled = false;
        this.initializeIMask = () => {
            if (!this.textInput)
                return;
            this.imask = index$1.IMask(this.textInput, this.maskOptions);
            this.imask.on('accept', () => {
                const rawValue = this.imask.unmaskedValue;
                this.inputHandler(this.name, rawValue);
                this.formControlInput.emit({ name: this.name, value: rawValue });
            });
            this.textInput.addEventListener('blur', () => this.formControlBlur.emit());
        };
        this.handleFormControlInput = (event) => {
            const target = event.target;
            const name = target.getAttribute('name');
            const rawValue = this.imask.unmaskedValue;
            this.formControlInput.emit({ name: name, value: rawValue });
        };
        this.updateInput = (newValue) => {
            if (this.imask) {
                this.imask.value = String(newValue);
            }
        };
    }
    handleDefaultValueChange(newValue) {
        this.updateInput(newValue);
    }
    componentDidLoad() {
        this.initializeIMask();
        this.updateInput(this.defaultValue);
    }
    disconnectedCallback() {
        var _a;
        (_a = this.imask) === null || _a === void 0 ? void 0 : _a.destroy();
    }
    get inputPart() {
        let part = parts.inputGroup;
        if (this.errorText) {
            part = parts.inputInvalid;
        }
        if (this.disabled) {
            part = parts.inputDisabled;
        }
        if (this.isFocused) {
            part = parts.inputFocused;
        }
        return part;
    }
    render() {
        return (index.h(index.Host, { key: '8ecc68592c7e6ef4418301935dde43e4ea10404d' }, index.h("div", { key: 'ba267bd049287c297aa67b761214b9d60503e605', class: "form-group d-flex flex-column" }, index.h("label", { key: 'b020e228d70145d758bf58298914ed2b3384bbbc', part: parts.label, class: "form-label", htmlFor: this.name }, this.label), index.h("div", { key: '08415f471180958426563cf0e47e016b292108c7', class: "input-group" }, index.h("span", { key: 'dc22d1181a2a23e77003ee67212e724e48c2201e', class: "input-group-text", part: parts.inputAdornment }, "$"), index.h("input", { key: '3cdedc0317df58360601186dd744bcb721a68ec7', 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.inputPart, class: this.errorText ? 'form-control monetary is-invalid' : 'form-control monetary', type: "text", disabled: this.disabled })), index.h(formControlHelpText.FormControlHelpText, { key: '1315bdeefe7a551fbbb30680d289743a22a3792d', helpText: this.helpText, name: this.name }), index.h(formControlErrorText.FormControlErrorText, { key: '7212797f21917530b6bb093f146f1ebead4c232b', errorText: this.errorText, name: this.name }))));
    }
    static get watchers() { return {
        "defaultValue": ["handleDefaultValueChange"]
    }; }
};

exports.form_control_monetary_provisioning = FormControlMonetaryProvisioning;
