import { useState } from "react";
import { useEditContext } from "../client/editContext";
export function useNavigationPanelLogic() {
    const editContext = useEditContext();
    const [dragOverId, setDragOverId] = useState(null);
    const getDraggedSidebarId = (e) => {
        return (e.dataTransfer.getData("text/alpaca-sidebar-icon") ||
            e.dataTransfer.getData("text/alpaca-sidebar"));
    };
    const contextValues = editContext
        ? {
            workspaceId: editContext.workspaceId,
            sidebars: editContext.availableSidebars ?? [],
            openSidebars: editContext.openSidebars ?? [],
            pinnedSidebars: editContext.pinnedSidebars ?? [],
            lockedSidebars: editContext.lockedSidebars ?? [],
            sidebarStacks: editContext.sidebarStacks ?? [],
            showSidebarLabels: editContext.userInfo.preferences?.showSidebarLabels ?? false,
            visibleWorkspaces: editContext.visibleWorkspaces ?? [],
            isMobile: editContext.isMobile,
        }
        : null;
    const handleSidebarClick = (sidebarId, options) => {
        editContext?.toggleSidebar?.(sidebarId, options);
    };
    const handleOpenSidebar = (sidebarId) => {
        editContext?.openSidebar?.(sidebarId);
    };
    const handlePinClick = (sidebarId) => {
        editContext?.toggleSidebarPin?.(sidebarId);
    };
    const handleToggleLock = (sidebarId) => {
        editContext?.toggleSidebarLock?.(sidebarId);
    };
    const handleStack = (sourceId, targetId, position) => {
        editContext?.stackSidebar?.(sourceId, targetId, position);
    };
    const handleUnstack = (sidebarId) => {
        editContext?.unstackSidebar?.(sidebarId);
    };
    const handleWorkspaceClick = (targetWorkspaceId, options) => {
        if (targetWorkspaceId !== editContext?.workspaceId) {
            editContext?.switchWorkspace?.(targetWorkspaceId, options);
        }
    };
    const toggleShowSidebarLabels = () => {
        if (editContext) {
            const current = editContext.userInfo.preferences?.showSidebarLabels ?? false;
            editContext.setUserPreferences?.({ showSidebarLabels: !current });
        }
    };
    // Logic for visible items (pinned + open)
    const getVisibleItems = (sidebars) => {
        if (!contextValues)
            return [];
        const pinnedSet = new Set(contextValues.pinnedSidebars);
        const openSet = new Set(contextValues.openSidebars);
        const sidebarMap = new Map(sidebars.map((s) => [s.id, s]));
        const result = [];
        // Pinned items first
        for (const id of contextValues.pinnedSidebars) {
            const sidebar = sidebarMap.get(id);
            if (sidebar)
                result.push(sidebar);
        }
        // Open but not pinned
        for (const sidebar of sidebars) {
            if (openSet.has(sidebar.id) && !pinnedSet.has(sidebar.id)) {
                result.push(sidebar);
            }
        }
        return result;
    };
    const handleReorderPinnedSidebars = (pinnedSidebars) => {
        editContext?.reorderPinnedSidebars?.(pinnedSidebars);
    };
    const handleIconDragStart = (e, sidebarId) => {
        e.dataTransfer.setData("text/alpaca-sidebar-icon", sidebarId);
        e.dataTransfer.setData("text/alpaca-sidebar", sidebarId);
        e.dataTransfer.effectAllowed = "move";
    };
    const handleIconDragOver = (e, sidebarId) => {
        e.preventDefault();
        e.dataTransfer.dropEffect = "move";
        setDragOverId(sidebarId);
    };
    const handleIconDragLeave = () => {
        setDragOverId(null);
    };
    const handleIconDrop = (e, targetSidebarId) => {
        e.preventDefault();
        setDragOverId(null);
        const sourceSidebarId = getDraggedSidebarId(e);
        if (!sourceSidebarId || sourceSidebarId === targetSidebarId)
            return;
        const currentPinned = [...(contextValues?.pinnedSidebars ?? [])];
        const sourceIndex = currentPinned.indexOf(sourceSidebarId);
        const targetIndex = currentPinned.indexOf(targetSidebarId);
        if (sourceIndex === -1 || targetIndex === -1)
            return;
        currentPinned.splice(sourceIndex, 1);
        const newTargetIndex = currentPinned.indexOf(targetSidebarId);
        const insertIndex = sourceIndex < targetIndex ? newTargetIndex + 1 : newTargetIndex;
        currentPinned.splice(insertIndex, 0, sourceSidebarId);
        handleReorderPinnedSidebars(currentPinned);
    };
    return {
        contextValues,
        handlers: {
            onToggleSidebar: handleSidebarClick,
            onOpenSidebar: handleOpenSidebar,
            onTogglePin: handlePinClick,
            onToggleLock: handleToggleLock,
            onStack: handleStack,
            onUnstack: handleUnstack,
            onWorkspaceClick: handleWorkspaceClick,
            onToggleSidebarLabels: toggleShowSidebarLabels,
            onReorderPinnedSidebars: handleReorderPinnedSidebars,
            onIconDragStart: handleIconDragStart,
            onIconDragOver: handleIconDragOver,
            onIconDragLeave: handleIconDragLeave,
            onIconDrop: handleIconDrop,
        },
        dragOverId,
        getVisibleItems,
    };
}
//# sourceMappingURL=useNavigationPanelLogic.js.map