import * as i0 from '@angular/core';
import { Pipe, input, booleanAttribute, Component } from '@angular/core';
import { Message } from 'primeng/message';
import { isInvalid } from '@masterteam/components';

class ErrorMessagePipe {
    transform(errors) {
        if (!errors) {
            return '';
        }
        // Handle standard Angular validator errors first
        if (errors['required']) {
            return 'This field is required';
        }
        if (errors['email']) {
            return 'Please enter a valid email address';
        }
        if (errors['minlength']) {
            const { requiredLength, actualLength } = errors['minlength'];
            return `Minimum length is ${requiredLength} characters (current: ${actualLength})`;
        }
        if (errors['maxlength']) {
            const { requiredLength, actualLength } = errors['maxlength'];
            return `Maximum length is ${requiredLength} characters (current: ${actualLength})`;
        }
        if (errors['min']) {
            const { min, actual } = errors['min'];
            return `Minimum value is ${min} (current: ${actual})`;
        }
        if (errors['max']) {
            const { max, actual } = errors['max'];
            return `Maximum value is ${max} (current: ${actual})`;
        }
        if (errors['pattern']) {
            return 'The format is invalid';
        }
        if (errors['fileSizeLimited']) {
            return 'File size exceeds the limit';
        }
        // Handle custom errors that provide their own message
        // This looks for an error structured like: { myCustomError: { message: '...' } }
        for (const key in errors) {
            const errorValue = errors[key];
            if (errorValue && typeof errorValue === 'object' && errorValue.message) {
                return errorValue.message;
            }
        }
        // Fallback for any other error
        const firstErrorKey = Object.keys(errors)[0];
        return `Invalid field: ${firstErrorKey}`;
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.3", ngImport: i0, type: ErrorMessagePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
    static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "21.0.3", ngImport: i0, type: ErrorMessagePipe, isStandalone: true, name: "errorMessage" });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.3", ngImport: i0, type: ErrorMessagePipe, decorators: [{
            type: Pipe,
            args: [{
                    name: 'errorMessage',
                    standalone: true,
                    pure: true,
                }]
        }] });

class FieldValidation {
    control = input(null, ...(ngDevMode ? [{ debugName: "control" }] : []));
    touched = input(false, { ...(ngDevMode ? { debugName: "touched" } : {}), transform: booleanAttribute });
    isInvalid = isInvalid;
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.3", ngImport: i0, type: FieldValidation, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.0.3", type: FieldValidation, isStandalone: true, selector: "mt-field-validation", inputs: { control: { classPropertyName: "control", publicName: "control", isSignal: true, isRequired: false, transformFunction: null }, touched: { classPropertyName: "touched", publicName: "touched", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "class.hidden": "!isInvalid(control())" } }, ngImport: i0, template: `
    @if (isInvalid(control())) {
      <p-message
        severity="error"
        variant="simple"
        size="small"
        [text]="control().errors | errorMessage"
      >
      </p-message>
    }
  `, isInline: true, dependencies: [{ kind: "component", type: Message, selector: "p-message", inputs: ["severity", "text", "escape", "style", "styleClass", "closable", "icon", "closeIcon", "life", "showTransitionOptions", "hideTransitionOptions", "size", "variant", "motionOptions"], outputs: ["onClose"] }, { kind: "pipe", type: ErrorMessagePipe, name: "errorMessage" }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.3", ngImport: i0, type: FieldValidation, decorators: [{
            type: Component,
            args: [{ selector: 'mt-field-validation', standalone: true, imports: [Message, ErrorMessagePipe], template: `
    @if (isInvalid(control())) {
      <p-message
        severity="error"
        variant="simple"
        size="small"
        [text]="control().errors | errorMessage"
      >
      </p-message>
    }
  `, host: {
                        '[class.hidden]': '!isInvalid(control())',
                    } }]
        }], propDecorators: { control: [{ type: i0.Input, args: [{ isSignal: true, alias: "control", required: false }] }], touched: [{ type: i0.Input, args: [{ isSignal: true, alias: "touched", required: false }] }] } });

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

export { FieldValidation };
//# sourceMappingURL=masterteam-components-field-validation.mjs.map
