import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useMemo, useState, useSyncExternalStore } from "react";
import { ClipboardCheck } from "lucide-react";
import { SearchInput } from "../../components/SearchInput";
import { cn } from "../../lib/utils";
import { getTaskBoardNavSnapshot, setTaskBoardNavState, subscribeTaskBoardNav, } from "../taskBoardNavStore";
import { useEditContext } from "../../editor/client/editContext";
import { getMyTasks } from "../services/taskService";
import { normalizeTaskStatus } from "../taskStatus";
export function TaskBoardMyTasksSidebar() {
    const editContext = useEditContext();
    const nav = useSyncExternalStore(subscribeTaskBoardNav, getTaskBoardNavSnapshot, getTaskBoardNavSnapshot);
    const [search, setSearch] = useState("");
    useEffect(() => {
        if (!editContext?.user?.name) {
            setTaskBoardNavState({ myTasks: [], myTasksLoading: false });
            return;
        }
        let cancelled = false;
        setTaskBoardNavState({ myTasksLoading: true });
        (async () => {
            const result = await getMyTasks({ excludeDone: true });
            if (cancelled)
                return;
            if (result.type !== "success") {
                setTaskBoardNavState({ myTasks: [], myTasksLoading: false });
                return;
            }
            const list = (result.data ?? [])
                .map((item) => ({
                taskId: item.task.taskId,
                projectId: item.task.projectId,
                title: item.task.title,
                projectTitle: item.projectTitle ?? "",
                status: normalizeTaskStatus(item.task.status, item.task.executionState),
                updatedAt: item.task.updatedAt,
            }))
                .sort((a, b) => {
                const aTime = a.updatedAt ? new Date(a.updatedAt).getTime() : 0;
                const bTime = b.updatedAt ? new Date(b.updatedAt).getTime() : 0;
                return bTime - aTime;
            });
            setTaskBoardNavState({ myTasks: list, myTasksLoading: false });
        })().catch(() => {
            if (cancelled)
                return;
            setTaskBoardNavState({ myTasks: [], myTasksLoading: false });
        });
        return () => {
            cancelled = true;
        };
    }, [editContext?.user?.name]);
    const items = useMemo(() => {
        const query = search.trim().toLowerCase();
        if (!query)
            return nav.myTasks;
        return nav.myTasks.filter((task) => {
            return (task.title.toLowerCase().includes(query) ||
                task.projectTitle.toLowerCase().includes(query) ||
                task.status.toLowerCase().includes(query));
        });
    }, [nav.myTasks, search]);
    return (_jsxs("div", { className: "flex h-full min-h-0 flex-col bg-white", "data-testid": "taskboard-my-tasks-sidebar", children: [_jsx("div", { className: "border-border-default border-b px-3 py-2.5", children: _jsx(SearchInput, { value: search, onChange: setSearch, placeholder: "Search my tasks...", size: "md", searchIconPosition: "left", testId: "taskboard-my-tasks-search" }) }), _jsx("div", { className: "min-h-0 flex-1 overflow-y-auto p-2", children: nav.myTasksLoading ? (_jsx("div", { className: "text-neutral-grey-50 rounded-md border border-dashed px-3 py-8 text-center text-xs", children: "Loading my tasks..." })) : items.length === 0 ? (_jsx("div", { className: "text-neutral-grey-50 rounded-md border border-dashed px-3 py-8 text-center text-xs", children: "No tasks assigned to you" })) : (_jsx("div", { className: "flex flex-col gap-1", children: items.map((task) => {
                        const selected = nav.selectedProjectId === task.projectId &&
                            nav.selectedTaskId === task.taskId;
                        return (_jsxs("button", { type: "button", className: cn("w-full rounded-md border px-2 py-2 text-left transition-colors", selected
                                ? "border-border-default bg-neutral-grey-5"
                                : "hover:border-border-default hover:bg-neutral-grey-5/70 border-transparent"), onClick: () => nav.onSelectMyTask?.({
                                projectId: task.projectId,
                                taskId: task.taskId,
                            }), "data-testid": `taskboard-my-task-option-${task.taskId}`, children: [_jsxs("div", { className: "flex items-center gap-1.5", children: [_jsx(ClipboardCheck, { className: "text-neutral-grey-50 h-3.5 w-3.5" }), _jsx("span", { className: "text-neutral-grey-100 truncate text-xs font-medium", children: task.title })] }), _jsx("div", { className: "text-neutral-grey-50 mt-0.5 truncate pl-5 text-[10px]", children: task.projectTitle }), _jsx("div", { className: "text-neutral-grey-50 mt-1 pl-5 text-[10px] font-medium tracking-wider", children: task.status })] }, task.taskId));
                    }) })) })] }));
}
//# sourceMappingURL=TaskBoardMyTasksSidebar.js.map