class MarketWebContextService {
    href = '';
    market = '';
    language = '';
    environment = '';
    url = null;
    allowedLanguages = ['sv', 'da', 'fi', 'no', 'en', 'de', 'zh', 'fr', 'es', 'it', 'nl'];
    allowedMarkets = ['se', 'dk', 'fi', 'no', 'com', 'de', 'tpl', 'group'];
    wwwMarkets = ['se', 'dk', 'fi', 'no', 'de', 'com'];
    developmentDomains = ['localhost', '.local', 'local.', '.dev', 'dev.'];
    markets = {
        se: {
            fallbackLanguage: 'sv',
            integration: 'se-inte.postnord.com',
            preproduction: 'se-prep.postnord.com',
            production: 'se-prod.postnord.com',
            live: 'www.postnord.se',
        },
        dk: {
            fallbackLanguage: 'da',
            integration: 'dk-inte.postnord.com',
            preproduction: 'dk-prep.postnord.com',
            production: 'dk-prod.postnord.com',
            live: 'www.postnord.dk',
        },
        fi: {
            fallbackLanguage: 'fi',
            integration: 'fi-inte.postnord.com',
            preproduction: 'fi-prep.postnord.com',
            production: 'fi-prod.postnord.com',
            live: 'www.postnord.fi',
        },
        no: {
            fallbackLanguage: 'no',
            integration: 'no-inte.postnord.com',
            preproduction: 'no-prep.postnord.com',
            production: 'no-prod.postnord.com',
            live: 'www.postnord.no',
        },
        com: {
            fallbackLanguage: 'en',
            integration: 'com-inte.postnord.com',
            preproduction: 'com-prep.postnord.com',
            production: 'com-prod.postnord.com',
            live: 'www.postnord.com',
        },
        de: {
            fallbackLanguage: 'en',
            integration: 'de-inte.postnord.com',
            preproduction: 'de-prep.postnord.com',
            production: 'de-prod.postnord.com',
            live: 'www.postnord.de',
        },
        tpl: {
            fallbackLanguage: 'sv',
            integration: 'tpl-inte.postnord.com',
            preproduction: 'tpl-prep.postnord.com',
            production: 'tpl-prod.postnord.com',
            live: 'www.tpl.postnord.com',
        },
        group: {
            fallbackLanguage: 'en',
            integration: 'group-inte.postnord.com',
            preproduction: 'group-prep.postnord.com',
            production: 'group-prod.postnord.com',
            live: 'group.postnord.com',
        },
    };
    environmentTypes = ['integration', 'preproduction', 'production', 'inte', 'prep', 'prod'];
    environments = {
        production: {
            name: 'production',
            url: 'https://www.postnord.xx/',
        },
        preproduction: {
            name: 'preproduction',
            url: 'https://com-prep.postnord.com/',
        },
        integration: {
            name: 'integration',
            url: 'https://com-inte.postnord.com/',
        },
    };
    constructor(href = window.location.href) {
        this.href = href;
        this.url = new URL(this.href);
    }
    async getLanguage() {
        if (this.language) {
            return this.language;
        }
        await this.resolveLanguage();
        return this.language;
    }
    async getMarket() {
        if (this.market) {
            return this.market;
        }
        await this.resolveMarket();
        return this.market;
    }
    async getEndpoint(environmentName, marketName = '') {
        let url = this.environments.production.url;
        if (this.markets[marketName] && this.markets[marketName][environmentName]) {
            if (environmentName === 'production' && window.location.hostname.indexOf(this.markets[marketName]['live']) === 0) {
                // If the environment is production and we're on the www domain we'll use that
                return `https://` + this.markets[marketName]['live'];
            }
            if (window.location.hostname.indexOf(this.markets[marketName][environmentName]) === 0) {
                return `https://` + this.markets[marketName][environmentName];
            }
        }
        // Old solution to match URLs. Not bulletproof
        if (this.environments[environmentName]) {
            url = this.environments[environmentName].url;
            if (environmentName === this.environments.production.name) {
                if (this.wwwMarkets.indexOf(marketName) !== -1) {
                    // Handles www.postnord.xx domains
                    url = url.replace('.xx', '.' + marketName);
                }
                else {
                    url = url.replace('.xx', '.com'); // Expect URL to end in postnord.com
                    url = url.replace('www.', marketName + '.');
                    if (marketName === 'tpl') {
                        url = url.replace('tpl.', 'www.tpl.');
                    }
                }
            }
        }
        return url;
    }
    async getEnvironmentName() {
        const likelyEnvironment = this.environmentTypes.filter(x => this.url.hostname.indexOf('-' + x) !== -1)[0];
        if (likelyEnvironment) {
            this.environment = likelyEnvironment;
            return this.environment;
        }
        if (this.url.hostname.indexOf('www.') !== -1) {
            this.environment = this.environments.production.name;
            return this.environment;
        }
        this.environment = this.environments.integration.name;
        return this.environment;
    }
    isDevEnvironment() {
        const matchedDevEnvironment = this.developmentDomains.filter(x => this.url.hostname.indexOf(x) !== -1)[0];
        return matchedDevEnvironment.length > 0;
    }
    async resolveMarket() {
        // Resolve market from www domains
        if (this.href.indexOf('//www.') !== -1) {
            const domainRoot = this.url.hostname.substring(this.url.hostname.lastIndexOf('.') + 1, this.url.hostname.length);
            if (domainRoot && this.markets[domainRoot]) {
                this.market = domainRoot;
            }
            else {
                console.warn('Unable to define market from www. domain', this.href);
            }
        }
        // Check if domains are on one of the integration/preproduction/production environments urls
        const likelyEnvironment = this.environmentTypes.filter(x => this.url.hostname.indexOf('-' + x) !== -1);
        if (!this.market && likelyEnvironment && likelyEnvironment.length > 0) {
            const matchedEnvironment = likelyEnvironment[0];
            const marketPrefix = this.url.hostname.substring(0, this.url.hostname.indexOf('-' + matchedEnvironment));
            if (marketPrefix && this.markets[marketPrefix]) {
                this.market = marketPrefix;
            }
            else {
                console.warn('Unable to define market from preset environment names', this.href);
            }
        }
        if (!this.market) {
            if (this.url.hostname.indexOf('www') === -1 && this.url.hostname.indexOf('.postnord.') > 0) {
                const start = this.url.hostname.indexOf('.') !== this.url.hostname.indexOf('.postnord') ? this.url.hostname.indexOf('.') : 0;
                const potentialMarket = this.url.hostname.substring(start, this.url.hostname.indexOf('.postnord.'));
                // Break down the subdomain to the word closest to .postnord. and take that word.
                // Supports sub-sub domain down to 1 level.
                if (this.allowedMarkets.indexOf(potentialMarket) !== -1) {
                    this.market = potentialMarket;
                    return;
                }
            }
        }
        // Check if we're on a development environment
        if (!this.market) {
            const matchedDevEnvironment = this.developmentDomains.filter(x => this.url.hostname.indexOf(x) !== -1)[0];
            if (!matchedDevEnvironment) {
                // Fallback to first allowed market
                this.market = this.allowedMarkets[0];
                return;
            }
            const urlWithoutDomain = this.href.replace(matchedDevEnvironment, '');
            // We see if the dev environment contains something like /dk
            let matchedMarket = this.allowedMarkets.filter(x => {
                const typesOfFallback = ['.' + x, x + '.', '/' + x + '/']; // Ex /dk, dk.html, /dk/
                const matchedTypeOfFallback = typesOfFallback.filter(y => urlWithoutDomain.indexOf(y) !== -1);
                return matchedTypeOfFallback.length > 0;
            })[0];
            if (matchedMarket) {
                this.market = matchedMarket;
            }
            else {
                // We do a similar filter for subpaths but for language this time
                let filteredMarket = Object.entries(this.markets).filter(item => {
                    const typesOfFallback = ['.' + item[1].fallbackLanguage, item[1].fallbackLanguage + '.', '/' + item[1].fallbackLanguage + '/']; // Ex /dk, dk.html, /dk/
                    const matchedTypeOfFallback = typesOfFallback.filter(y => urlWithoutDomain.indexOf(y) !== -1);
                    return matchedTypeOfFallback.length > 0;
                })[0];
                if (filteredMarket && filteredMarket[0]) {
                    this.market = filteredMarket[0];
                }
            }
        }
        if (!this.market) {
            console.info('Was unable to detect market, used fallback');
            this.market = this.allowedMarkets[0];
        }
    }
    async resolveLanguage() {
        if (this.language) {
            return;
        }
        const potentialLanguages = [
            document.querySelector('html[lang]')?.getAttribute('lang'), // Get HTML tag language
            document.querySelector('[http-equiv="content-language"][content]')?.getAttribute('content'), // Obsolute tag but might still be used
            document.querySelector('[property="og:locale"][content]')?.getAttribute('content'), // Get open graph share language
            document.querySelector('[lang]')?.getAttribute('lang'), // Find any tag with a lang attribute
        ];
        potentialLanguages
            .filter(x => x)
            .forEach(language => {
            if (this.allowedLanguages.indexOf(language)) {
                this.language = language;
                return;
            }
        });
        if (!this.language && this.market) {
            this.language = this.markets[this.market].fallbackLanguage;
        }
    }
}
export { MarketWebContextService };
//# sourceMappingURL=MarketWebContextService.js.map
