import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useMemo, useState } from "react";
import { TaskRow } from "../components/TaskRow";
import { Select } from "../../components/ui/select";
import { deleteTask, updateTask } from "../services/taskService";
import { useEditContext } from "../../editor/client/editContext";
import { toast } from "sonner";
import { normalizeTaskStatus, getTaskStatusLabel } from "../taskStatus";
import { stripAgentConfigFromResultData } from "../taskAgentConfig";
import { TaskBoardFilterBox } from "../components/TaskBoardFilterBox";
export function ListView(props) {
    const { tasks, selectedTaskId, onSelectTask, onTasksChanged, projectId, permission, } = props;
    const editContext = useEditContext();
    const [filter, setFilter] = useState("");
    const [status, setStatus] = useState("All");
    const canEdit = permission === "Owner" || permission === "Editor";
    const handleDeleteTask = (task) => {
        const runDelete = async () => {
            const result = await deleteTask(task.taskId);
            if (result.type !== "success") {
                toast.error(result.summary || "Failed to delete task");
                return;
            }
            toast.success("Task deleted");
            await onTasksChanged();
        };
        if (editContext?.confirm) {
            editContext.confirm({
                header: "Delete task",
                message: `Are you sure you want to delete "${task.title}"?\n\nThis action cannot be undone.`,
                acceptLabel: "Delete",
                showCancel: true,
                accept: () => {
                    void runDelete();
                },
            });
            return;
        }
        void runDelete();
    };
    const handleTaskAssigneeChange = async (task, selection) => {
        const result = await updateTask(selection
            ? {
                taskId: task.taskId,
                assigneeType: selection.assigneeType,
                assigneeId: selection.assigneeId,
                ...(selection.assigneeType === "Agent"
                    ? { resultData: stripAgentConfigFromResultData(task.resultData) }
                    : {}),
            }
            : {
                taskId: task.taskId,
                assigneeType: "user",
                assigneeId: "",
            });
        if (result.type !== "success") {
            toast.error(result.summary ||
                (selection
                    ? "Failed to update assignee"
                    : "Failed to clear assignee"));
            return;
        }
        if (!selection) {
            toast.success("Assignee cleared");
        }
        await onTasksChanged();
    };
    const filtered = useMemo(() => {
        const q = filter.trim().toLowerCase();
        return tasks.filter((t) => {
            const taskStatus = normalizeTaskStatus(t.status, t.executionState);
            if (status !== "All" && taskStatus !== status)
                return false;
            if (!q)
                return true;
            return (t.title.toLowerCase().includes(q) ||
                (t.description || "").toLowerCase().includes(q) ||
                (t.taskKey || "").toLowerCase().includes(q) ||
                (t.assigneeDisplayName || "").toLowerCase().includes(q) ||
                (t.assigneeId || "").toLowerCase().includes(q));
        });
    }, [filter, status, tasks]);
    return (_jsx(_Fragment, { children: _jsxs("div", { className: "grid gap-4", "data-testid": "taskboard-list-view", children: [_jsxs("div", { className: "flex flex-col items-stretch justify-between gap-2 sm:flex-row sm:items-center", children: [_jsx(TaskBoardFilterBox, { value: filter, onChange: setFilter }), _jsxs("div", { className: "flex items-center justify-between gap-2 sm:justify-end", children: [_jsx("div", { className: "text-muted-foreground text-xs", children: "Status" }), _jsx(Select, { "data-testid": "taskboard-list-status-filter", value: status, onValueChange: (v) => setStatus(v), options: [
                                        { value: "All", label: "All" },
                                        { value: "Todo", label: getTaskStatusLabel("Todo") },
                                        { value: "Waiting", label: getTaskStatusLabel("Waiting") },
                                        {
                                            value: "InProgress",
                                            label: getTaskStatusLabel("InProgress"),
                                        },
                                        { value: "Review", label: getTaskStatusLabel("Review") },
                                        { value: "Done", label: getTaskStatusLabel("Done") },
                                    ], className: "h-7 w-[140px] text-xs" })] })] }), _jsx("div", { className: "overflow-hidden rounded-md bg-white p-2", "data-testid": "taskboard-task-list", children: _jsx("div", { className: "overflow-x-auto", children: _jsxs("div", { className: "min-w-[41rem]", children: [_jsxs("div", { className: "border-border-default/70 text-neutral-grey-50 grid grid-cols-[minmax(0,1fr)_5.5rem_6rem_10rem_2.5rem] gap-2.5 border-b bg-white px-3.5 py-2 text-xs font-normal tracking-wider", children: [_jsx("div", { children: "Task" }), _jsx("div", { children: "Priority" }), _jsx("div", { children: "Status" }), _jsx("div", { children: "Assignee" }), _jsx("div", { className: "text-right", children: "\u00A0" })] }), _jsx("div", { className: "divide-border-default/70 divide-y", children: filtered.length === 0 ? (_jsx("div", { className: "flex flex-col items-center justify-center py-12", "data-testid": "taskboard-task-list-empty", children: _jsx("p", { className: "text-neutral-grey-50 text-sm", children: "No tasks found matching your filters" }) })) : (filtered.map((t) => (_jsx(TaskRow, { task: t, selected: t.taskId === selectedTaskId, onClick: () => onSelectTask(t.taskId), onDelete: canEdit ? () => handleDeleteTask(t) : undefined, projectId: projectId, canEditAssignee: canEdit, onAssigneeChange: (selection) => handleTaskAssigneeChange(t, selection) }, t.taskId)))) })] }) }) })] }) }));
}
//# sourceMappingURL=ListView.js.map