import * as i0 from '@angular/core';
import { InjectionToken, NgZone, Directive, Inject, Optional, Input, NgModule, HostListener, Injectable } from '@angular/core';
import * as i1 from '@angular/forms';
import { gaussRound, toNumber, getFractionSeparator } from '@angular-ru/cdk/number';
import { deepClone } from '@angular-ru/cdk/object';
import { removeLastSymbol, getLastSymbol, removeNonNumericSymbols, toStringVal, filter, trim } from '@angular-ru/cdk/string';
import { checkValueIsFilled, detectChanges, isNotNil } from '@angular-ru/cdk/utils';
import { Subscription, fromEvent, Subject, timer } from 'rxjs';
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(el, globalOptions, ngControl, ngZone, cd) {
        this.el = el;
        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.el.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.el.nativeElement.setAttribute('type', 'text');
    }
}
/** @nocollapse */ /** @nocollapse */ AmountFormatDirective.ɵfac = function AmountFormatDirective_Factory(t) { return new (t || AmountFormatDirective)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(AMOUNT_FORMAT_OPTIONS), i0.ɵɵdirectiveInject(i1.NgControl, 8), i0.ɵɵdirectiveInject(i0.NgZone, 8), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef, 8)); };
/** @nocollapse */ /** @nocollapse */ AmountFormatDirective.ɵdir = /** @pureOrBreakMyCode */ i0.ɵɵdefineDirective({ type: AmountFormatDirective, selectors: [["", "amountFormat", ""]], inputs: { amountFormatOptions: "amountFormatOptions" } });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AmountFormatDirective, [{
        type: Directive,
        args: [{ selector: '[amountFormat]' }]
    }], 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
            }] }]; }, { 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 */ /** @nocollapse */ AmountFormatModule.ɵfac = function AmountFormatModule_Factory(t) { return new (t || AmountFormatModule)(); };
/** @nocollapse */ /** @nocollapse */ AmountFormatModule.ɵmod = /** @pureOrBreakMyCode */ i0.ɵɵdefineNgModule({ type: AmountFormatModule });
/** @nocollapse */ /** @nocollapse */ AmountFormatModule.ɵinj = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjector({ imports: [[CommonModule]] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AmountFormatModule, [{
        type: NgModule,
        args: [{
                imports: [CommonModule],
                exports: [AmountFormatDirective],
                declarations: [AmountFormatDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(AmountFormatModule, { declarations: [AmountFormatDirective], imports: [CommonModule], exports: [AmountFormatDirective] }); })();

class ConvertCaseDirective {
    constructor(el, ngControl) {
        this.el = el;
        this.ngControl = ngControl;
        this.toUpperCase = true;
        this.toLowerCase = false;
    }
    get element() {
        return this.el.nativeElement;
    }
    onInput() {
        var _a;
        const dirtyValue = toStringVal(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 */ /** @nocollapse */ ConvertCaseDirective.ɵfac = function ConvertCaseDirective_Factory(t) { return new (t || ConvertCaseDirective)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i1.NgControl, 8)); };
/** @nocollapse */ /** @nocollapse */ ConvertCaseDirective.ɵdir = /** @pureOrBreakMyCode */ i0.ɵɵdefineDirective({ type: ConvertCaseDirective, selectors: [["", "convertCase", ""]], hostBindings: function ConvertCaseDirective_HostBindings(rf, ctx) { if (rf & 1) {
        i0.ɵɵlistener("input", function ConvertCaseDirective_input_HostBindingHandler() { return ctx.onInput(); });
    } }, inputs: { toUpperCase: "toUpperCase", toLowerCase: "toLowerCase" } });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ConvertCaseDirective, [{
        type: Directive,
        args: [{ selector: '[convertCase]' }]
    }], function () { return [{ type: i0.ElementRef }, { type: i1.NgControl, decorators: [{
                type: Optional
            }] }]; }, { toUpperCase: [{
            type: Input
        }], toLowerCase: [{
            type: Input
        }], onInput: [{
            type: HostListener,
            args: ['input']
        }] }); })();

class ConvertCaseModule {
}
/** @nocollapse */ /** @nocollapse */ ConvertCaseModule.ɵfac = function ConvertCaseModule_Factory(t) { return new (t || ConvertCaseModule)(); };
/** @nocollapse */ /** @nocollapse */ ConvertCaseModule.ɵmod = /** @pureOrBreakMyCode */ i0.ɵɵdefineNgModule({ type: ConvertCaseModule });
/** @nocollapse */ /** @nocollapse */ ConvertCaseModule.ɵinj = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjector({ imports: [[CommonModule]] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ConvertCaseModule, [{
        type: NgModule,
        args: [{
                declarations: [ConvertCaseDirective],
                imports: [CommonModule],
                exports: [ConvertCaseDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(ConvertCaseModule, { 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 */ /** @nocollapse */ DisableControlDirective.ɵfac = function DisableControlDirective_Factory(t) { return new (t || DisableControlDirective)(i0.ɵɵdirectiveInject(i1.NgControl, 8)); };
/** @nocollapse */ /** @nocollapse */ DisableControlDirective.ɵdir = /** @pureOrBreakMyCode */ i0.ɵɵdefineDirective({ type: DisableControlDirective, selectors: [["", "disableControl", ""]], inputs: { disableControl: "disableControl" } });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(DisableControlDirective, [{
        type: Directive,
        args: [{ selector: '[disableControl]' }]
    }], function () { return [{ type: i1.NgControl, decorators: [{
                type: Optional
            }] }]; }, { disableControl: [{
            type: Input
        }] }); })();

class DisableControlModule {
}
/** @nocollapse */ /** @nocollapse */ DisableControlModule.ɵfac = function DisableControlModule_Factory(t) { return new (t || DisableControlModule)(); };
/** @nocollapse */ /** @nocollapse */ DisableControlModule.ɵmod = /** @pureOrBreakMyCode */ i0.ɵɵdefineNgModule({ type: DisableControlModule });
/** @nocollapse */ /** @nocollapse */ DisableControlModule.ɵinj = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjector({ imports: [[CommonModule]] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(DisableControlModule, [{
        type: NgModule,
        args: [{
                declarations: [DisableControlDirective],
                imports: [CommonModule],
                exports: [DisableControlDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(DisableControlModule, { 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 */ /** @nocollapse */ InitialFocusDirective.ɵfac = function InitialFocusDirective_Factory(t) { return new (t || InitialFocusDirective)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.NgZone)); };
/** @nocollapse */ /** @nocollapse */ InitialFocusDirective.ɵdir = /** @pureOrBreakMyCode */ i0.ɵɵdefineDirective({ type: InitialFocusDirective, selectors: [["", "initialFocus", ""]], inputs: { focusDelay: "focusDelay", focusDisabled: "focusDisabled", type: "type" } });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(InitialFocusDirective, [{
        type: Directive,
        args: [{ selector: '[initialFocus]' }]
    }], function () { return [{ type: i0.ElementRef }, { type: i0.NgZone }]; }, { focusDelay: [{
            type: Input
        }], focusDisabled: [{
            type: Input
        }], type: [{
            type: Input
        }] }); })();

class InitialFocusModule {
}
/** @nocollapse */ /** @nocollapse */ InitialFocusModule.ɵfac = function InitialFocusModule_Factory(t) { return new (t || InitialFocusModule)(); };
/** @nocollapse */ /** @nocollapse */ InitialFocusModule.ɵmod = /** @pureOrBreakMyCode */ i0.ɵɵdefineNgModule({ type: InitialFocusModule });
/** @nocollapse */ /** @nocollapse */ InitialFocusModule.ɵinj = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjector({ imports: [[CommonModule]] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(InitialFocusModule, [{
        type: NgModule,
        args: [{
                imports: [CommonModule],
                declarations: [InitialFocusDirective],
                exports: [InitialFocusDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(InitialFocusModule, { declarations: [InitialFocusDirective], imports: [CommonModule], exports: [InitialFocusDirective] }); })();

class InputFilterConfig {
    constructor() {
        this.default = null;
    }
}
/** @nocollapse */ /** @nocollapse */ InputFilterConfig.ɵfac = function InputFilterConfig_Factory(t) { return new (t || InputFilterConfig)(); };
/** @nocollapse */ /** @nocollapse */ InputFilterConfig.ɵprov = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjectable({ token: InputFilterConfig, factory: InputFilterConfig.ɵfac });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(InputFilterConfig, [{
        type: Injectable
    }], null, null); })();

class InputFilterDirective {
    constructor(elementRef, config) {
        this.elementRef = elementRef;
        this.config = config;
        this.manualEvent = null;
        this.inputFilter = 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);
        const predicate = isInputPredicate
            ? this.inputFilter
            : (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.default) !== null && _b !== void 0 ? _b : [];
        return predicate;
    }
}
/** @nocollapse */ /** @nocollapse */ InputFilterDirective.ɵfac = function InputFilterDirective_Factory(t) { return new (t || InputFilterDirective)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(InputFilterConfig, 8)); };
/** @nocollapse */ /** @nocollapse */ InputFilterDirective.ɵdir = /** @pureOrBreakMyCode */ i0.ɵɵdefineDirective({ type: InputFilterDirective, selectors: [["", "inputFilter", ""]], hostBindings: function InputFilterDirective_HostBindings(rf, ctx) { if (rf & 1) {
        i0.ɵɵlistener("input", function InputFilterDirective_input_HostBindingHandler($event) { return ctx.onInput($event); });
    } }, inputs: { inputFilter: "inputFilter", filterDisabled: "filterDisabled" }, features: [i0.ɵɵProvidersFeature([ControlValueInterceptor])] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(InputFilterDirective, [{
        type: Directive,
        args: [{
                selector: '[inputFilter]',
                providers: [ControlValueInterceptor]
            }]
    }], function () { return [{ type: i0.ElementRef }, { type: undefined, decorators: [{
                type: Optional
            }, {
                type: Inject,
                args: [InputFilterConfig]
            }] }]; }, { 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 */ /** @nocollapse */ InputFilterModule.ɵfac = function InputFilterModule_Factory(t) { return new (t || InputFilterModule)(); };
/** @nocollapse */ /** @nocollapse */ InputFilterModule.ɵmod = /** @pureOrBreakMyCode */ i0.ɵɵdefineNgModule({ type: InputFilterModule });
/** @nocollapse */ /** @nocollapse */ InputFilterModule.ɵinj = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjector({ imports: [[CommonModule]] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(InputFilterModule, [{
        type: NgModule,
        args: [{
                imports: [CommonModule],
                declarations: [InputFilterDirective],
                exports: [InputFilterDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(InputFilterModule, { declarations: [InputFilterDirective], imports: [CommonModule], 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(trim).filter(checkValueIsFilled);
    }
}
/** @nocollapse */ /** @nocollapse */ SplitStringDirective.ɵfac = function SplitStringDirective_Factory(t) { return new (t || SplitStringDirective)(i0.ɵɵdirectiveInject(i1$1.ControlValueInterceptor)); };
/** @nocollapse */ /** @nocollapse */ SplitStringDirective.ɵdir = /** @pureOrBreakMyCode */ i0.ɵɵdefineDirective({ type: SplitStringDirective, selectors: [["", "splitString", ""]], inputs: { splitOptions: "splitOptions" }, features: [i0.ɵɵProvidersFeature([ControlValueInterceptor])] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SplitStringDirective, [{
        type: Directive,
        args: [{
                selector: '[splitString]',
                providers: [ControlValueInterceptor]
            }]
    }], function () { return [{ type: i1$1.ControlValueInterceptor }]; }, { splitOptions: [{
            type: Input
        }] }); })();

class SplitStringModule {
}
/** @nocollapse */ /** @nocollapse */ SplitStringModule.ɵfac = function SplitStringModule_Factory(t) { return new (t || SplitStringModule)(); };
/** @nocollapse */ /** @nocollapse */ SplitStringModule.ɵmod = /** @pureOrBreakMyCode */ i0.ɵɵdefineNgModule({ type: SplitStringModule });
/** @nocollapse */ /** @nocollapse */ SplitStringModule.ɵinj = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjector({ imports: [[CommonModule]] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SplitStringModule, [{
        type: NgModule,
        args: [{
                declarations: [SplitStringDirective],
                imports: [CommonModule],
                exports: [SplitStringDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(SplitStringModule, { declarations: [SplitStringDirective], imports: [CommonModule], exports: [SplitStringDirective] }); })();

class TrimInputDirective {
    constructor(el, ngControl) {
        this.el = el;
        this.ngControl = ngControl;
        this.disabled = 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.disabled) {
            return;
        }
        this.el.nativeElement.value = (_a = this.el.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 */ /** @nocollapse */ TrimInputDirective.ɵfac = function TrimInputDirective_Factory(t) { return new (t || TrimInputDirective)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i1.NgControl, 8)); };
/** @nocollapse */ /** @nocollapse */ TrimInputDirective.ɵdir = /** @pureOrBreakMyCode */ i0.ɵɵdefineDirective({ type: TrimInputDirective, selectors: [["", "trimInput", ""]], hostBindings: function TrimInputDirective_HostBindings(rf, ctx) { if (rf & 1) {
        i0.ɵɵlistener("keydown.enter", function TrimInputDirective_keydown_enter_HostBindingHandler() { return ctx.onEnter(); })("blur", function TrimInputDirective_blur_HostBindingHandler() { return ctx.onBlur(); });
    } }, inputs: { disabled: ["trimDisabled", "disabled"], formControlName: "formControlName" } });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TrimInputDirective, [{
        type: Directive,
        args: [{ selector: '[trimInput]' }]
    }], function () { return [{ type: i0.ElementRef }, { type: i1.NgControl, decorators: [{
                type: Optional
            }] }]; }, { disabled: [{
            type: Input,
            args: ['trimDisabled']
        }], formControlName: [{
            type: Input
        }], onEnter: [{
            type: HostListener,
            args: ['keydown.enter']
        }], onBlur: [{
            type: HostListener,
            args: ['blur']
        }] }); })();

class TrimInputModule {
}
/** @nocollapse */ /** @nocollapse */ TrimInputModule.ɵfac = function TrimInputModule_Factory(t) { return new (t || TrimInputModule)(); };
/** @nocollapse */ /** @nocollapse */ TrimInputModule.ɵmod = /** @pureOrBreakMyCode */ i0.ɵɵdefineNgModule({ type: TrimInputModule });
/** @nocollapse */ /** @nocollapse */ TrimInputModule.ɵinj = /** @pureOrBreakMyCode */ i0.ɵɵdefineInjector({ imports: [[CommonModule]] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TrimInputModule, [{
        type: NgModule,
        args: [{
                imports: [CommonModule],
                declarations: [TrimInputDirective],
                exports: [TrimInputDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(TrimInputModule, { declarations: [TrimInputDirective], imports: [CommonModule], 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.js.map
