"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFeature = exports.isFeature = exports.checkFeatureFlagVersion = exports.formatToFirebaseFeatureId = void 0;
const snakeCase_1 = __importDefault(require("lodash/snakeCase"));
const semver_1 = __importDefault(require("semver"));
const live_env_1 = require("@ledgerhq/live-env");
const LiveConfig_1 = require("@ledgerhq/live-config/LiveConfig");
const formatToFirebaseFeatureId = (id) => `feature_${(0, snakeCase_1.default)(id)}`;
exports.formatToFirebaseFeatureId = formatToFirebaseFeatureId;
const checkFeatureFlagVersion = (feature) => {
    const platform = LiveConfig_1.LiveConfig.instance.platform;
    if (!feature?.enabled || !platform) {
        return feature;
    }
    const featureSpecificVersion = (() => {
        switch (platform) {
            case "desktop":
                return feature.desktop_version;
            case "ios":
            case "android":
                return feature.mobile_version;
            default:
                return undefined;
        }
    })();
    if (featureSpecificVersion &&
        !semver_1.default.satisfies(LiveConfig_1.LiveConfig.instance.appVersion, featureSpecificVersion, {
            includePrerelease: true,
        })) {
        return {
            enabledOverriddenForCurrentVersion: true,
            ...feature,
            enabled: false,
        };
    }
    return feature;
};
exports.checkFeatureFlagVersion = checkFeatureFlagVersion;
const isFeature = (key) => {
    if (!LiveConfig_1.LiveConfig.instance?.provider?.getValueByKey) {
        return false;
    }
    try {
        const value = LiveConfig_1.LiveConfig.getValueByKey((0, exports.formatToFirebaseFeatureId)(key));
        if (!value) {
            return false;
        }
        return true;
    }
    catch {
        console.error(`Failed to check if feature "${key}" exists`);
        return false;
    }
};
exports.isFeature = isFeature;
const getFeature = args => {
    if (!LiveConfig_1.LiveConfig.instance?.provider?.getValueByKey) {
        return null;
    }
    const { key, appLanguage, localOverrides, allowOverride = true } = args;
    try {
        // Nb prioritize local overrides
        if (allowOverride && localOverrides && localOverrides[key]) {
            const feature = localOverrides[key];
            if (feature) {
                return (0, exports.checkFeatureFlagVersion)(feature);
            }
        }
        const envFlags = (0, live_env_1.getEnv)("FEATURE_FLAGS");
        if (allowOverride && envFlags) {
            const feature = envFlags[key];
            if (feature)
                return {
                    ...feature,
                    overridesRemote: true,
                    overriddenByEnv: true,
                };
        }
        const feature = LiveConfig_1.LiveConfig.getValueByKey((0, exports.formatToFirebaseFeatureId)(key));
        if (feature.enabled &&
            appLanguage &&
            ((feature.languages_whitelisted && !feature.languages_whitelisted.includes(appLanguage)) ||
                (feature.languages_blacklisted && feature.languages_blacklisted.includes(appLanguage)))) {
            return {
                enabledOverriddenForCurrentLanguage: true,
                ...feature,
                enabled: false,
            };
        }
        return (0, exports.checkFeatureFlagVersion)(feature);
    }
    catch {
        // console.error(`Failed to retrieve feature "${key}"`);
        return null;
    }
};
exports.getFeature = getFeature;
//# sourceMappingURL=firebaseFeatureFlags.js.map