"use client";
import { useCallback, useMemo, useState } from "react";
import type { IWatermarkPanelProps } from "dockview-react";
import { IconPlus, IconLayoutSidebarRightCollapse } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from "@kandev/ui/dropdown-menu";
import { useDockviewStore } from "@/lib/state/dockview-store";
import { useAppStore } from "@/components/state-provider";
import { useEnvironmentId } from "@/hooks/use-environment-session-id";
import { useUserShells } from "@/hooks/domains/session/use-user-shells";
import { useTaskPR } from "@/hooks/domains/github/use-task-pr";
import { createUserShell } from "@/lib/api/domains/user-shell-api";
import { AddPanelMenuItems } from "./dockview-add-panel-items";
import { NewSessionDialog } from "./new-session-dialog";
import { useActiveSessionDevScript } from "./repository-scripts-menu";
/**
* Watermark rendered by Dockview when a group becomes empty (e.g. after
* the user splits a group and the new half has no panels yet). Mirrors
* the header "+" menu so the user gets the same rich picker — existing
* sessions and terminals are listed (clicking re-focuses an open tab or
* re-opens a parked one) instead of always minting a fresh panel.
*
* Before this, the watermark's terminal button called createUserShell
* without a taskId, so it produced a legacy passthrough shell that
* never appeared in the user-shell list at all.
*/
export function DockviewWatermark({ containerApi, group }: IWatermarkPanelProps) {
const groupId = group?.id;
const environmentId = useEnvironmentId();
const taskID = useAppStore((s) => s.tasks?.activeTaskId ?? null);
const sidebarGroupId = useDockviewStore((s) => s.sidebarGroupId);
const [showNewSessionDialog, setShowNewSessionDialog] = useState(false);
// Eagerly populate the user-shell store so the Terminals submenu has
// its list ready the moment the watermark opens.
useUserShells(environmentId, taskID);
const menuState = useWatermarkMenuState(containerApi, taskID);
const handlers = useWatermarkHandlers(groupId, environmentId, taskID);
if (group?.id === sidebarGroupId) return null;
if (!groupId) return null;
return (