import { r as registerInstance, c as createEvent, h, H as Host } from './index-CV0Pyo1O.js';
import { I as IMask } from './index-Ce_F4E0f.js';
import { C as CURRENCY_MASK } from './form-input-masks-CVaOFfrI.js';
import { E as inputGroup, x as label, y as inputAdornment, q as inputInvalid, u as inputDisabled, v as inputFocused } from './parts-CPKMvyV_.js';
import { F as FormControlHelpText } from './form-control-help-text-DGMpMuAm.js';
import { F as FormControlErrorText } from './form-control-error-text-Di9QePY5.js';
import './event-types-CZfYo5-1.js';

const FormControlMonetaryProvisioning = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.formControlInput = createEvent(this, "formControlInput", 7);
        this.formControlBlur = createEvent(this, "formControlBlur", 7);
        this.imask = null;
        this.isFocused = false;
        this.errorText = '';
        this.defaultValue = '0';
        this.inputHandler = () => { };
        this.maskOptions = CURRENCY_MASK.DECIMAL;
        this.disabled = false;
        this.initializeIMask = () => {
            if (!this.textInput)
                return;
            this.imask = 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 = inputGroup;
        if (this.errorText) {
            part = inputInvalid;
        }
        if (this.disabled) {
            part = inputDisabled;
        }
        if (this.isFocused) {
            part = inputFocused;
        }
        return part;
    }
    render() {
        return (h(Host, { key: '8ecc68592c7e6ef4418301935dde43e4ea10404d' }, h("div", { key: 'ba267bd049287c297aa67b761214b9d60503e605', class: "form-group d-flex flex-column" }, h("label", { key: 'b020e228d70145d758bf58298914ed2b3384bbbc', part: label, class: "form-label", htmlFor: this.name }, this.label), h("div", { key: '08415f471180958426563cf0e47e016b292108c7', class: "input-group" }, h("span", { key: 'dc22d1181a2a23e77003ee67212e724e48c2201e', class: "input-group-text", part: inputAdornment }, "$"), 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 })), h(FormControlHelpText, { key: '1315bdeefe7a551fbbb30680d289743a22a3792d', helpText: this.helpText, name: this.name }), h(FormControlErrorText, { key: '7212797f21917530b6bb093f146f1ebead4c232b', errorText: this.errorText, name: this.name }))));
    }
    static get watchers() { return {
        "defaultValue": ["handleDefaultValueChange"]
    }; }
};

export { FormControlMonetaryProvisioning as form_control_monetary_provisioning };
