"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidEmailAddress = exports.validateOrganizationEmailDomains = exports.isValidEmailDomain = exports.isPublicEmailProviderDomain = exports.getEmailDomain = void 0;
const company_email_validator_1 = require("company-email-validator");
const getEmailDomain = (email) => {
    if (/\s/.test(email)) {
        throw new Error(`Invalid email, contains whitespace: ${email}`);
    }
    const domains = email.split('@');
    if (domains.length !== 2 || !domains[1]) {
        throw new Error(`Invalid email: ${email}`);
    }
    return domains[1].toLowerCase();
};
exports.getEmailDomain = getEmailDomain;
/**
 * Public-provider domains that the package list doesn't cover but that still
 * can't be owned by a single customer org on a shared instance.
 */
const ADDITIONAL_PUBLIC_PROVIDER_DOMAINS = [
    'google.com',
    'microsoft.com',
    'onmicrosoft.com',
    'sfr.fr',
];
const ADDITIONAL_PUBLIC_PROVIDER_DOMAIN_SET = new Set(ADDITIONAL_PUBLIC_PROVIDER_DOMAINS);
/**
 * Whether a domain is a public/consumer email provider that no single
 * organization can own. Normalizes case before checking.
 */
const isPublicEmailProviderDomain = (domain) => ADDITIONAL_PUBLIC_PROVIDER_DOMAIN_SET.has(domain.trim().toLowerCase()) ||
    !(0, company_email_validator_1.isCompanyDomain)(domain.trim().toLowerCase());
exports.isPublicEmailProviderDomain = isPublicEmailProviderDomain;
const VALID_EMAIL_DOMAIN_REGEX = /^[a-zA-Z0-9][\w.-]+\.\w{2,4}/g;
const isValidEmailDomain = (value) => value.match(VALID_EMAIL_DOMAIN_REGEX);
exports.isValidEmailDomain = isValidEmailDomain;
const validateOrganizationEmailDomains = (domains) => {
    const invalidDomains = domains.filter((domain) => (0, exports.isPublicEmailProviderDomain)(domain));
    if (!invalidDomains.length) {
        return undefined;
    }
    const readableDomainList = new Intl.ListFormat('en-US', {
        style: 'long',
        type: 'conjunction',
    }).format(invalidDomains);
    return `${readableDomainList} ${invalidDomains.length === 1 ? 'is' : 'are'} not allowed as organization email(s)`;
};
exports.validateOrganizationEmailDomains = validateOrganizationEmailDomains;
const isValidEmailAddress = (email) => /^[\w.+'-]+@[\w-]+\.[A-Za-z]{2,}(?:\.[A-Za-z]{2,})*$/.test(email);
exports.isValidEmailAddress = isValidEmailAddress;
//# sourceMappingURL=email.js.map