"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getName = exports.isOlderThen = exports.getDomain = exports.isBlacklisted = exports.getDevUserId = exports.getDevOrgId = exports.getBaseUrl = exports.parseJwt = void 0;
const parseJwt = (token) => {
    return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
};
exports.parseJwt = parseJwt;
const getBaseUrl = (settings) => {
    try {
        const url = (0, exports.parseJwt)(settings.apiKey)
            .iss.replace(/auth-token/g, 'api')
            .slice(0, -1);
        return url;
    }
    catch (e) {
        return 'https://api.devrev.ai';
    }
};
exports.getBaseUrl = getBaseUrl;
const getDevOrgId = (settings) => {
    return (0, exports.parseJwt)(settings.apiKey)['http://devrev.ai/devo_don'];
};
exports.getDevOrgId = getDevOrgId;
const getDevUserId = (settings) => {
    return (0, exports.parseJwt)(settings.apiKey).sub;
};
exports.getDevUserId = getDevUserId;
const isBlacklisted = (settings, domain) => {
    const blacklistedDomains = settings.blacklistedDomains?.split(',');
    if (!blacklistedDomains)
        return false;
    return blacklistedDomains.includes(domain);
};
exports.isBlacklisted = isBlacklisted;
const getDomain = (settings, email) => {
    const domain = email.split('@')[1];
    if ((0, exports.isBlacklisted)(settings, domain))
        return email;
    return domain;
};
exports.getDomain = getDomain;
const isOlderThen = (firstDate, secondDate) => {
    const first = new Date(firstDate);
    const second = new Date(secondDate);
    return first < second;
};
exports.isOlderThen = isOlderThen;
const getName = (payload) => {
    const { firstName, lastName, fullName } = payload;
    if (fullName)
        return fullName;
    if (firstName && lastName)
        return `${firstName} ${lastName}`;
    if (firstName)
        return firstName;
    if (lastName)
        return lastName;
    return '';
};
exports.getName = getName;
//# sourceMappingURL=helpers.js.map