import * as i0 from '@angular/core';
import { Pipe, NgModule } from '@angular/core';

/**
 * @description Pipe que aplica uma máscara de formatação a uma string,
 * suportando os tokens `9` (dígito), `a` (letra) e `*` (alfanumérico),
 * além de suporte a posições opcionais com `?`.
 *
 * @example
 * ```html
 * {{ '12345678901' | sMaskFormatter:'999.999.999-99' }}
 * ```
 *
 * @category Data
 */
class MaskFormatterPipe {
    _defs = {
        '9': '[0-9]',
        a: '[A-Za-z]',
        '*': '[A-Za-z]|[0-9]',
    };
    transform(value, maskFormat) {
        const buffer = [];
        let opcionalPos = -1;
        let pos = 0;
        const maskTokens = maskFormat.split('');
        const symbolsPosAfterOpcional = [];
        const hasOpcional = maskFormat.includes('?');
        for (let i = 0; i < maskTokens.length; i++) {
            const token = maskTokens[i];
            if (token !== '?') {
                if (this._defs[token]) {
                    const tokenRegex = this._defs[token];
                    const charAtPos = value.charAt(pos);
                    const isValidChar = new RegExp(tokenRegex).test(charAtPos);
                    if (isValidChar) {
                        buffer.push(charAtPos);
                        pos++;
                    }
                    else {
                        throw new Error("Value doesn't match mask format");
                    }
                }
                else {
                    buffer.push(token);
                    if (hasOpcional && opcionalPos !== -1 && i > opcionalPos) {
                        symbolsPosAfterOpcional.push(i);
                    }
                }
            }
            else {
                opcionalPos = i;
            }
        }
        if (buffer[buffer.length - 1] !== value.charAt(value.length - 1) && symbolsPosAfterOpcional.length) {
            let formattedValue = buffer.join('');
            for (let i = 0; i < symbolsPosAfterOpcional.length; i++) {
                const symbolPos = symbolsPosAfterOpcional[i];
                const beforeSymbol = formattedValue.substring(0, symbolPos - 1);
                const symbol = formattedValue.substring(symbolPos - 1, symbolPos);
                const firstAfterSymbol = formattedValue.substring(symbolPos, symbolPos + 1);
                const endOfString = formattedValue.substring(symbolPos + 1);
                formattedValue = `${beforeSymbol}${firstAfterSymbol}${symbol}${endOfString}`;
            }
            return `${formattedValue}${value.substr(value.length - symbolsPosAfterOpcional.length)}`;
        }
        return buffer.join('');
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: MaskFormatterPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
    static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: MaskFormatterPipe, name: "sMaskFormatter" });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: MaskFormatterPipe, decorators: [{
            type: Pipe,
            args: [{
                    name: 'sMaskFormatter',
                }]
        }] });

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

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

export { MaskFormatterModule, MaskFormatterPipe };
//# sourceMappingURL=seniorsistemas-angular-components-mask.mjs.map
