"use strict";
/**
 * Task Lists
 *
 * Listr2 integration with fallback task runner
 * @module utils/ui/tasks
 */
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.taskList = void 0;
const types_1 = require("./types");
const init_1 = require("./init");
const indicators_1 = require("./indicators");
const spinner_1 = require("./spinner");
/**
 * Fallback task runner (no Listr2)
 * Uses spinners for sequential task execution
 */
async function runTasksFallback(tasks) {
    const ctx = {};
    for (const task of tasks) {
        if (task.skip) {
            const skipResult = task.skip();
            if (skipResult) {
                console.log((0, indicators_1.info)(`${task.title} [skipped]`));
                continue;
            }
        }
        const spin = await (0, spinner_1.spinner)(task.title);
        try {
            await task.task(ctx);
            spin.succeed();
        }
        catch (e) {
            spin.fail(`${task.title}: ${e.message}`);
            throw e;
        }
    }
    return ctx;
}
/**
 * Create a task list for progress display
 * Uses Listr2 in TTY mode, falls back to spinners in non-TTY
 */
async function taskList(tasks, options = {}) {
    // Lazy load Listr2 if not already loaded
    if (!types_1.moduleCache.listr && (0, init_1.isInteractive)()) {
        try {
            const listr2 = await Promise.resolve().then(() => __importStar(require('listr2')));
            types_1.moduleCache.listr = listr2.Listr;
        }
        catch (_e) {
            // Fallback to sequential execution with spinners
            return runTasksFallback(tasks);
        }
    }
    if (types_1.moduleCache.listr && (0, init_1.isInteractive)()) {
        // Determine renderer based on context
        // Use 'simple' in non-TTY, CI, or Claude Code context
        const useSimple = (0, init_1.isClaudeCodeContext)();
        const list = new types_1.moduleCache.listr(tasks.map((t) => ({
            title: t.title,
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            task: async (ctx) => t.task(ctx),
            skip: t.skip,
        })), {
            concurrent: options.concurrent ?? false,
            renderer: useSimple ? 'simple' : 'default',
            rendererOptions: {
                showSubtasks: true,
                collapseSubtasks: false,
            },
        });
        return list.run({});
    }
    // Fallback: non-interactive or Listr2 not available
    return runTasksFallback(tasks);
}
exports.taskList = taskList;
//# sourceMappingURL=tasks.js.map