"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMinVersion = exports.mustUpgrade = exports.shouldUpgrade = void 0;
const semver_1 = __importDefault(require("semver"));
const shouldUseTrustedInputForSegwit_1 = require("@ledgerhq/hw-app-btc/shouldUseTrustedInputForSegwit");
const polyfill_1 = require("./polyfill");
const live_env_1 = require("@ledgerhq/live-env");
const LiveConfig_1 = require("@ledgerhq/live-config/LiveConfig");
function shouldUpgrade(appName, appVersion) {
    if ((0, live_env_1.getEnv)("DISABLE_APP_VERSION_REQUIREMENTS"))
        return false;
    const deps = (0, polyfill_1.getDependencies)(appName);
    if ((deps.includes("Bitcoin") &&
        (0, shouldUseTrustedInputForSegwit_1.shouldUseTrustedInputForSegwit)({
            name: appName,
            version: "1.4.0",
        })) ||
        appName === "Bitcoin") {
        // https://donjon.ledger.com/lsb/010/
        // the `-0` is here to allow for pre-release tags of the 1.4.0
        return !semver_1.default.satisfies(appVersion || "", ">= 1.4.0-0", {
            includePrerelease: true, // this will allow pre-release tags for higher versions (> 1.4.0)
        });
    }
    return false;
}
exports.shouldUpgrade = shouldUpgrade;
function mustUpgrade(appName, appVersion) {
    if ((0, live_env_1.getEnv)("DISABLE_APP_VERSION_REQUIREMENTS"))
        return false;
    // we should convert the app name to camel case and replace spaces with underscores to match the config convention in firebase
    const minVersion = LiveConfig_1.LiveConfig.getValueByKey(`config_nanoapp_${appName.toLowerCase().replace(/ /g, "_")}`)?.minVersion;
    if (minVersion) {
        // necessary when using test versions on other providers that often end up on -dev
        const appVersionCoerced = semver_1.default.coerce(appVersion);
        return !semver_1.default.gte(appVersionCoerced || "", minVersion, {
            includePrerelease: true, // this will allow pre-release tags for higher versions than the minimum one
        });
    }
    return false;
}
exports.mustUpgrade = mustUpgrade;
function getMinVersion(appName, model) {
    if ((0, live_env_1.getEnv)("DISABLE_APP_VERSION_REQUIREMENTS")) {
        return undefined;
    }
    // we should convert the app name to camel case and replace spaces with underscores to match the config convention in firebase
    const config = LiveConfig_1.LiveConfig.getValueByKey(`config_nanoapp_${appName.toLowerCase().replace(/ /g, "_")}`);
    const minVersion = config?.[`${model?.toLowerCase()}MinVersion`] ?? config?.minVersion;
    return minVersion ? semver_1.default.coerce(minVersion)?.version : undefined;
}
exports.getMinVersion = getMinVersion;
//# sourceMappingURL=support.js.map