import fs from "node:fs";
import path from "node:path";
/**
 * Read a task's git handoff for a specific repo, falling back to legacy flat
 * fields for single-repo tasks. Returns an empty object when neither is set.
 *
 * Callers should always go through this helper rather than reading flat fields
 * directly so single- and multi-repo tasks behave uniformly.
 */
export function readHandoff(task, repo) {
    if (task.gitHandoff && task.gitHandoff[repo])
        return { ...task.gitHandoff[repo] };
    return { baseSha: task.baseSha, headSha: task.headSha, branch: task.branch };
}
/**
 * Simple JSON file store for tasks.
 * Tasks persist across server restarts.
 */
export class TaskStore {
    storeDir;
    storeFile;
    tasks = new Map();
    constructor(stateDir) {
        this.storeDir = stateDir;
        this.storeFile = path.join(stateDir, "tasks.json");
        this.load();
    }
    load() {
        try {
            if (fs.existsSync(this.storeFile)) {
                const data = JSON.parse(fs.readFileSync(this.storeFile, "utf-8"));
                if (Array.isArray(data)) {
                    for (const t of data) {
                        if (t.id && t.goalId && t.title && t.type && t.state) {
                            // Migrate old field names
                            if (t.workflowArtifactId && !t.workflowGateId) {
                                t.workflowGateId = t.workflowArtifactId;
                                delete t.workflowArtifactId;
                            }
                            if (t.inputArtifactIds && !t.inputGateIds) {
                                t.inputGateIds = t.inputArtifactIds;
                                delete t.inputArtifactIds;
                            }
                            // Migrate commitSha -> headSha
                            if (t.commitSha && !t.headSha) {
                                t.headSha = t.commitSha;
                            }
                            delete t.commitSha;
                            this.tasks.set(t.id, t);
                        }
                    }
                }
            }
        }
        catch (err) {
            console.error("[task-store] Failed to load persisted tasks:", err);
        }
    }
    save() {
        try {
            if (!fs.existsSync(this.storeDir)) {
                fs.mkdirSync(this.storeDir, { recursive: true });
            }
            const data = Array.from(this.tasks.values());
            fs.writeFileSync(this.storeFile, JSON.stringify(data, null, 2), "utf-8");
        }
        catch (err) {
            console.error("[task-store] Failed to save tasks:", err);
        }
    }
    put(task) {
        this.tasks.set(task.id, task);
        this.save();
    }
    get(id) {
        return this.tasks.get(id);
    }
    remove(id) {
        this.tasks.delete(id);
        this.save();
    }
    removeMany(ids) {
        for (const id of ids) {
            this.tasks.delete(id);
        }
        if (ids.length > 0)
            this.save();
    }
    getAll() {
        return Array.from(this.tasks.values());
    }
    getByGoalId(goalId) {
        return this.getAll().filter((t) => t.goalId === goalId);
    }
    getBySessionId(sessionId) {
        return this.getAll().filter((t) => t.assignedSessionId === sessionId);
    }
    getByParentTaskId(parentTaskId) {
        return this.getAll().filter((t) => t.parentTaskId === parentTaskId);
    }
}
//# sourceMappingURL=task-store.js.map