"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const parse_domain_1 = require("parse-domain");
function getDomain(url) {
    const parsedDomain = (0, parse_domain_1.parseDomain)((0, parse_domain_1.fromUrl)(url));
    if (!parsedDomain.topLevelDomains) {
        return null;
    }
    return `${parsedDomain.domain}.${parsedDomain.topLevelDomains.join('.')}`;
}
// TODO:  Check if this can be summarized as a single regex
const SEARCH_TESTS = [
    // non-SSL google search
    /(google|bing|yahoo|baidu)\.\w{2,3}(\.\w{2,3})?\/(search|url|s)/i,
    // SSL search sets a referrer with no path (except for baidu)
    /https:\/\/(www|search)?\.?(google|yahoo|bing|duckduckgo)\..*?\/?$/i,
];
function getCurrentSourceType(inputs) {
    if (inputs.queryParams?.get('utm_source') ||
        inputs.queryParams?.get('utm_campaign') ||
        inputs.queryParams?.get('gclid') ||
        inputs.queryParams?.get('otm_source')) {
        return 'campaign';
    }
    const referrer = inputs.referrer;
    if (referrer) {
        for (var i = 0; i < SEARCH_TESTS.length; i++) {
            var searchRegExp = SEARCH_TESTS[i];
            var match = referrer.match(searchRegExp);
            if (match) {
                return 'search';
            }
        }
    }
    if (referrer && getDomain(referrer) !== getDomain(inputs.url)) {
        return 'referral';
    }
    return 'direct';
}
exports.default = getCurrentSourceType;
