import chalk from "chalk";
import { LinearRestClient } from "../../integrations/linear/rest-client.js";
function getEnv(key, defaultValue) {
  const value = process.env[key];
  if (value === void 0) {
    if (defaultValue !== void 0) return defaultValue;
    throw new Error(`Environment variable ${key} is required`);
  }
  return value;
}
function getOptionalEnv(key) {
  return process.env[key];
}
function registerLinearListCommand(parent) {
  parent.command("linear:list").alias("linear:ls").description("List Linear tasks (fast, memory-cached)").option("--limit <n>", "Number of tasks to show", "20").option(
    "--status <status>",
    "Filter by status (backlog, started, completed, etc.)"
  ).option("--my", "Show only tasks assigned to me").option("--cache", "Show cache stats only").option("--refresh", "Force refresh cache").option("--count", "Show count by status only").action(async (options) => {
    try {
      const apiKey = process.env["LINEAR_API_KEY"];
      if (!apiKey) {
        console.log(
          chalk.yellow("\u26A0 Set LINEAR_API_KEY environment variable")
        );
        return;
      }
      const restClient = new LinearRestClient(apiKey);
      if (options.cache) {
        const stats = restClient.getCacheStats();
        console.log(chalk.cyan("\u{1F4CA} Cache Stats:"));
        console.log(`  Size: ${stats.size} tasks`);
        console.log(`  Age: ${Math.round(stats.age / 1e3)}s`);
        console.log(`  Fresh: ${stats.fresh ? "yes" : "no"}`);
        console.log(
          `  Last sync: ${new Date(stats.lastSync).toLocaleString()}`
        );
        return;
      }
      if (options.count) {
        const counts = await restClient.getTaskCounts();
        console.log(chalk.cyan("\u{1F4CA} Task Counts:"));
        Object.entries(counts).sort(([, a], [, b]) => b - a).forEach(([status, count]) => {
          console.log(`  ${status}: ${count}`);
        });
        const cacheStats2 = restClient.getCacheStats();
        console.log(
          chalk.gray(
            `
Cache: ${cacheStats2.size} tasks, age: ${Math.round(cacheStats2.age / 1e3)}s`
          )
        );
        return;
      }
      let tasks;
      if (options.my) {
        tasks = await restClient.getMyTasks();
      } else if (options.status) {
        tasks = await restClient.getTasksByStatus(options.status);
      } else {
        tasks = await restClient.getAllTasks(options.refresh);
      }
      if (!tasks || tasks.length === 0) {
        console.log(chalk.gray("No tasks found"));
        return;
      }
      const limit = parseInt(options.limit);
      const displayTasks = tasks.slice(0, limit);
      console.log(
        chalk.cyan(
          `
\u{1F4CB} Linear Tasks (${displayTasks.length}/${tasks.length}):`
        )
      );
      displayTasks.forEach((task) => {
        const priority = task.priority ? `P${task.priority}` : "";
        const assignee = task.assignee ? ` @${task.assignee.name}` : "";
        const statusColor = task.state.type === "completed" ? chalk.green : task.state.type === "started" ? chalk.yellow : chalk.gray;
        console.log(`${chalk.blue(task.identifier)} ${task.title}`);
        console.log(
          chalk.gray(
            `  ${statusColor(task.state.name)} ${priority}${assignee}`
          )
        );
      });
      const cacheStats = restClient.getCacheStats();
      console.log(
        chalk.gray(
          `
${displayTasks.length} shown, ${tasks.length} total \u2022 Cache: ${Math.round(cacheStats.age / 1e3)}s old`
        )
      );
    } catch (error) {
      console.error(
        chalk.red("Failed to list tasks:"),
        error.message
      );
    }
  });
}
export {
  registerLinearListCommand
};
//# sourceMappingURL=linear-list.js.map
