/**
 * Per-profile task persistence. Stores tasks.json in the profile data dir.
 */
import { join } from 'path';
import { existsSync, readFileSync } from 'fs';
import { getDataDir, atomicWriteFileSync, generateNodeId } from './helpers.js';
function tasksPath() {
    return join(getDataDir(), 'tasks.json');
}
export function readTasks() {
    const p = tasksPath();
    if (!existsSync(p))
        return [];
    try {
        return JSON.parse(readFileSync(p, 'utf-8'));
    }
    catch {
        return [];
    }
}
export function writeTasks(tasks) {
    atomicWriteFileSync(tasksPath(), JSON.stringify(tasks, null, 2));
}
export function addTask(text) {
    const tasks = readTasks();
    const maxPos = tasks.reduce((m, t) => Math.max(m, t.position), -1);
    const task = { id: generateNodeId(), text, completed: false, position: maxPos + 1 };
    tasks.push(task);
    writeTasks(tasks);
    return task;
}
export function updateTask(id, patch) {
    const tasks = readTasks();
    const task = tasks.find((t) => t.id === id);
    if (!task)
        return null;
    if (patch.text !== undefined)
        task.text = patch.text;
    if (patch.completed !== undefined)
        task.completed = patch.completed;
    writeTasks(tasks);
    return task;
}
export function removeTask(id) {
    const tasks = readTasks();
    const idx = tasks.findIndex((t) => t.id === id);
    if (idx === -1)
        return false;
    tasks.splice(idx, 1);
    writeTasks(tasks);
    return true;
}
