import * as i0 from '@angular/core';
import { inject, forwardRef, Directive, Input, NgModule } from '@angular/core';
import { NG_VALIDATORS } from '@angular/forms';
import { LocaleService } from '@seniorsistemas/angular-components/locale';
import { first } from 'rxjs/operators';
import { CommonModule } from '@angular/common';

/**
 * @description Diretiva de validação para campos `<input>` que verificam se o valor digitado
 * é um número localizado válido (considerando separadores decimal e de milhar do locale ativo).
 * Ao ser integrada ao `NG_VALIDATORS`, adiciona o erro `invalidNumber` ao control quando
 * o valor não corresponde ao formato esperado.
 *
 * @example
 * ```html
 * <input sLocalizedNumberInput [precision]="4" formControlName="valor" />
 * ```
 *
 * @category Inputs
 */
class LocalizedNumberInputDirective {
    localeService = inject(LocaleService);
    /** @description Separador decimal personalizado. Usa o do locale ativo se não informado. */
    decimalSeparator;
    /** @description Separador de milhar personalizado. Usa o do locale ativo se não informado. */
    thousandsSeparator;
    /** @description Número máximo de casas decimais aceitas na validação. @default 2 */
    precision = 2;
    numberRegex;
    ngOnInit() {
        // Gera a expressão regular para validar números de acordo com os separadores decimais e de milhares.
        // Exemplo: /^(\+?)(\-?)([0-9]{0,3})*((\,[0-9]{3,3})|((\,[0-9]{3,3})+(\,[0-9]{3,3}))|((\,[0-9]{3,3})+(\.[0-9]{1,10})))?$/g
        // tslint:disable-next-line: max-line-length
        this.numberRegex = `^((\\+?)|(-?))([0-9]{1,3})+((\\${this.thousandsSeparator}[0-9]{3,3})|((\\${this.thousandsSeparator}[0-9]{3,3})+(\\${this.thousandsSeparator}[0-9]{3,3}))|((\\${this.thousandsSeparator}[0-9]{3,3})+(\\${this.decimalSeparator}[0-9]{1,2}))|(\\${this.decimalSeparator}[0-9]{1,${this.precision}}))?$`;
        this.localeService
            .getLocale()
            .pipe(first())
            .subscribe(() => {
            this.decimalSeparator = this.decimalSeparator ?? this.localeService.getDecimalSeparator();
            this.thousandsSeparator = this.thousandsSeparator ?? this.localeService.getGroupingSeparator();
            this.numberRegex = `^((\\+?)|(-?))([0-9]{1,3})+((\\${this.thousandsSeparator}[0-9]{3,3})|((\\${this.thousandsSeparator}[0-9]{3,3})+(\\${this.thousandsSeparator}[0-9]{3,3}))|((\\${this.thousandsSeparator}[0-9]{3,3})+(\\${this.decimalSeparator}[0-9]{1,2}))|(\\${this.decimalSeparator}[0-9]{1,${this.precision}}))?$`;
        });
    }
    validate(control) {
        if (control.value && !String(control.value).match(new RegExp(this.numberRegex ?? '', 'g'))) {
            return { invalidNumber: true };
        }
        return null;
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: LocalizedNumberInputDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.14", type: LocalizedNumberInputDirective, selector: "input[sLocalizedNumberInput]", inputs: { decimalSeparator: "decimalSeparator", thousandsSeparator: "thousandsSeparator", precision: "precision" }, providers: [
            {
                provide: NG_VALIDATORS,
                useExisting: forwardRef(() => LocalizedNumberInputDirective),
                multi: true,
            },
        ], ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: LocalizedNumberInputDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: 'input[sLocalizedNumberInput]',
                    providers: [
                        {
                            provide: NG_VALIDATORS,
                            useExisting: forwardRef(() => LocalizedNumberInputDirective),
                            multi: true,
                        },
                    ],
                }]
        }], propDecorators: { decimalSeparator: [{
                type: Input
            }], thousandsSeparator: [{
                type: Input
            }], precision: [{
                type: Input
            }] } });

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

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

export { LocalizedNumberInputDirective, LocalizedNumberInputModule };
//# sourceMappingURL=seniorsistemas-angular-components-localized-number-input.mjs.map
