'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import React from 'react';
import { PasswordInput } from '../../../ui/components/data-entry/password-input/index.js';
import { withFieldValidation } from '../fragments/with-field-validation/index.js';
import { specialCharacters } from '../../../ui/components/data-entry/password-input/utils.js';
const PasswordInputWrapper = React.forwardRef((props, ref) => {
    const { requireUppercase: _1, requireLowercase: _2, requireNumbers: _3, requireSpecialCharacters: _4, matchFieldName, ...rest } = props;
    return _jsx(PasswordInput, { ref: ref, ...rest, ...(!!matchFieldName && { 'data-exclude': 'true' }) });
});
PasswordInputWrapper.displayName = 'PasswordInputWrapper';
const PasswordField = withFieldValidation(PasswordInputWrapper, {
    validations: {
        _requireUppercase: ({ requireUppercase = true }) => (value) => {
            if (!requireUppercase || value === '' || value === undefined) {
                return true;
            }
            return /[A-Z]/.test(value) || 'This field must contain at least one uppercase letter';
        },
        _requireLowercase: ({ requireLowercase = true }) => (value) => {
            if (!requireLowercase || value === '' || value === undefined) {
                return true;
            }
            return /[a-z]/.test(value) || 'This field must contain at least one lowercase letter';
        },
        _requireNumbers: ({ requireNumbers = true }) => (value) => {
            if (!requireNumbers || value === '' || value === undefined) {
                return true;
            }
            return /[0-9]/.test(value) || 'This field must contain at least one number';
        },
        _requireSpecialCharacters: ({ requireSpecialCharacters = true }) => (value) => {
            if (!requireSpecialCharacters || value === '' || value === undefined) {
                return true;
            }
            for (const char of specialCharacters) {
                if (value.includes(char)) {
                    return true;
                }
            }
            return `This field must contain at least one special character: ${specialCharacters}`;
        },
        _matchPassword: ({ matchFieldName }) => (value, formState) => {
            if (!matchFieldName)
                return true;
            if (value !== formState[matchFieldName]) {
                const matchFieldElements = document.querySelectorAll(`input[name="${matchFieldName}"]`);
                if (matchFieldElements.length === 1) {
                    const matchFieldLabel = matchFieldElements[0].getAttribute('data-label');
                    if (matchFieldLabel) {
                        return `Password must match the "${matchFieldLabel}" field`;
                    }
                }
                return 'Password does not match';
            }
            return true;
        },
    },
});
PasswordField.displayName = 'PasswordField';
export { PasswordField };
//# sourceMappingURL=password-field.js.map