import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import React from 'react';
import { TaskListDisplay } from '../components/task-list-display.js';
import { useTheme } from '../hooks/useTheme.js';
import { generateKey } from '../session/key-generator.js';
import { clearAllTasks, generateTaskId, loadTasks, saveTasks, } from '../tools/tasks/storage.js';
function TaskMessage({ message, isError }) {
    const { colors } = useTheme();
    return (_jsx(Box, { marginY: 1, children: _jsx(Text, { color: isError ? colors.error : colors.success, children: message }) }));
}
function TasksDisplay({ tasks, message, isError }) {
    return (_jsxs(Box, { flexDirection: "column", children: [message && _jsx(TaskMessage, { message: message, isError: isError }), _jsx(TaskListDisplay, { tasks: tasks, title: "Tasks" })] }));
}
export const tasksCommand = {
    name: 'tasks',
    description: 'Manage your task list',
    handler: async (args) => {
        const subcommand = args[0]?.toLowerCase();
        const rest = args.slice(1).join(' ');
        // No subcommand - show task list
        if (!subcommand) {
            const tasks = await loadTasks();
            return React.createElement(TasksDisplay, {
                key: generateKey('tasks-list'),
                tasks,
            });
        }
        // Add task
        if (subcommand === 'add') {
            if (!rest.trim()) {
                return React.createElement(TaskMessage, {
                    key: generateKey('tasks-error'),
                    message: 'Usage: /tasks add <title>',
                    isError: true,
                });
            }
            const tasks = await loadTasks();
            const now = new Date().toISOString();
            const newTask = {
                id: generateTaskId(),
                title: rest.trim(),
                status: 'pending',
                createdAt: now,
                updatedAt: now,
            };
            tasks.push(newTask);
            await saveTasks(tasks);
            return React.createElement(TasksDisplay, {
                key: generateKey('tasks-added'),
                tasks,
                message: `Added: ${newTask.title}`,
            });
        }
        // Remove task
        if (subcommand === 'remove' || subcommand === 'rm') {
            if (!rest.trim()) {
                return React.createElement(TaskMessage, {
                    key: generateKey('tasks-error'),
                    message: 'Usage: /tasks remove <number>',
                    isError: true,
                });
            }
            const taskNumber = parseInt(rest.trim(), 10);
            if (isNaN(taskNumber) || taskNumber < 1) {
                return React.createElement(TaskMessage, {
                    key: generateKey('tasks-error'),
                    message: 'Please provide a valid task number (e.g., /tasks remove 1)',
                    isError: true,
                });
            }
            const tasks = await loadTasks();
            const taskIndex = taskNumber - 1;
            if (taskIndex >= tasks.length) {
                return React.createElement(TaskMessage, {
                    key: generateKey('tasks-error'),
                    message: `Task ${taskNumber} not found. You have ${tasks.length} task(s).`,
                    isError: true,
                });
            }
            const removed = tasks.splice(taskIndex, 1)[0];
            await saveTasks(tasks);
            return React.createElement(TasksDisplay, {
                key: generateKey('tasks-removed'),
                tasks,
                message: `Removed: ${removed.title}`,
            });
        }
        // Clear all
        if (subcommand === 'clear') {
            await clearAllTasks();
            return React.createElement(TasksDisplay, {
                key: generateKey('tasks-cleared'),
                tasks: [],
                message: 'All tasks cleared',
            });
        }
        // Unknown subcommand - treat as task title to add
        const fullTitle = args.join(' ').trim();
        const tasks = await loadTasks();
        const now = new Date().toISOString();
        const newTask = {
            id: generateTaskId(),
            title: fullTitle,
            status: 'pending',
            createdAt: now,
            updatedAt: now,
        };
        tasks.push(newTask);
        await saveTasks(tasks);
        return React.createElement(TasksDisplay, {
            key: generateKey('tasks-added'),
            tasks,
            message: `Added: ${newTask.title}`,
        });
    },
};
//# sourceMappingURL=tasks.js.map