'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import React from 'react';
import { EmailInput } from '../../../ui/components/data-entry/email-input/index.js';
import { withFieldValidation } from '../fragments/with-field-validation/index.js';
import { validateEmailDomain, validateEmailFormat } from './utils.js';
const EmailInputWrapper = React.forwardRef((props, ref) => {
    const { allowedDomains: _1, matchFieldName, ...rest } = props;
    return _jsx(EmailInput, { ref: ref, ...rest, ...(!!matchFieldName && { 'data-exclude': 'true' }) });
});
EmailInputWrapper.displayName = 'EmailInputWrapper';
const EmailField = withFieldValidation(EmailInputWrapper, {
    validations: {
        _validEmail: () => (value) => {
            if (!value)
                return true;
            return validateEmailFormat(value);
        },
        _allowedDomains: ({ allowedDomains = [] }) => (value) => {
            if (!value)
                return true;
            return validateEmailDomain(value, { allowedDomains });
        },
        _matchEmail: ({ 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 `Email must match the "${matchFieldLabel}" field`;
                    }
                }
                return 'Email does not match';
            }
            return true;
        },
    },
});
EmailField.displayName = 'EmailField';
export { EmailField };
//# sourceMappingURL=email-field.js.map