import { readFileSync, existsSync } from "fs";
import { join } from "path";
class LinearTaskReader {
  tasksFile;
  mappingsFile;
  constructor(projectRoot) {
    const root = projectRoot || process.cwd();
    this.tasksFile = join(root, ".stackmemory", "tasks.jsonl");
    this.mappingsFile = join(root, ".stackmemory", "linear-mappings.json");
  }
  /**
   * Read tasks from the Linear-synced JSONL file
   */
  getTasks() {
    if (!existsSync(this.tasksFile)) {
      console.log(
        'No tasks file found. Run "npm run linear:sync" to sync tasks.'
      );
      return [];
    }
    try {
      const content = readFileSync(this.tasksFile, "utf8");
      const lines = content.split("\n").filter(Boolean);
      const tasks = lines.map((line) => {
        const task = JSON.parse(line);
        return {
          id: task.id || task.linearId,
          identifier: task.taskId || task.linearId || task.id,
          title: task.title,
          description: task.description || "",
          state: task.linearState || this.mapStatusToState(task.status),
          priority: task.priority || 4,
          estimate: task.estimate,
          assignee: task.assignee,
          createdAt: task.createdAt,
          updatedAt: task.updatedAt,
          lastSyncedAt: task.updatedAt,
          syncStatus: "synced"
        };
      });
      return tasks;
    } catch (error) {
      console.error("Error reading tasks:", error);
      return [];
    }
  }
  /**
   * Get task mappings between Linear and local IDs
   */
  getMappings() {
    if (!existsSync(this.mappingsFile)) {
      return /* @__PURE__ */ new Map();
    }
    try {
      const content = readFileSync(this.mappingsFile, "utf8");
      const mappings = JSON.parse(content);
      return new Map(Object.entries(mappings));
    } catch (error) {
      console.error("Error reading mappings:", error);
      return /* @__PURE__ */ new Map();
    }
  }
  /**
   * Get active tasks (not completed or canceled)
   */
  getActiveTasks() {
    const allTasks = this.getTasks();
    return allTasks.filter(
      (task) => task.state !== "Done" && task.state !== "Canceled" && task.state !== "Duplicate"
    );
  }
  /**
   * Get tasks by state
   */
  getTasksByState(state) {
    const allTasks = this.getTasks();
    return allTasks.filter((task) => task.state === state);
  }
  /**
   * Map local status to Linear state name
   */
  mapStatusToState(status) {
    switch (status) {
      case "completed":
        return "Done";
      case "in_progress":
        return "In Progress";
      case "cancelled":
        return "Canceled";
      case "backlog":
        return "Backlog";
      case "todo":
        return "Todo";
      default:
        return "Backlog";
    }
  }
}
export {
  LinearTaskReader
};
//# sourceMappingURL=linear-task-reader.js.map
