import { NgClass } from '@angular/common';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, forwardRef, Inject, Input, Optional, ViewEncapsulation } from '@angular/core';
import { KBQ_FORM_FIELD_REF, KbqComponentColors } from '@koobiq/components/core';
import { KbqIconModule } from '@koobiq/components/icon';
import { KbqFormField } from './form-field';
import { KbqHint } from './hint';
import * as i0 from "@angular/core";
import * as i1 from "@koobiq/components/icon";
let nextPasswordHintUniqueId = 0;
export var PasswordRules;
(function (PasswordRules) {
    PasswordRules[PasswordRules["Length"] = 0] = "Length";
    PasswordRules[PasswordRules["UpperLatin"] = 1] = "UpperLatin";
    PasswordRules[PasswordRules["LowerLatin"] = 2] = "LowerLatin";
    PasswordRules[PasswordRules["Digit"] = 3] = "Digit";
    PasswordRules[PasswordRules["LatinAndSpecialSymbols"] = 4] = "LatinAndSpecialSymbols";
    PasswordRules[PasswordRules["Custom"] = 5] = "Custom";
})(PasswordRules || (PasswordRules = {}));
export const regExpPasswordValidator = {
    [PasswordRules.LowerLatin]: RegExp(/^(?=.*?[a-z])/),
    [PasswordRules.UpperLatin]: RegExp(/^(?=.*?[A-Z])/),
    [PasswordRules.Digit]: RegExp(/^(?=.*?[0-9])/),
    [PasswordRules.LatinAndSpecialSymbols]: RegExp(/[^ !`"'#№$%&()*+,-./\\:;<=>?@[\]^_{|}~A-Za-z0-9]/)
};
export const hasPasswordStrengthError = (passwordHints) => {
    return passwordHints.some((hint) => hint.hasError);
};
export class KbqPasswordHint extends KbqHint {
    get icon() {
        return this.checked ? 'kbq-check-s_16' : 'kbq-xmark-s_16';
    }
    /**
     * The form field hint icon color.
     *
     * @docs-private
     */
    get iconColor() {
        if (this.control?.ngControl.untouched && this.control?.ngControl.pristine) {
            return KbqComponentColors.ContrastFade;
        }
        return this.checked ? KbqComponentColors.Success : KbqComponentColors.Error;
    }
    get control() {
        return this.formField.control;
    }
    constructor(changeDetectorRef, formField) {
        super();
        this.changeDetectorRef = changeDetectorRef;
        this.formField = formField;
        this.id = `kbq-hint-${nextPasswordHintUniqueId++}`;
        this.fillTextOff = true;
        this.hasError = false;
        this.checked = false;
        this.checkValue = () => {
            if (this.control.focused && this.isValueChanged()) {
                this.hasError = false;
                this.checked = this.checkRule(this.control.value);
            }
            else if (!this.control.focused && !this.isValueChanged()) {
                this.hasError = !this.checkRule(this.control.value);
            }
            if (!this.control.required && !this.control.value) {
                this.checked = this.hasError = false;
            }
            this.lastControlValue = this.control.value;
            this.changeDetectorRef.markForCheck();
        };
        this.checkRegexRule = (value) => {
            return !!this.regex?.test(value);
        };
        this.color = KbqComponentColors.ContrastFade;
        this.setDefaultColor(KbqComponentColors.ContrastFade);
    }
    ngAfterContentInit() {
        this.formField = this.formField || this.viewFormField;
        if (this.rule === PasswordRules.Custom && this.regex === undefined && this.customCheckRule === undefined) {
            throw Error('You should set [regex] or [checkRule] for PasswordRules.Custom');
        }
        if (this.rule === PasswordRules.Length && (this.min || this.max) === null) {
            throw Error('For [rule] "Length" need set [min] and [max]');
        }
        if (this.rule === PasswordRules.Length) {
            this.checkRule = this.checkLengthRule;
        }
        else if ([PasswordRules.UpperLatin, PasswordRules.LowerLatin, PasswordRules.Digit].includes(this.rule)) {
            this.regex = regExpPasswordValidator[this.rule];
            this.checkRule = this.checkRegexRule;
        }
        else if (this.rule === PasswordRules.LatinAndSpecialSymbols) {
            this.regex = regExpPasswordValidator[this.rule];
            this.checkRule = this.checkSpecialSymbolsRegexRule;
        }
        else if (this.rule === PasswordRules.Custom) {
            this.checkRule = this.regex === undefined ? this.customCheckRule : this.checkRegexRule;
        }
        else {
            throw Error(`Unknown [rule]=${this.rule}`);
        }
        // prevent error when formField.control is undefined
        setTimeout(() => {
            this.formField.control.stateChanges.subscribe(this.checkValue);
            this.formField.control.checkRule.subscribe(() => {
                this.checked = this.checkRule(this.control.value);
                this.hasError = !this.checkRule(this.control.value);
            });
        });
    }
    checkLengthRule(value) {
        return value.length >= this.min && value.length <= this.max;
    }
    checkSpecialSymbolsRegexRule(value) {
        return !!value && !this.regex?.test(value);
    }
    isValueChanged() {
        return this.lastControlValue !== this.formField.control.value;
    }
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqPasswordHint, deps: [{ token: i0.ChangeDetectorRef }, { token: forwardRef(() => KBQ_FORM_FIELD_REF), optional: true }], target: i0.ɵɵFactoryTarget.Component }); }
    /** @nocollapse */ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.14", type: KbqPasswordHint, isStandalone: true, selector: "kbq-password-hint", inputs: { id: "id", rule: "rule", min: "min", max: "max", regex: "regex", customCheckRule: ["checkRule", "customCheckRule"], viewFormField: "viewFormField", fillTextOff: "fillTextOff" }, host: { properties: { "attr.id": "id", "class.kbq-success": "checked", "class.kbq-error": "hasError", "class.kbq-hint_fill-text-off": "fillTextOff", "class.kbq-hint_compact": "compact" }, classAttribute: "kbq-hint kbq-password-hint" }, usesInheritance: true, ngImport: i0, template: `
        <i class="kbq-password-hint__icon" kbq-icon="" [ngClass]="icon" [color]="iconColor"></i>

        <span class="kbq-hint__text">
            <ng-content />
        </span>
    `, isInline: true, styles: [".kbq-hint{display:flex}.kbq-hint .kbq-icon{margin-top:var(--kbq-hint-size-normal-margin-top);margin-right:var(--kbq-hint-size-normal-content-padding)}.kbq-hint.kbq-hint_compact .kbq-icon{margin-top:var(--kbq-hint-size-compact-margin-top);margin-right:var(--kbq-hint-size-compact-content-padding)}.kbq-hint.kbq-contrast-fade{color:var(--kbq-hint-fill-text-on-fade-contrast-text)}.kbq-hint.kbq-contrast-fade .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-fade-contrast-icon)!important}.kbq-hint.kbq-success{color:var(--kbq-hint-fill-text-on-success-text)}.kbq-hint.kbq-success .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-success-icon)!important}.kbq-hint.kbq-warning{color:var(--kbq-hint-fill-text-on-warning-text)}.kbq-hint.kbq-warning .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-warning-icon)!important}.kbq-hint.kbq-error{color:var(--kbq-hint-fill-text-on-error-text)}.kbq-hint.kbq-error .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-error-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-contrast-fade{color:var(--kbq-hint-fill-text-off-fade-contrast-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-contrast-fade .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-fade-contrast-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-success{color:var(--kbq-hint-fill-text-off-success-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-success .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-success-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-warning{color:var(--kbq-hint-fill-text-off-warning-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-warning .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-warning-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-error{color:var(--kbq-hint-fill-text-off-error-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-error .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-error-icon)!important}.kbq-hint .kbq-hint__text{font-size:var(--kbq-typography-text-normal-font-size);font-style:var(--kbq-typography-text-normal-font-style);font-weight:var(--kbq-typography-text-normal-font-weight);line-height:var(--kbq-typography-text-normal-line-height);font-family:var(--kbq-typography-text-normal-font-family);text-transform:var(--kbq-typography-text-normal-text-transform);font-feature-settings:var(--kbq-typography-text-normal-font-feature-settings);letter-spacing:var(--kbq-typography-text-normal-letter-spacing)}.kbq-hint.kbq-hint_compact .kbq-hint__text{font-size:var(--kbq-typography-text-compact-font-size);font-style:var(--kbq-typography-text-compact-font-style);font-weight:var(--kbq-typography-text-compact-font-weight);line-height:var(--kbq-typography-text-compact-line-height);font-family:var(--kbq-typography-text-compact-font-family);text-transform:var(--kbq-typography-text-compact-text-transform);font-feature-settings:var(--kbq-typography-text-compact-font-feature-settings);letter-spacing:var(--kbq-typography-text-compact-letter-spacing)}\n", ".kbq-hint{--kbq-hint-size-normal-margin-top: var(--kbq-size-3xs);--kbq-hint-size-normal-content-padding: var(--kbq-size-xxs);--kbq-hint-size-compact-margin-top: 0;--kbq-hint-size-compact-content-padding: var(--kbq-size-xxs);--kbq-hint-fill-text-off-fade-contrast-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-fade-contrast-icon: var(--kbq-icon-contrast-fade);--kbq-hint-fill-text-off-success-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-success-icon: var(--kbq-icon-success);--kbq-hint-fill-text-off-warning-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-warning-icon: var(--kbq-icon-warning);--kbq-hint-fill-text-off-error-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-error-icon: var(--kbq-icon-error);--kbq-hint-fill-text-on-fade-contrast-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-on-fade-contrast-icon: var(--kbq-icon-contrast-fade);--kbq-hint-fill-text-on-success-text: var(--kbq-foreground-success);--kbq-hint-fill-text-on-success-icon: var(--kbq-icon-success);--kbq-hint-fill-text-on-warning-text: var(--kbq-foreground-warning);--kbq-hint-fill-text-on-warning-icon: var(--kbq-icon-warning);--kbq-hint-fill-text-on-error-text: var(--kbq-foreground-error);--kbq-hint-fill-text-on-error-icon: var(--kbq-icon-error)}\n"], dependencies: [{ kind: "directive", type: NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "ngmodule", type: KbqIconModule }, { kind: "component", type: i1.KbqIcon, selector: "[kbq-icon]", inputs: ["small", "autoColor", "kbq-icon"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqPasswordHint, decorators: [{
            type: Component,
            args: [{ standalone: true, imports: [NgClass, KbqIconModule], selector: 'kbq-password-hint', template: `
        <i class="kbq-password-hint__icon" kbq-icon="" [ngClass]="icon" [color]="iconColor"></i>

        <span class="kbq-hint__text">
            <ng-content />
        </span>
    `, host: {
                        '[attr.id]': 'id',
                        class: 'kbq-hint kbq-password-hint',
                        '[class.kbq-success]': 'checked',
                        '[class.kbq-error]': 'hasError',
                        '[class.kbq-hint_fill-text-off]': 'fillTextOff',
                        '[class.kbq-hint_compact]': 'compact'
                    }, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, styles: [".kbq-hint{display:flex}.kbq-hint .kbq-icon{margin-top:var(--kbq-hint-size-normal-margin-top);margin-right:var(--kbq-hint-size-normal-content-padding)}.kbq-hint.kbq-hint_compact .kbq-icon{margin-top:var(--kbq-hint-size-compact-margin-top);margin-right:var(--kbq-hint-size-compact-content-padding)}.kbq-hint.kbq-contrast-fade{color:var(--kbq-hint-fill-text-on-fade-contrast-text)}.kbq-hint.kbq-contrast-fade .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-fade-contrast-icon)!important}.kbq-hint.kbq-success{color:var(--kbq-hint-fill-text-on-success-text)}.kbq-hint.kbq-success .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-success-icon)!important}.kbq-hint.kbq-warning{color:var(--kbq-hint-fill-text-on-warning-text)}.kbq-hint.kbq-warning .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-warning-icon)!important}.kbq-hint.kbq-error{color:var(--kbq-hint-fill-text-on-error-text)}.kbq-hint.kbq-error .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-on-error-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-contrast-fade{color:var(--kbq-hint-fill-text-off-fade-contrast-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-contrast-fade .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-fade-contrast-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-success{color:var(--kbq-hint-fill-text-off-success-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-success .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-success-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-warning{color:var(--kbq-hint-fill-text-off-warning-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-warning .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-warning-icon)!important}.kbq-hint.kbq-hint_fill-text-off.kbq-error{color:var(--kbq-hint-fill-text-off-error-text)}.kbq-hint.kbq-hint_fill-text-off.kbq-error .kbq-icon.kbq-empty{color:var(--kbq-hint-fill-text-off-error-icon)!important}.kbq-hint .kbq-hint__text{font-size:var(--kbq-typography-text-normal-font-size);font-style:var(--kbq-typography-text-normal-font-style);font-weight:var(--kbq-typography-text-normal-font-weight);line-height:var(--kbq-typography-text-normal-line-height);font-family:var(--kbq-typography-text-normal-font-family);text-transform:var(--kbq-typography-text-normal-text-transform);font-feature-settings:var(--kbq-typography-text-normal-font-feature-settings);letter-spacing:var(--kbq-typography-text-normal-letter-spacing)}.kbq-hint.kbq-hint_compact .kbq-hint__text{font-size:var(--kbq-typography-text-compact-font-size);font-style:var(--kbq-typography-text-compact-font-style);font-weight:var(--kbq-typography-text-compact-font-weight);line-height:var(--kbq-typography-text-compact-line-height);font-family:var(--kbq-typography-text-compact-font-family);text-transform:var(--kbq-typography-text-compact-text-transform);font-feature-settings:var(--kbq-typography-text-compact-font-feature-settings);letter-spacing:var(--kbq-typography-text-compact-letter-spacing)}\n", ".kbq-hint{--kbq-hint-size-normal-margin-top: var(--kbq-size-3xs);--kbq-hint-size-normal-content-padding: var(--kbq-size-xxs);--kbq-hint-size-compact-margin-top: 0;--kbq-hint-size-compact-content-padding: var(--kbq-size-xxs);--kbq-hint-fill-text-off-fade-contrast-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-fade-contrast-icon: var(--kbq-icon-contrast-fade);--kbq-hint-fill-text-off-success-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-success-icon: var(--kbq-icon-success);--kbq-hint-fill-text-off-warning-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-warning-icon: var(--kbq-icon-warning);--kbq-hint-fill-text-off-error-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-off-error-icon: var(--kbq-icon-error);--kbq-hint-fill-text-on-fade-contrast-text: var(--kbq-foreground-contrast);--kbq-hint-fill-text-on-fade-contrast-icon: var(--kbq-icon-contrast-fade);--kbq-hint-fill-text-on-success-text: var(--kbq-foreground-success);--kbq-hint-fill-text-on-success-icon: var(--kbq-icon-success);--kbq-hint-fill-text-on-warning-text: var(--kbq-foreground-warning);--kbq-hint-fill-text-on-warning-icon: var(--kbq-icon-warning);--kbq-hint-fill-text-on-error-text: var(--kbq-foreground-error);--kbq-hint-fill-text-on-error-icon: var(--kbq-icon-error)}\n"] }]
        }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [forwardRef(() => KBQ_FORM_FIELD_REF)]
                }] }], propDecorators: { id: [{
                type: Input
            }], rule: [{
                type: Input
            }], min: [{
                type: Input
            }], max: [{
                type: Input
            }], regex: [{
                type: Input
            }], customCheckRule: [{
                type: Input,
                args: ['checkRule']
            }], viewFormField: [{
                type: Input
            }], fillTextOff: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 