import * as i0 from '@angular/core';
import { Component, Input, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Subject, takeUntil } from 'rxjs';

class ControlErrorsComponent {
    static nextIdSeed = 0;
    static RANDOM_ID_END_INDEX = 9;
    static BASE_36 = 36;
    static _nextId = ControlErrorsComponent.nextIdSeed;
    static getNextId() {
        return this._nextId++;
    }
    id = `s-control-errors-${ControlErrorsComponent.getNextId()}`;
    control;
    errorMessages = {};
    form;
    errorMessagesList = [];
    onDestroy$ = new Subject();
    ngOnChanges(changes) {
        if (changes['control'] && changes['control'].firstChange) {
            this.control.valueChanges?.pipe(takeUntil(this.onDestroy$)).subscribe(() => this.setErrorMessagesList());
        }
    }
    setErrorMessagesList() {
        if (!this.control?.dirty) {
            this.errorMessagesList = [];
        }
        this.errorMessagesList = Object.keys(this.control.errors || {}).map((error) => ({
            id: `${Date.now()}-${Math.random().toString(ControlErrorsComponent.BASE_36).substring(2, ControlErrorsComponent.RANDOM_ID_END_INDEX)}`,
            message: this.errorMessages?.[error],
        }));
    }
    ngOnDestroy() {
        this.onDestroy$.next();
        this.onDestroy$.complete();
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: ControlErrorsComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "18.2.14", type: ControlErrorsComponent, selector: "s-control-errors", inputs: { id: "id", control: "control", errorMessages: "errorMessages", form: "form" }, usesOnChanges: true, ngImport: i0, template: "@for (errorMessage of errorMessagesList; track errorMessage.id) {\n    @if (errorMessage) {\n        <div class=\"error-message\">{{ errorMessage.message }}</div>\n    }\n}\n", styles: [".error-message{color:#c13018;margin-top:5px;font-size:12px}\n"] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: ControlErrorsComponent, decorators: [{
            type: Component,
            args: [{ selector: 's-control-errors', template: "@for (errorMessage of errorMessagesList; track errorMessage.id) {\n    @if (errorMessage) {\n        <div class=\"error-message\">{{ errorMessage.message }}</div>\n    }\n}\n", styles: [".error-message{color:#c13018;margin-top:5px;font-size:12px}\n"] }]
        }], propDecorators: { id: [{
                type: Input
            }], control: [{
                type: Input,
                args: [{ required: true }]
            }], errorMessages: [{
                type: Input
            }], form: [{
                type: Input
            }] } });

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

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

export { ControlErrorsComponent, ControlErrorsModule };
//# sourceMappingURL=seniorsistemas-angular-components-control-errors.mjs.map
