import { Brand } from '../../types/brand.js';

var Link = /** @class */ (function () {
    function Link(_a) {
        var config = _a.config, brand = _a.brand, userType = _a.userType, environment = _a.environment, useAbsoluteUrls = _a.useAbsoluteUrls, project = _a.project, linkProtocol = _a.linkProtocol, domains = _a.domains, forceMotoscoutLink = _a.forceMotoscoutLink, forceAutoscoutLink = _a.forceAutoscoutLink, _b = _a.userEntitlements, userEntitlements = _b === void 0 ? [] : _b, rightIcon = _a.rightIcon;
        var _c, _d;
        this.target = config.target;
        this.onClick = config.onClick;
        this.projectIdentifier = config.projectIdentifier;
        var hasEntitlement = config.entitlementConfig
            ? config.entitlementConfig.singleRequiredEntitlement.some(function (entitlement) {
                return userEntitlements.includes(entitlement);
            })
            : true;
        this.isVisible = Link.determineVisibility({
            userEntitlements: userEntitlements,
            entitlementConfig: config.entitlementConfig,
            hasEntitlement: hasEntitlement,
            visibilitySettings: config.visibilitySettings,
            brand: brand,
            userType: userType,
        });
        var link = this.resolveLink({ hasEntitlement: hasEntitlement, config: config });
        this.link = this.prefixDomain({
            link: link,
            brand: brand,
            environment: environment,
            useAbsoluteUrls: useAbsoluteUrls,
            project: project,
            linkProtocol: linkProtocol,
            domains: domains,
            forceMotoscoutLink: forceMotoscoutLink,
            forceAutoscoutLink: forceAutoscoutLink,
            userType: userType,
        });
        this.rightIcon = Link.shouldDisplayMissingEntitlementIcon(hasEntitlement, config.entitlementConfig)
            ? (_c = config.entitlementConfig) === null || _c === void 0 ? void 0 : _c.missingEntitlementLinkIcon
            : rightIcon;
        this.title = config.title;
        this.translationKey = Link.shouldDisplayMissingEntitlementTranslation(hasEntitlement, config.entitlementConfig)
            ? (_d = config.entitlementConfig) === null || _d === void 0 ? void 0 : _d.missingEntitlementTranslationKey
            : config.translationKey;
        this.translationParameters = config.translationParameters;
    }
    Link.shouldDisplayMissingEntitlementTranslation = function (hasEntitlement, entitlementConfig) {
        return (!hasEntitlement && !!(entitlementConfig === null || entitlementConfig === void 0 ? void 0 : entitlementConfig.missingEntitlementTranslationKey));
    };
    Link.shouldDisplayMissingEntitlementIcon = function (hasEntitlement, entitlementConfig) {
        return !hasEntitlement && !!(entitlementConfig === null || entitlementConfig === void 0 ? void 0 : entitlementConfig.missingEntitlementLinkIcon);
    };
    Link.prototype.resolveLink = function (_a) {
        var hasEntitlement = _a.hasEntitlement, _b = _a.config, link = _b.link, entitlementConfig = _b.entitlementConfig;
        return !hasEntitlement && (entitlementConfig === null || entitlementConfig === void 0 ? void 0 : entitlementConfig.missingEntitlementFallbackLink)
            ? entitlementConfig.missingEntitlementFallbackLink
            : link;
    };
    Link.prototype.prefixDomain = function (_a) {
        var link = _a.link, brand = _a.brand, environment = _a.environment, useAbsoluteUrls = _a.useAbsoluteUrls, project = _a.project, linkProtocol = _a.linkProtocol, _b = _a.forceMotoscoutLink, forceMotoscoutLink = _b === void 0 ? false : _b, _c = _a.forceAutoscoutLink, forceAutoscoutLink = _c === void 0 ? false : _c, domains = _a.domains;
        var forceBrandDomain = function () {
            if (forceAutoscoutLink) {
                return Brand.AutoScout24;
            }
            else if (forceMotoscoutLink) {
                return Brand.MotoScout24;
            }
            else {
                return brand;
            }
        };
        var forceBrand = forceBrandDomain();
        var domain = domains[forceBrand][environment];
        var baseUrl = "".concat(linkProtocol, "://").concat(domain);
        var isAlreadyAbsolute = (link === null || link === void 0 ? void 0 : link.de.substring(0, 4)) === 'http';
        if (link && (forceAutoscoutLink || forceMotoscoutLink)) {
            return {
                de: "".concat(baseUrl).concat(link.de),
                fr: "".concat(baseUrl).concat(link.fr),
                it: "".concat(baseUrl).concat(link.it),
                en: "".concat(baseUrl).concat(link.en),
            };
        }
        var isLinkTargetInSameProject = project && this.projectIdentifier && project === this.projectIdentifier;
        if (!useAbsoluteUrls ||
            !link ||
            isAlreadyAbsolute ||
            isLinkTargetInSameProject) {
            return link;
        }
        return {
            de: "".concat(baseUrl).concat(link.de),
            fr: "".concat(baseUrl).concat(link.fr),
            it: "".concat(baseUrl).concat(link.it),
            en: "".concat(baseUrl).concat(link.en),
        };
    };
    Link.determineVisibility = function (_a) {
        var _b;
        var hasEntitlement = _a.hasEntitlement, visibilitySettings = _a.visibilitySettings, brand = _a.brand, userType = _a.userType, userEntitlements = _a.userEntitlements, entitlementConfig = _a.entitlementConfig;
        if (!visibilitySettings.brand[brand]) {
            return false;
        }
        var hasRestrictedEntitlement = Link.hasRestrictedEntitlement({
            entitlementConfig: entitlementConfig,
            userEntitlements: userEntitlements,
        });
        if (hasRestrictedEntitlement) {
            return false;
        }
        var hideIfRequiredEntitlementIsMissing = Link.hideIfRequiredEntitlementIsMissing({
            hasEntitlement: hasEntitlement,
            entitlementConfig: entitlementConfig,
        });
        if (hideIfRequiredEntitlementIsMissing) {
            return false;
        }
        // This scenario is essential when dealing with guest users. The guest user
        // type was introduced at a later stage. Rather than modifying visibility
        // settings for the guest user type across all link nodes, our goal is to
        // selectively hide a single link in the navigation for guest users.
        if (userType && ((_b = visibilitySettings === null || visibilitySettings === void 0 ? void 0 : visibilitySettings.userType) === null || _b === void 0 ? void 0 : _b[userType]) !== undefined) {
            return !!visibilitySettings.userType[userType];
        }
        return true;
    };
    Link.hasRestrictedEntitlement = function (_a) {
        var entitlementConfig = _a.entitlementConfig, userEntitlements = _a.userEntitlements;
        return entitlementConfig && entitlementConfig.hideIfEntitlementIsPresent
            ? userEntitlements.includes(entitlementConfig.hideIfEntitlementIsPresent)
            : false;
    };
    Link.hideIfRequiredEntitlementIsMissing = function (_a) {
        var hasEntitlement = _a.hasEntitlement, entitlementConfig = _a.entitlementConfig;
        return (!hasEntitlement && (entitlementConfig === null || entitlementConfig === void 0 ? void 0 : entitlementConfig.hideIfRequiredEntitlementIsMissing));
    };
    return Link;
}());

export { Link };
//# sourceMappingURL=link.js.map
