"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.inContinuousIntegration = inContinuousIntegration;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const agentInstallRegistry_1 = require("./agentInstallRegistry");
const chalk = require('chalk');
console.log(chalk.cyan(`
 ┌──────────────────────────────────────────────┐
 │         Welcome to the mabl CLI tool!        │
 └──────────────────────────────────────────────┘
 By using this product you agree to the terms of
 use described in the License.txt file and the
 terms of service at: https://www.mabl.com/tos

 run:`, chalk.magenta(`mabl -h`), `for help
`));
const AUTO_UPDATE_TIMEOUT_MS = 60_000;
maybeRefreshStaleAgentSkills();
function maybeRefreshStaleAgentSkills() {
    try {
        const currentVersion = (0, agentInstallRegistry_1.readCliVersion)();
        if (!currentVersion) {
            return;
        }
        const { installations } = (0, agentInstallRegistry_1.readRegistry)();
        if (installations.length === 0) {
            return;
        }
        const stale = (0, agentInstallRegistry_1.getStaleInstallations)(installations, currentVersion);
        if (stale.length === 0) {
            return;
        }
        const cliPath = path.join(__dirname, '..', 'cli.js');
        if (!inContinuousIntegration() && fs.existsSync(cliPath)) {
            try {
                (0, child_process_1.execFileSync)(process.execPath, [cliPath, 'agent', 'update', '--yes', '--quiet'], { stdio: 'inherit', timeout: AUTO_UPDATE_TIMEOUT_MS });
                return;
            }
            catch {
            }
        }
        printStaleNotice(stale.length);
    }
    catch {
    }
}
function inContinuousIntegration() {
    const value = process.env.CI;
    return (value !== undefined && value !== '' && value !== 'false' && value !== '0');
}
function printStaleNotice(count) {
    const isSingle = count === 1;
    const subject = isSingle ? 'agent skill was' : 'agent skills were';
    const pronoun = isSingle ? 'it' : 'them';
    console.log(chalk.yellow(` ${count} ${subject} installed by an older CLI version.`));
    console.log(` Run`, chalk.magenta('mabl agent update'), `to refresh ${pronoun}.\n`);
}
