"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.version = version;
exports.installGopls = installGopls;
exports.checkGopls = checkGopls;
exports.installGomodifytags = installGomodifytags;
exports.installGotests = installGotests;
exports.installGoplay = installGoplay;
exports.installImpl = installImpl;
exports.installTools = installTools;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const coc_nvim_1 = require("coc.nvim");
const tools_1 = require("./utils/tools");
const checktag_1 = tslib_1.__importDefault(require("./utils/checktag"));
const ui_1 = require("./utils/ui");
const config_1 = require("./utils/config");
const binaries_1 = require("./binaries");
const versions_1 = require("./utils/versions");
async function version() {
    const v1 = await pkgVersion();
    const v2 = await goplsVersion() || 'unknown';
    coc_nvim_1.window.showMessage(`Version: coc-go ${v1}; gopls ${v2}`, 'more');
}
async function installGopls(client) {
    await (0, tools_1.installGoBin)(binaries_1.GOPLS, true, goplsVersion);
    if (client.needsStop()) {
        await client.stop();
        client.restart();
    }
}
const checkInterval = 24 * 60 * 60 * 1000;
async function shouldCheckGopls() {
    const now = new Date().getTime();
    const last = await (0, config_1.getState)('gopls:last-check');
    if (last - (now - checkInterval) < 0) {
        await (0, config_1.setState)('gopls:last-check', new Date().getTime());
        return true;
    }
    return false;
}
async function checkGopls(client, mode) {
    try {
        if (!(await shouldCheckGopls())) {
            return;
        }
        let install = false;
        await (0, ui_1.withProgress)('Checking for new gopls version', async () => {
            const [current, latest] = await Promise.all([
                goplsVersion(),
                (0, checktag_1.default)("golang/tools", /^gopls\//),
            ]);
            if (!(0, versions_1.isValidVersion)(current) || !(0, versions_1.isValidVersion)(latest)) {
                coc_nvim_1.window.showMessage('checking for a new gopls version failed', 'warning');
                return;
            }
            switch ((0, versions_1.compareVersions)(latest, current)) {
                case 0:
                    coc_nvim_1.window.showMessage(`[gopls] up-to-date: ${current}`, 'more');
                    break;
                case 1:
                    switch (mode) {
                        case 'install':
                            install = true;
                            break;
                        case 'ask':
                            install = await coc_nvim_1.window.showPrompt(`[gopls] Install update? ${current} => ${latest}`);
                            break;
                        case 'inform':
                            coc_nvim_1.window.showMessage(`[gopls] update available: ${current} => ${latest}`);
                            break;
                    }
                    break;
                case -1:
                    coc_nvim_1.window.showMessage(`[gopls] current: ${current} | latest: ${latest}`, 'more');
                    break;
            }
        });
        if (install) {
            await installGopls(client);
        }
    }
    catch (e) {
        coc_nvim_1.window.showMessage(e.toString(), 'error');
    }
}
async function pkgVersion() {
    try {
        const pkgPath = path_1.default.resolve(__dirname, '..', 'package.json');
        const pkgContent = await fs_1.default.promises.readFile(pkgPath, 'utf8');
        return JSON.parse(pkgContent).version;
    }
    catch (err) {
        console.error(err);
    }
    return '';
}
async function goplsVersion() {
    const [, versionOut] = await (0, tools_1.runGoTool)("gopls", ["version"]);
    const m = versionOut.trim().match(/\s{4}golang\.org\/x\/tools\/gopls@(v?\d+\.\d+\.\d+) .*/);
    if (m && (0, versions_1.isValidVersion)(m[1])) {
        return m[1].replace(/^v/, '');
    }
    return '';
}
async function installGomodifytags() {
    await (0, tools_1.installGoBin)(binaries_1.GOMODIFYTAGS, true);
}
async function installGotests() {
    await (0, tools_1.installGoBin)(binaries_1.GOTESTS, true);
}
async function installGoplay() {
    await (0, tools_1.installGoBin)(binaries_1.GOPLAY, true);
}
async function installImpl() {
    await (0, tools_1.installGoBin)(binaries_1.IMPL, true);
}
async function installTools() {
    for (const tool of binaries_1.TOOLS) {
        await (0, tools_1.installGoBin)(tool, true);
    }
}
//# sourceMappingURL=commands.js.map