import * as i0 from '@angular/core';
import { EventEmitter, forwardRef, Directive, Input, HostBinding, Output, HostListener, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { CurrencyMaskDirective } from 'ng2-currency-mask';
import { first } from 'rxjs/operators';
import * as i1 from '@seniorsistemas/angular-components/locale';
import { CommonModule } from '@angular/common';

/**
 * @deprecated Should use 'AlignmentOptions' from @seniorsistemas/ng2-currency-mask instead
 */
var BignumberAlignmentOption;
(function (BignumberAlignmentOption) {
    BignumberAlignmentOption["RIGHT"] = "right";
    BignumberAlignmentOption["LEFT"] = "left";
})(BignumberAlignmentOption || (BignumberAlignmentOption = {}));
/**
 * @deprecated Should use BigNumberInput directive instead
 */
class BignumberInputDirective extends CurrencyMaskDirective {
    localeService;
    precision = 15;
    scale = 0;
    decimalSeparator;
    thousandsSeparator;
    alignTo = 'left';
    allowNegative = true;
    placeholder;
    maxLength;
    pasteRejected = new EventEmitter();
    regex = /\d/;
    //TODO: Ajuste no ng2-currency-mask para utilizar esse Código (aplicado na tarefa: SDS-305)
    // private readonly _angularComponentsLocaleService: LocaleService;
    // constructor(injector: Injector) {
    // super(injector);
    // this._angularComponentsLocaleService = injector.get(LocaleService);
    // this.onLocaleService();
    // }
    //TODO:  Remover esse constructor
    constructor(_elementRef, _keyValueDiffers, localeService) {
        super({ align: '', allowNegative: true, decimal: '', precision: 0, prefix: '', suffix: '', thousands: '' }, _elementRef, _keyValueDiffers);
        this.localeService = localeService;
        this.onLocaleService();
    }
    ngOnInit() {
        this.updateVariables();
        super.ngOnInit();
    }
    ngOnChanges(changes) {
        const placeholderChange = changes['placeholder']?.currentValue;
        if (!placeholderChange && this.scale) {
            this.placeholder = `0${this.decimalSeparator}${''.padEnd(this.scale, '0')}`;
        }
        const configChanged = changes['scale'] ||
            changes['decimalSeparator'] ||
            changes['thousandsSeparator'] ||
            changes['alignTo'] ||
            changes['precision'] ||
            changes['allowNegative'];
        if (configChanged) {
            this.updateVariables();
        }
    }
    onPaste(event) {
        const pastedText = event.clipboardData?.getData("text") ?? "";
        const numericText = pastedText.replace(new RegExp(`\\${this.thousandsSeparator}`, "g"), "").replace(this.decimalSeparator, ".");
        const [intPart, decimalPart = ""] = numericText.split(".");
        const totalDigits = intPart.replace(/\D/g, "").length + decimalPart.slice(0, this.scale).length;
        if (totalDigits > this.precision) {
            event.preventDefault();
            this.pasteRejected.emit(pastedText);
        }
    }
    /**
     * Update the options values according to the directive input values.
     */
    updateVariables() {
        this.options = {
            align: this.alignTo,
            decimal: this.decimalSeparator,
            thousands: this.thousandsSeparator,
            precision: this.scale || 0,
            prefix: '',
            suffix: '',
            allowNegative: this.allowNegative,
        };
        this.calculateMaxLength();
    }
    /**
     * Responsible to calculate the field maximum length considering the separators.
     */
    calculateMaxLength() {
        const decSepLength = this.decimalSeparator?.length ?? 0;
        const thoSepLength = this.thousandsSeparator?.length ?? 0;
        const DIGITS_GROUP_SIZE = 3;
        let maxLength = this.precision + (this.scale ? decSepLength : 0);
        maxLength += Math.ceil((this.precision - (this.scale ? this.scale : 0)) / DIGITS_GROUP_SIZE - 1) * thoSepLength;
        this.maxLength = maxLength;
    }
    onLocaleService() {
        this.localeService
            .getLocale()
            .pipe(first())
            .subscribe(() => {
            this.decimalSeparator = this.decimalSeparator ?? this.localeService.getDecimalSeparator();
            this.thousandsSeparator = this.thousandsSeparator ?? this.localeService.getGroupingSeparator();
        });
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BignumberInputDirective, deps: [{ token: i0.ElementRef }, { token: i0.KeyValueDiffers }, { token: i1.LocaleService }], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.14", type: BignumberInputDirective, selector: "input[sBignumberInput]", inputs: { precision: "precision", scale: "scale", decimalSeparator: "decimalSeparator", thousandsSeparator: "thousandsSeparator", alignTo: "alignTo", allowNegative: "allowNegative", placeholder: "placeholder" }, outputs: { pasteRejected: "pasteRejected" }, host: { listeners: { "paste": "onPaste($event)" }, properties: { "attr.placeholder": "this.placeholder", "attr.maxLength": "this.maxLength" } }, providers: [
            {
                provide: NG_VALUE_ACCESSOR,
                // NOSONAR - 'BignumberInputDirective' is deprecated.
                useExisting: forwardRef(() => BignumberInputDirective),
                multi: true,
            },
        ], usesInheritance: true, usesOnChanges: true, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BignumberInputDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: 'input[sBignumberInput]',
                    providers: [
                        {
                            provide: NG_VALUE_ACCESSOR,
                            // NOSONAR - 'BignumberInputDirective' is deprecated.
                            useExisting: forwardRef(() => BignumberInputDirective),
                            multi: true,
                        },
                    ],
                }]
        }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.KeyValueDiffers }, { type: i1.LocaleService }], propDecorators: { precision: [{
                type: Input
            }], scale: [{
                type: Input
            }], decimalSeparator: [{
                type: Input
            }], thousandsSeparator: [{
                type: Input
            }], alignTo: [{
                type: Input
            }], allowNegative: [{
                type: Input
            }], placeholder: [{
                type: Input
            }, {
                type: HostBinding,
                args: ['attr.placeholder']
            }], maxLength: [{
                type: HostBinding,
                args: ['attr.maxLength']
            }], pasteRejected: [{
                type: Output
            }], onPaste: [{
                type: HostListener,
                args: ["paste", ["$event"]]
            }] } });

class BignumberInputModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BignumberInputModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: BignumberInputModule, declarations: [BignumberInputDirective], imports: [CommonModule], exports: [BignumberInputDirective] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BignumberInputModule, imports: [CommonModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BignumberInputModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    // NOSONAR - 'BignumberInputDirective' is deprecated.
                    declarations: [BignumberInputDirective],
                    // NOSONAR - 'BignumberInputDirective' is deprecated.
                    exports: [BignumberInputDirective],
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { BignumberAlignmentOption, BignumberInputDirective, BignumberInputModule };
//# sourceMappingURL=seniorsistemas-angular-components-bignumber-input.mjs.map
