"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const fse = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const Errors = __importStar(require("@oclif/errors"));
const config_1 = __importDefault(require("../../utils/config"));
const env_1 = __importDefault(require("../../utils/env"));
const update_1 = require("../../utils/update");
async function getUpdateData(version) {
    const updatePath = path.join(config_1.default.CacheRoot, config_1.default.updateFile);
    try {
        if (fse.existsSync(updatePath)) {
            const updateData = (await fse.readJSON(updatePath));
            return updateData.data;
        }
    }
    catch (_a) {
    }
    try {
        const responseData = await (0, update_1.fetchUpdateData)(version);
        if (responseData) {
            const { cli } = responseData;
            if (!(cli.needUpdate && cli.forceUpdate)) {
                await fse.ensureDir(config_1.default.CacheRoot);
                await fse.outputJson(updatePath, {
                    data: responseData,
                    time: Date.now(),
                    version,
                });
            }
            return responseData;
        }
        else {
            throw Error('fetch config failed');
        }
    }
    catch (e) {
    }
    return null;
}
const hook = async function (options) {
    const { version } = options.config;
    const argv = options.argv.join(' ');
    if (env_1.default.private) {
    }
    if (options.id === 'clear' && (argv === 'update' || argv === 'all')) {
        return;
    }
    const updateData = await getUpdateData(version);
    if (!updateData) {
        return;
    }
    const { cli } = updateData;
    if (cli === null || cli === void 0 ? void 0 : cli.needUpdate) {
        const updateAvailableText = `Update version available from ${chalk_1.default.yellowBright(version)} to ${chalk_1.default.greenBright(cli.latestVersion)}\n`;
        if (cli.forceUpdate) {
            const defaultReason = updateAvailableText +
                `You must update because old version is deprecated\nPlease run ${chalk_1.default.greenBright(cli.installCmd)} to complete the upgrade\n`;
            const reason = cli.updateReason || defaultReason;
            Errors.error(reason);
        }
        else {
            const defaultReason = updateAvailableText +
                `You can run ${chalk_1.default.greenBright(cli.installCmd)} to complete the upgrade\n`;
            const reason = cli.updateReason || defaultReason;
            Errors.warn(reason);
        }
    }
};
exports.default = hook;
