import * as i0 from '@angular/core';
import { InjectionToken, NgZone, Directive, Inject, Optional, Input, NgModule, HostListener, Injectable } from '@angular/core';
import { gaussRound, toNumber, getFractionSeparator } from '@angular-ru/cdk/number';
import { deepClone } from '@angular-ru/cdk/object';
import { removeLastSymbol, getLastSymbol, removeNonNumericSymbols, toStringValue, filter, trim } from '@angular-ru/cdk/string';
import { checkValueIsFilled, detectChanges, isNotNil } from '@angular-ru/cdk/utils';
import { Subscription, fromEvent, Subject, timer } from 'rxjs';
import * as i1 from '@angular/forms';
import { CommonModule } from '@angular/common';
import { takeUntil } from 'rxjs/operators';
import { hasItems } from '@angular-ru/cdk/array';
import * as i1$1 from '@angular-ru/cdk/forms';
import { ControlValueInterceptor } from '@angular-ru/cdk/forms';

const AMOUNT_FORMAT_OPTIONS = new InjectionToken('AMOUNT_FORMAT_OPTIONS');
const DEFAULT_AMOUNT_OPTIONS = { lang: 'ru-RU' };

class AmountFormatDirective {
    constructor(elementRef, globalOptions, ngControl, ngZone, cd) {
        this.elementRef = elementRef;
        this.ngControl = ngControl;
        this.ngZone = ngZone;
        this.cd = cd;
        this.subscriptions = new Subscription();
        this.previousLang = null;
        this.maximumFractionDigits = 3;
        this.isInsideAngularZone = true;
        this.options = {};
        this.markedAsDirty = true;
        this.cursorPointer = 0;
        this.setFirstLocalOptionsByGlobal(globalOptions);
    }
    get isInAngularZone() {
        return this.isInsideAngularZone;
    }
    get dirty() {
        return this.markedAsDirty;
    }
    get element() {
        return this.elementRef.nativeElement;
    }
    get amountFormatOptions() {
        return this.options;
    }
    set amountFormatOptions(options) {
        this.options = Object.assign(Object.assign({}, this.options), (options !== null && options !== void 0 ? options : {}));
        this.recalculateWhenChangesOptions();
    }
    setLang(lang) {
        this.options.lang = lang;
        this.recalculateWhenChangesOptions();
    }
    getCursorPosition() {
        return this.cursorPointer;
    }
    ngOnInit() {
        this.setupElementType();
        this.subscribeToElementEvents();
    }
    ngAfterViewInit() {
        this.format();
    }
    ngOnDestroy() {
        this.unsubscribeFromElementEvents();
    }
    subscribeToElementEvents() {
        var _a;
        (_a = this.ngZone) === null || _a === void 0 ? void 0 : _a.runOutsideAngular(() => {
            this.subscriptions.add(fromEvent(this.element, 'input').subscribe(() => {
                this.isInsideAngularZone = NgZone.isInAngularZone();
                this.format();
            }));
            this.subscriptions.add(fromEvent(this.element, 'blur').subscribe(() => {
                this.isInsideAngularZone = NgZone.isInAngularZone();
                this.formatOnBlur();
            }));
        });
    }
    unsubscribeFromElementEvents() {
        this.subscriptions.unsubscribe();
    }
    formatOnBlur() {
        var _a;
        if (this.isIncorrectPreviewAfterBlur()) {
            this.element.value = (_a = removeLastSymbol(this.element.value)) !== null && _a !== void 0 ? _a : '';
        }
    }
    format() {
        var _a, _b;
        const fraction = this.getFractionSeparator();
        this.cursorPointer = (_a = this.element.selectionStart) !== null && _a !== void 0 ? _a : 0;
        const isInSafeSelectionPosition = this.cursorPointer !== this.element.value.length;
        this.replaceAllInvalidSymbolsBeforeTranslation();
        if (this.viewModelIsOnlyMinus()) {
            this.resetModelValue();
            // Note: should be set custom view model value
            // because when we trigger ngControl?.reset
            // it's also reset view model
            this.element.value = '-';
            return;
        }
        const originValueBeforeReset = this.element.value;
        const numberValue = this.getNumberValueWithGaussRounded();
        this.setModelValueBy(numberValue);
        this.element.value = originValueBeforeReset;
        const stringValue = this.prepareConvertedToLocaleValue(numberValue, fraction);
        // Note: we should reset view model value
        // when user set invalid number value as `15,,,,,0` or `15.00.00.00.00`
        this.element.value = isNaN((_b = this.ngControl) === null || _b === void 0 ? void 0 : _b.value) ? '' : stringValue;
        this.setSelectionRangeBy(isInSafeSelectionPosition, stringValue);
        this.preventExpressionChangedAfter();
    }
    isIncorrectPreviewAfterBlur() {
        return this.lastSymbolIsFraction() || this.viewModelIsOnlyMinus();
    }
    /**
     * note: after reset model value then control override view model
     */
    resetModelValue(value = null) {
        var _a;
        (_a = this.ngControl) === null || _a === void 0 ? void 0 : _a.reset(value);
    }
    replaceAllInvalidSymbolsBeforeTranslation() {
        const fraction = this.getFractionSeparator();
        this.element.value = this.removeNonNumericSymbols();
        this.element.value = this.replaceInvalidFractionPosition(fraction);
        this.element.value = this.removeDuplicateMinusOrFractionSymbol(fraction);
    }
    getMaximumFractionDigits() {
        var _a, _b;
        return (_b = (_a = this.options.formatOptions) === null || _a === void 0 ? void 0 : _a.maximumFractionDigits) !== null && _b !== void 0 ? _b : this.maximumFractionDigits;
    }
    getLastSymbolsAsZeroDot(fraction) {
        var _a, _b, _c;
        const maximumFractionDigits = this.getMaximumFractionDigits();
        let lastSymbolsAsZeroDot = maximumFractionDigits === 0
            ? ''
            : (_c = (_b = (_a = this.element.value.match(new RegExp(`(\\${fraction})(.+)?`))) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.replace(/,|./, '')) !== null && _c !== void 0 ? _c : '';
        const isOverflowGaussRound = lastSymbolsAsZeroDot.length > this.maximumFractionDigits;
        if (isOverflowGaussRound) {
            const parsedDot = gaussRound(toNumber(`0${fraction}${lastSymbolsAsZeroDot}`, this.options.lang), maximumFractionDigits);
            lastSymbolsAsZeroDot = isNaN(parsedDot) ? '' : parsedDot.toString().replace(/0\./, '');
        }
        return lastSymbolsAsZeroDot;
    }
    prepareConvertedToLocaleValue(numberValueWithGaussRounded, fraction) {
        const lastSymbolAsFraction = this.lastSymbolIsFraction();
        let convertedToLocaleValue = this.getConvertedToLocaleString(numberValueWithGaussRounded);
        if (lastSymbolAsFraction) {
            convertedToLocaleValue = `${convertedToLocaleValue}${fraction}`;
        }
        else {
            const lastSymbolsAsZeroDot = this.getLastSymbolsAsZeroDot(fraction);
            if (checkValueIsFilled(lastSymbolsAsZeroDot)) {
                const splitValues = convertedToLocaleValue.split(fraction);
                const beforePoint = splitValues === null || splitValues === void 0 ? void 0 : splitValues[0];
                if (checkValueIsFilled(beforePoint)) {
                    convertedToLocaleValue = `${beforePoint}${fraction}${lastSymbolsAsZeroDot}`;
                }
                else {
                    convertedToLocaleValue = `0${fraction}${lastSymbolsAsZeroDot}`;
                }
            }
        }
        return convertedToLocaleValue.replace(/\s/g, ' ');
    }
    setModelValueBy(numberValue) {
        if (isNaN(numberValue)) {
            this.resetModelValue();
        }
        else {
            this.resetModelValue(numberValue);
        }
    }
    getConvertedToLocaleString(numberValue) {
        return (isNaN(numberValue) ? '' : numberValue).toLocaleString(this.options.lang, this.options.formatOptions);
    }
    getNumberValueWithGaussRounded() {
        const maximumFractionDigits = this.getMaximumFractionDigits();
        return gaussRound(toNumber(this.element.value, this.options.lang), maximumFractionDigits);
    }
    lastSymbolIsFraction() {
        return getLastSymbol(this.element.value) === this.getFractionSeparator();
    }
    replaceInvalidFractionPosition(fraction) {
        return this.element.value.replace(new RegExp(`\\${fraction}+$`, 'g'), fraction);
    }
    removeDuplicateMinusOrFractionSymbol(fraction) {
        let value = this.element.value;
        let count = 0;
        value = value.replace(new RegExp(`\\${fraction}`, 'g'), () => {
            count++;
            return count > 1 ? '' : fraction;
        });
        if (value.indexOf('-') !== 0) {
            value = value.replace(/-/g, '');
        }
        return value;
    }
    viewModelIsOnlyMinus() {
        return this.element.value === '-';
    }
    getFractionSeparator() {
        var _a;
        return getFractionSeparator((_a = this.options.lang) !== null && _a !== void 0 ? _a : DEFAULT_AMOUNT_OPTIONS.lang);
    }
    removeNonNumericSymbols() {
        return this.viewModelIsOnlyMinus() ? this.element.value : removeNonNumericSymbols(this.element.value);
    }
    setSelectionRangeBy(safePosition, value) {
        try {
            if (safePosition) {
                this.element.setSelectionRange(this.cursorPointer, this.cursorPointer);
            }
            else {
                this.cursorPointer = value.length;
                this.element.setSelectionRange(this.cursorPointer, this.cursorPointer);
            }
        }
        catch (_a) {
            // Caretaker note:
            // this situation can happen if <input type=number />
            // at some point, the directive may not have time to work and set the value `type=text`
        }
    }
    setFirstLocalOptionsByGlobal(options) {
        this.options = deepClone(options);
        this.previousLang = this.options.lang;
    }
    recalculateWhenChangesOptions() {
        var _a;
        const value = toNumber(this.element.value, (_a = this.previousLang) !== null && _a !== void 0 ? _a : this.options.lang);
        this.element.value = value.toLocaleString(this.options.lang, this.options.formatOptions);
        this.previousLang = this.options.lang;
        this.format();
    }
    preventExpressionChangedAfter() {
        if (this.markedAsDirty) {
            detectChanges(this.cd);
            this.markedAsDirty = false;
        }
    }
    setupElementType() {
        this.elementRef.nativeElement.setAttribute('type', 'text');
    }
}
/** @nocollapse */ AmountFormatDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: AmountFormatDirective, deps: [{ token: i0.ElementRef }, { token: AMOUNT_FORMAT_OPTIONS }, { token: i1.NgControl, optional: true }, { token: i0.NgZone, optional: true }, { token: i0.ChangeDetectorRef, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
/** @nocollapse */ AmountFormatDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.3.2", type: AmountFormatDirective, selector: "[amountFormat]", inputs: { amountFormatOptions: "amountFormatOptions" }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: AmountFormatDirective, decorators: [{
            type: Directive,
            args: [{ selector: '[amountFormat]' }]
        }], ctorParameters: function () {
        return [{ type: i0.ElementRef }, { type: undefined, decorators: [{
                        type: Inject,
                        args: [AMOUNT_FORMAT_OPTIONS]
                    }] }, { type: i1.NgControl, decorators: [{
                        type: Optional
                    }] }, { type: i0.NgZone, decorators: [{
                        type: Optional
                    }] }, { type: i0.ChangeDetectorRef, decorators: [{
                        type: Optional
                    }] }];
    }, propDecorators: { amountFormatOptions: [{
                type: Input
            }] } });

class AmountFormatModule {
    static forRoot(options = {}) {
        return {
            ngModule: AmountFormatModule,
            providers: [{ provide: AMOUNT_FORMAT_OPTIONS, useValue: Object.assign(Object.assign({}, DEFAULT_AMOUNT_OPTIONS), options) }]
        };
    }
}
/** @nocollapse */ AmountFormatModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: AmountFormatModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ AmountFormatModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: AmountFormatModule, declarations: [AmountFormatDirective], imports: [CommonModule], exports: [AmountFormatDirective] });
/** @nocollapse */ AmountFormatModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: AmountFormatModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: AmountFormatModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    declarations: [AmountFormatDirective],
                    exports: [AmountFormatDirective]
                }]
        }] });

class ConvertCaseDirective {
    constructor(elementRef, ngControl) {
        this.elementRef = elementRef;
        this.ngControl = ngControl;
        this.toUpperCase = true;
        this.toLowerCase = false;
    }
    get element() {
        return this.elementRef.nativeElement;
    }
    onInput() {
        var _a;
        const dirtyValue = toStringValue(this.element.value);
        this.element.value = this.toLowerCase ? dirtyValue.toLowerCase() : dirtyValue.toUpperCase();
        (_a = this.ngControl) === null || _a === void 0 ? void 0 : _a.reset(this.element.value);
    }
    ngAfterViewInit() {
        if (this.element.value) {
            this.onInput();
        }
    }
}
/** @nocollapse */ ConvertCaseDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: ConvertCaseDirective, deps: [{ token: i0.ElementRef }, { token: i1.NgControl, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
/** @nocollapse */ ConvertCaseDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.3.2", type: ConvertCaseDirective, selector: "[convertCase]", inputs: { toUpperCase: "toUpperCase", toLowerCase: "toLowerCase" }, host: { listeners: { "input": "onInput()" } }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: ConvertCaseDirective, decorators: [{
            type: Directive,
            args: [{ selector: '[convertCase]' }]
        }], ctorParameters: function () {
        return [{ type: i0.ElementRef }, { type: i1.NgControl, decorators: [{
                        type: Optional
                    }] }];
    }, propDecorators: { toUpperCase: [{
                type: Input
            }], toLowerCase: [{
                type: Input
            }], onInput: [{
                type: HostListener,
                args: ['input']
            }] } });

class ConvertCaseModule {
}
/** @nocollapse */ ConvertCaseModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: ConvertCaseModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ ConvertCaseModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: ConvertCaseModule, declarations: [ConvertCaseDirective], imports: [CommonModule], exports: [ConvertCaseDirective] });
/** @nocollapse */ ConvertCaseModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: ConvertCaseModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: ConvertCaseModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [ConvertCaseDirective],
                    imports: [CommonModule],
                    exports: [ConvertCaseDirective]
                }]
        }] });

class DisableControlDirective {
    constructor(ngControl) {
        this.ngControl = ngControl;
    }
    set disableControl(condition) {
        var _a, _b, _c;
        const action = condition ? 'disable' : 'enable';
        (_c = (_b = (_a = this.ngControl) === null || _a === void 0 ? void 0 : _a.control) === null || _b === void 0 ? void 0 : _b[action]) === null || _c === void 0 ? void 0 : _c.call(_b);
    }
}
/** @nocollapse */ DisableControlDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: DisableControlDirective, deps: [{ token: i1.NgControl, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
/** @nocollapse */ DisableControlDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.3.2", type: DisableControlDirective, selector: "[disableControl]", inputs: { disableControl: "disableControl" }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: DisableControlDirective, decorators: [{
            type: Directive,
            args: [{ selector: '[disableControl]' }]
        }], ctorParameters: function () {
        return [{ type: i1.NgControl, decorators: [{
                        type: Optional
                    }] }];
    }, propDecorators: { disableControl: [{
                type: Input
            }] } });

class DisableControlModule {
}
/** @nocollapse */ DisableControlModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: DisableControlModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ DisableControlModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: DisableControlModule, declarations: [DisableControlDirective], imports: [CommonModule], exports: [DisableControlDirective] });
/** @nocollapse */ DisableControlModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: DisableControlModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: DisableControlModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [DisableControlDirective],
                    imports: [CommonModule],
                    exports: [DisableControlDirective]
                }]
        }] });

const MIN_DELAY = 500;
class InitialFocusDirective {
    constructor(element, ngZone) {
        this.element = element;
        this.ngZone = ngZone;
        this.className = 'initial-focused';
        this.unsubscribe$ = new Subject();
        this.focusDelay = MIN_DELAY;
        this.focusDisabled = false;
    }
    get el() {
        return this.element.nativeElement;
    }
    ngAfterViewInit() {
        this.decideAndTryToFocus();
    }
    ngOnDestroy() {
        this.unsubscribe$.next(true);
    }
    decideAndTryToFocus() {
        if (!this.focusDisabled && !this.isFocused()) {
            this.ngZone.runOutsideAngular(() => {
                timer(this.focusDelay)
                    .pipe(takeUntil(this.unsubscribe$))
                    .subscribe((_value) => {
                    this.focus();
                });
            });
        }
    }
    isFocused() {
        return this.elementIsActive() || this.el.classList.contains(this.className);
    }
    elementIsActive() {
        return document.activeElement === this.element.nativeElement;
    }
    focus() {
        this.el.focus();
        // selection range doesn't work with number type
        if (this.type === 'number') {
            this.el.type = 'text';
        }
        this.el.setSelectionRange(this.el.value.length, this.el.value.length);
        if (this.type === 'number') {
            this.el.type = 'number';
        }
        if (this.elementIsActive()) {
            this.el.classList.add(this.className);
        }
    }
}
/** @nocollapse */ InitialFocusDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InitialFocusDirective, deps: [{ token: i0.ElementRef }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive });
/** @nocollapse */ InitialFocusDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.3.2", type: InitialFocusDirective, selector: "[initialFocus]", inputs: { focusDelay: "focusDelay", focusDisabled: "focusDisabled", type: "type" }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InitialFocusDirective, decorators: [{
            type: Directive,
            args: [{ selector: '[initialFocus]' }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.NgZone }]; }, propDecorators: { focusDelay: [{
                type: Input
            }], focusDisabled: [{
                type: Input
            }], type: [{
                type: Input
            }] } });

class InitialFocusModule {
}
/** @nocollapse */ InitialFocusModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InitialFocusModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ InitialFocusModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InitialFocusModule, declarations: [InitialFocusDirective], imports: [CommonModule], exports: [InitialFocusDirective] });
/** @nocollapse */ InitialFocusModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InitialFocusModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InitialFocusModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    declarations: [InitialFocusDirective],
                    exports: [InitialFocusDirective]
                }]
        }] });

class InputFilterConfig {
    constructor() {
        this.default = null;
    }
}
/** @nocollapse */ InputFilterConfig.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterConfig, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
/** @nocollapse */ InputFilterConfig.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterConfig });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterConfig, decorators: [{
            type: Injectable
        }] });

class InputFilterDirective {
    constructor(elementRef, config) {
        this.elementRef = elementRef;
        this.config = config;
        this.manualEvent = null;
        this.filterDisabled = false;
    }
    onInput(baseEvent) {
        var _a, _b;
        if (this.filterDisabled || this.manualEvent === baseEvent) {
            return;
        }
        const predicate = this.getPredicate();
        const baseValue = this.elementRef.nativeElement.value;
        const preparedValue = filter(baseValue, predicate);
        if (preparedValue === baseValue) {
            return;
        }
        this.manualEvent = new InputEvent('input', Object.assign(Object.assign({}, baseEvent), { data: preparedValue }));
        this.elementRef.nativeElement.value = (_b = (_a = this.manualEvent) === null || _a === void 0 ? void 0 : _a.data) !== null && _b !== void 0 ? _b : '';
        this.elementRef.nativeElement.dispatchEvent(this.manualEvent);
    }
    getPredicate() {
        var _a, _b;
        const isInputPredicate = Array.isArray(this.inputFilter)
            ? hasItems(this.inputFilter)
            : checkValueIsFilled(this.inputFilter);
        return isInputPredicate ? this.inputFilter : (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.default) !== null && _b !== void 0 ? _b : [];
    }
}
/** @nocollapse */ InputFilterDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterDirective, deps: [{ token: i0.ElementRef }, { token: InputFilterConfig, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
/** @nocollapse */ InputFilterDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.3.2", type: InputFilterDirective, selector: "[inputFilter]", inputs: { inputFilter: "inputFilter", filterDisabled: "filterDisabled" }, host: { listeners: { "input": "onInput($event)" } }, providers: [ControlValueInterceptor], ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[inputFilter]',
                    providers: [ControlValueInterceptor]
                }]
        }], ctorParameters: function () {
        return [{ type: i0.ElementRef }, { type: undefined, decorators: [{
                        type: Optional
                    }, {
                        type: Inject,
                        args: [InputFilterConfig]
                    }] }];
    }, propDecorators: { inputFilter: [{
                type: Input
            }], filterDisabled: [{
                type: Input
            }], onInput: [{
                type: HostListener,
                args: ['input', ['$event']]
            }] } });

class InputFilterModule {
    static forRoot(config) {
        return {
            ngModule: InputFilterModule,
            providers: [
                {
                    provide: InputFilterConfig,
                    useValue: config
                }
            ]
        };
    }
    static forChild(config) {
        return {
            ngModule: InputFilterModule,
            providers: [
                {
                    provide: InputFilterConfig,
                    useValue: config
                }
            ]
        };
    }
}
/** @nocollapse */ InputFilterModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ InputFilterModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterModule, declarations: [InputFilterDirective], imports: [CommonModule], exports: [InputFilterDirective] });
/** @nocollapse */ InputFilterModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: InputFilterModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    declarations: [InputFilterDirective],
                    exports: [InputFilterDirective]
                }]
        }] });

class SplitStringDirective {
    constructor(interceptor) {
        this.defaultSplitOptions = {
            separator: /[\n,;]/g,
            joinWith: ', '
        };
        interceptor.attach({
            toModelValue: (viewValue) => this.splitAndTrimViewValue(viewValue),
            toViewValue: (modelValue) => {
                var _a, _b;
                return Array.isArray(modelValue)
                    ? modelValue.join((_b = (_a = this.splitOptions) === null || _a === void 0 ? void 0 : _a.joinWith) !== null && _b !== void 0 ? _b : this.defaultSplitOptions.joinWith)
                    : modelValue;
            }
        });
    }
    splitAndTrimViewValue(viewValue) {
        var _a, _b;
        const separator = (_b = (_a = this.splitOptions) === null || _a === void 0 ? void 0 : _a.separator) !== null && _b !== void 0 ? _b : this.defaultSplitOptions.separator;
        return viewValue
            .split(separator)
            .map((element) => trim(element))
            .filter((element) => checkValueIsFilled(element));
    }
}
/** @nocollapse */ SplitStringDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: SplitStringDirective, deps: [{ token: i1$1.ControlValueInterceptor }], target: i0.ɵɵFactoryTarget.Directive });
/** @nocollapse */ SplitStringDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.3.2", type: SplitStringDirective, selector: "[splitString]", inputs: { splitOptions: "splitOptions" }, providers: [ControlValueInterceptor], ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: SplitStringDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[splitString]',
                    providers: [ControlValueInterceptor]
                }]
        }], ctorParameters: function () { return [{ type: i1$1.ControlValueInterceptor }]; }, propDecorators: { splitOptions: [{
                type: Input
            }] } });

class SplitStringModule {
}
/** @nocollapse */ SplitStringModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: SplitStringModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ SplitStringModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: SplitStringModule, declarations: [SplitStringDirective], imports: [CommonModule], exports: [SplitStringDirective] });
/** @nocollapse */ SplitStringModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: SplitStringModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: SplitStringModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [SplitStringDirective],
                    imports: [CommonModule],
                    exports: [SplitStringDirective]
                }]
        }] });

class TrimInputDirective {
    constructor(elementRef, ngControl) {
        this.elementRef = elementRef;
        this.ngControl = ngControl;
        this.trimDisabled = false;
    }
    set formControlName(name) {
        var _a, _b, _c, _d;
        this.previousValue = (_d = (_c = (_b = (_a = this.ngControl) === null || _a === void 0 ? void 0 : _a.control) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.get(this.name)) === null || _d === void 0 ? void 0 : _d.value;
        this.previousName = this.name;
        this.name = name;
    }
    onEnter() {
        this.trimValue();
    }
    onBlur() {
        this.trimValue();
    }
    ngOnInit() {
        this.trimValue();
    }
    trimValue() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
        if (this.trimDisabled) {
            return;
        }
        this.elementRef.nativeElement.value = (_a = this.elementRef.nativeElement.value) === null || _a === void 0 ? void 0 : _a.toString().trim();
        const control = ((_c = (_b = this.ngControl) === null || _b === void 0 ? void 0 : _b.control) === null || _c === void 0 ? void 0 : _c.parent)
            ? (_f = (_e = (_d = this.ngControl) === null || _d === void 0 ? void 0 : _d.control) === null || _e === void 0 ? void 0 : _e.parent) === null || _f === void 0 ? void 0 : _f.get(this.name)
            : (_h = (_g = this.ngControl) === null || _g === void 0 ? void 0 : _g.control) === null || _h === void 0 ? void 0 : _h.get(this.name);
        if (isNotNil(control)) {
            const modelValue = (_l = ((_k = (_j = this.ngControl) === null || _j === void 0 ? void 0 : _j.value) !== null && _k !== void 0 ? _k : control === null || control === void 0 ? void 0 : control.value)) === null || _l === void 0 ? void 0 : _l.toString().trim();
            if (((_m = this.ngControl) === null || _m === void 0 ? void 0 : _m.control) === control) {
                (_p = (_o = this.ngControl) === null || _o === void 0 ? void 0 : _o.control) === null || _p === void 0 ? void 0 : _p.setValue(modelValue, { emitEvent: false });
            }
            else {
                control === null || control === void 0 ? void 0 : control.setValue(modelValue, { emitEvent: false });
                (_r = (_q = control === null || control === void 0 ? void 0 : control.parent) === null || _q === void 0 ? void 0 : _q.get(this.previousName)) === null || _r === void 0 ? void 0 : _r.setValue(this.previousValue, { emitEvent: false });
            }
        }
    }
}
/** @nocollapse */ TrimInputDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: TrimInputDirective, deps: [{ token: i0.ElementRef }, { token: i1.NgControl, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
/** @nocollapse */ TrimInputDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.3.2", type: TrimInputDirective, selector: "[trimInput]", inputs: { trimDisabled: "trimDisabled", formControlName: "formControlName" }, host: { listeners: { "keydown.enter": "onEnter()", "blur": "onBlur()" } }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: TrimInputDirective, decorators: [{
            type: Directive,
            args: [{ selector: '[trimInput]' }]
        }], ctorParameters: function () {
        return [{ type: i0.ElementRef }, { type: i1.NgControl, decorators: [{
                        type: Optional
                    }] }];
    }, propDecorators: { trimDisabled: [{
                type: Input
            }], formControlName: [{
                type: Input
            }], onEnter: [{
                type: HostListener,
                args: ['keydown.enter']
            }], onBlur: [{
                type: HostListener,
                args: ['blur']
            }] } });

class TrimInputModule {
}
/** @nocollapse */ TrimInputModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: TrimInputModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ TrimInputModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: TrimInputModule, declarations: [TrimInputDirective], imports: [CommonModule], exports: [TrimInputDirective] });
/** @nocollapse */ TrimInputModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: TrimInputModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImport: i0, type: TrimInputModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    declarations: [TrimInputDirective],
                    exports: [TrimInputDirective]
                }]
        }] });

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

export { AMOUNT_FORMAT_OPTIONS, AmountFormatDirective, AmountFormatModule, ConvertCaseDirective, ConvertCaseModule, DEFAULT_AMOUNT_OPTIONS, DisableControlDirective, DisableControlModule, InitialFocusDirective, InitialFocusModule, InputFilterConfig, InputFilterDirective, InputFilterModule, SplitStringDirective, SplitStringModule, TrimInputDirective, TrimInputModule };
//# sourceMappingURL=angular-ru-cdk-directives.mjs.map
