import { domainLocales } from './constantsDomainLocales.js';
import { isProduction } from './constantsIsProduction.js';

const isNumeric = (n) => !isNaN(parseFloat(n)) && isFinite(n);
const defaultDomain = 'hellosehat.com';
class GAssets {
    constructor() {
        this.baseDomain = '';
        this.subPath = '';
        this.getAssetPath = (path, forceBaseDomain, forceSubPath) => {
            const fileUrl = 'https://hhg-common.' +
                [
                    forceBaseDomain || this.getBaseDomain(),
                    forceSubPath || this.subPath,
                    path,
                ].join('/');
            if (!isProduction && !process.env.JEST_WORKER_ID) {
                // don't add timestamp when running jest tests
                // add timestamp to avoid caching if not production
                return fileUrl + '?v=' + Math.floor(new Date().getTime() / 1000 / 60 / 3);
            }
            return fileUrl;
        };
    }
    // constructor() {}
    getBaseDomain() {
        if (this.baseDomain) {
            return this.baseDomain;
        }
        if (typeof window !== 'undefined') {
            const { host } = window.location;
            const hostParts = host.split('.');
            const includeDomain = domainLocales[Object.keys(domainLocales).find((k) => host.includes(domainLocales[k]))];
            if (hostParts.includes('localhost') ||
                hostParts.includes('hellohealthgroup') ||
                hostParts.every((part) => isNumeric(part)) || // is IP address
                !includeDomain) {
                return defaultDomain;
            }
            return includeDomain;
        }
        return defaultDomain;
    }
}
const CommonGAssets = new GAssets();
CommonGAssets.subPath = 'common';
if (process.env.NEXT_PUBLIC_DEPLOY_LOCALE) {
    CommonGAssets.baseDomain =
        domainLocales[process.env.NEXT_PUBLIC_DEPLOY_LOCALE];
}

export { CommonGAssets as C, GAssets as G };
