import { createHash, randomUUID } from "node:crypto";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { basename, join } from "node:path";
const TASKS_BASE = join(homedir(), ".gg-tasks", "projects");
function hashPath(cwd) {
    return createHash("sha256").update(cwd).digest("hex").slice(0, 16);
}
function projectDir(cwd) {
    return join(TASKS_BASE, hashPath(cwd));
}
function taskFilePath(cwd) {
    return join(projectDir(cwd), "tasks.json");
}
function migrateTask(task) {
    if (!task.prompt && task.text) {
        return { ...task, title: task.title || task.text, prompt: task.text, text: undefined };
    }
    return task;
}
export function createTaskRecord(title, prompt) {
    return {
        id: randomUUID(),
        title,
        prompt,
        status: "pending",
        createdAt: new Date().toISOString(),
    };
}
export async function loadTasks(cwd) {
    try {
        const data = await readFile(taskFilePath(cwd), "utf-8");
        const raw = JSON.parse(data);
        return raw.map(migrateTask);
    }
    catch {
        return [];
    }
}
export function loadTasksSync(cwd) {
    try {
        const data = readFileSync(taskFilePath(cwd), "utf-8");
        const raw = JSON.parse(data);
        return raw.map(migrateTask);
    }
    catch {
        return [];
    }
}
export async function saveTasks(cwd, tasks) {
    const dir = projectDir(cwd);
    await mkdir(dir, { recursive: true });
    await writeFile(join(dir, "tasks.json"), JSON.stringify(tasks, null, 2) + "\n", "utf-8");
    const meta = JSON.stringify({ path: cwd, name: basename(cwd) }, null, 2) + "\n";
    await writeFile(join(dir, "meta.json"), meta, "utf-8");
}
export function saveTasksSync(cwd, tasks) {
    writeFileSync(taskFilePath(cwd), JSON.stringify(tasks, null, 2) + "\n", "utf-8");
}
export function getTaskCount(cwd) {
    return loadTasksSync(cwd).filter((task) => task.status !== "done").length;
}
export function getNextPendingTask(cwd) {
    const pending = loadTasksSync(cwd).find((task) => task.status === "pending");
    if (!pending)
        return null;
    return {
        id: pending.id,
        title: pending.title,
        prompt: pending.prompt || pending.text || pending.title,
    };
}
/**
 * Drop every completed task and persist the pruned list, returning the
 * survivors. Used by the desktop app so finished tasks disappear from the Tasks
 * modal on completion instead of lingering with a "done" badge. No-op write
 * when nothing was done (keeps the file untouched on idle runs).
 */
export function pruneDoneTasksSync(cwd) {
    const tasks = loadTasksSync(cwd);
    const remaining = tasks.filter((task) => task.status !== "done");
    if (remaining.length !== tasks.length)
        saveTasksSync(cwd, remaining);
    return remaining;
}
export function markTaskInProgress(cwd, taskId) {
    const tasks = loadTasksSync(cwd);
    if (tasks.length === 0)
        return;
    const updated = tasks.map((task) => task.id === taskId ? { ...task, status: "in-progress" } : task);
    saveTasksSync(cwd, updated);
}
//# sourceMappingURL=tasks-store.js.map