import { type PropsWithChildren } from 'react';

import {
  type SidebarCapabilities,
  type SidebarModel,
} from '@composed-components/LibrariesSidebar/types/model';

export type LibrariesTreeItem = {
  title: string;
  id: number;
  icon: import('@src/types/icons').IconInterface;
  position: number | null;
  hasChildren: boolean;
  parentId: number | null;
  hasReachedLimit?: boolean;
  isActive: boolean;
  showBadge?: boolean;
  hideCreateButton?: boolean;
};

export type SidebarChildRowProps = Pick<LibrariesSidebarProps, 'onAdd'> & {
  item: LibrariesTreeItem;
  depth: number;
};
export type SidebarRootListProps = Pick<LibrariesSidebarProps, 'loading'>;

export type SortableRootRowProps = { item: LibrariesTreeItem };

export type SidebarButtonBase = {
  tooltipTitle?: string;
  label?: string;
};

export type SidebarSlotProps = {
  sortButton?: SidebarButtonBase;
  addButton?: SidebarButtonBase;
};

export type SortedItem = {
  items: LibrariesTreeItem[];
  parentId: number | null;
};

export type SortOrder = {
  parentId: number | null;
  orderedIds: number[];
};

export type SidebarContextValue = Pick<
  LibrariesSidebarProps,
  'onAddMouseEnter' | 'onItemClick'
> & {
  isSortMode: boolean;
  showSortButton: boolean;
  getTreeItem: (id: number) => LibrariesTreeItem | undefined;
  getOrderedItems: (parentId?: number) => LibrariesTreeItem[];
  setPendingSort: (items: LibrariesTreeItem[], parentId?: number) => void;
  handleToggleSortMode: () => void;
  handleRestoreSortableItems: () => void;
  handleSaveSort: () => void;
  expandedIds: Set<number>;
  setRowExpanded: (id: number, expanded: boolean) => void;
};

export type SidebarProviderProps = PropsWithChildren<
  Pick<
    LibrariesSidebarProps,
    'model' | 'capabilities' | 'onSort' | 'onAddMouseEnter' | 'onItemClick'
  >
>;

export type SidebarAddFunction = (parentId?: number) => void;

export type LibrariesSidebarProps = {
  model: SidebarModel;
  capabilities: SidebarCapabilities;
  parentId?: number;
  loading?: boolean;
  slotProps?: SidebarSlotProps;
  onAddMouseEnter?: (articleId?: number) => void;
  onBack: () => void;
  onSort?: (sortedItems: SortedItem[]) => void;
  onAdd?: SidebarAddFunction;
  onItemClick?: (item: LibrariesTreeItem) => void;
};
