import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Directive, NgModule } from '@angular/core';
import { Destroy } from '@deja-js/component/core';
import { of, fromEvent, mergeWith, startWith, debounceTime, takeUntil } from 'rxjs';
import * as i1 from '@angular/material/input';

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class InputAutosizeDirective extends Destroy {
    constructor(elementRef, ngZone, matInput) {
        super();
        this.elementRef = elementRef;
        this.ngZone = ngZone;
        this.matInput = matInput;
    }
    ngOnInit() {
        const inputElement = this.elementRef.nativeElement;
        const valueChanges$ = this.matInput?.ngControl?.valueChanges || of(null);
        this.ngZone.runOutsideAngular(() => {
            fromEvent(inputElement, 'input').pipe(mergeWith(fromEvent(inputElement, 'paste'), valueChanges$), startWith(inputElement.value), debounceTime(5), takeUntil(this.destroyed$)).subscribe(() => {
                const nbChar = inputElement.value?.length;
                // Forced to have a fallback value as the "style" is higher than class/usual style in the hierarchy
                inputElement.style.width = nbChar ? `${nbChar}ch` : '1ch';
                inputElement.style.maxWidth = '100%';
            });
        });
    }
}
InputAutosizeDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: InputAutosizeDirective, deps: [{ token: i0.ElementRef }, { token: i0.NgZone }, { token: i1.MatInput }], target: i0.ɵɵFactoryTarget.Directive });
InputAutosizeDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.1.3", type: InputAutosizeDirective, selector: "[inputAutosize][matInput], [inputAutosize] [matInput]", exportAs: ["inputAutosize"], usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: InputAutosizeDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[inputAutosize][matInput], [inputAutosize] [matInput]',
                    exportAs: 'inputAutosize'
                }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.NgZone }, { type: i1.MatInput }]; } });

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class InputAutosizeModule {
}
InputAutosizeModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: InputAutosizeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
InputAutosizeModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.3", ngImport: i0, type: InputAutosizeModule, declarations: [InputAutosizeDirective], imports: [CommonModule], exports: [InputAutosizeDirective] });
InputAutosizeModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: InputAutosizeModule, imports: [CommonModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: InputAutosizeModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [
                        CommonModule
                    ],
                    exports: [InputAutosizeDirective],
                    declarations: [InputAutosizeDirective]
                }]
        }] });

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

export { InputAutosizeDirective, InputAutosizeModule };
//# sourceMappingURL=deja-js-component-input-autosize.mjs.map
