import { useMemo } from 'react';

type Identifier = string | number;

export interface Breadcrumb {
  id?: Identifier;
  name?: string;
  label?: string;
  title?: string;
}

export interface FolderClickHandlers {
  onDrop?: (...args: unknown[]) => void;
  onDragOver?: (...args: unknown[]) => void;
  onDragLeave?: (...args: unknown[]) => void;
}

export interface UseDocumentsPanelPropsArgs {
  currentFolderName?: string | null;
  breadcrumbs?: Breadcrumb[];
  refreshCurrentFolder?: () => void | Promise<void>;
  currentSubfolders?: unknown[];
  documents?: unknown[];
  searchResults?: unknown[] | null;
  isFilterActive?: boolean;
  folderClickHandlers: FolderClickHandlers;
  selectFolder?: (...args: unknown[]) => void;
  handleFolderDragStart?: (...args: unknown[]) => void;
  handleFolderDragEnd?: (...args: unknown[]) => void;
  draggedFolderId?: Identifier | null;
  handleFolderRename?: (...args: unknown[]) => void;
  openDocumentPreview?: (...args: unknown[]) => void;
  handleDocumentTitleUpdate?: (...args: unknown[]) => void;
  selectedDocumentIds?: Identifier[];
  selectedFolderIds?: Identifier[];
  focusedRowKey?: Identifier | string | null;
  draggedDocumentIds?: Identifier[];
  handleDocumentDragStart?: (...args: unknown[]) => void;
  handleDocumentDragEnd?: (...args: unknown[]) => void;
  searchLoading?: boolean;
  tagLookupById?: unknown;
  activeCorrespondentFilters?: Identifier[];
  selectedEntries?: unknown[];
  setFocusedRowKey?: (key: Identifier | string | null) => void;
  ensureAssetUrl?: (...args: unknown[]) => void;
  getDocumentAsset?: (...args: unknown[]) => unknown;
  toggleTagFilter?: (...args: unknown[]) => void;
  toggleCorrespondentFilter?: (...args: unknown[]) => void;
  handleDocumentTagDrop?: (...args: unknown[]) => void;
  documentsViewMode?: string;
  documentsSortField?: string;
  documentsSortDirection?: string;
  handleDocumentsSortFieldChange?: (field: string) => void;
  handleDocumentsSortDirectionToggle?: () => void;
  searchIncludeDescendants?: boolean;
  toggleSearchIncludeDescendants?: () => void;
  handleDocumentsViewModeChange?: (mode: string) => void;
  clearDocumentSelection?: () => void;
  handleDeleteSelection?: () => void;
  handleEntryPointerCore?: (...args: unknown[]) => void;
  inspectDocument?: (docId: Identifier | null, metadata?: unknown) => void;
  handleEntrySelection?: (...args: unknown[]) => void;
  tags?: unknown[];
  correspondents?: unknown[];
  documentLookup?: unknown;
  handleBulkTagAddFromDetail?: (...args: unknown[]) => void;
  handleBulkTagRemoveFromDetail?: (...args: unknown[]) => void;
  handleBulkCorrespondentAdd?: (...args: unknown[]) => void;
  handleBulkCorrespondentRemove?: (...args: unknown[]) => void;
  handleBulkSelectionReanalyze?: (...args: unknown[]) => void;
  folderOptions?: unknown[];
  moveDocumentsToFolder?: (...args: unknown[]) => void;
}

export type DocumentsPanelProps = ReturnType<typeof useDocumentsPanelProps>;

const useDocumentsPanelProps = (props: UseDocumentsPanelPropsArgs) => {
  const {
    currentFolderName,
    breadcrumbs,
    refreshCurrentFolder,
    currentSubfolders,
    documents,
    searchResults,
    isFilterActive,
    folderClickHandlers,
    selectFolder,
    handleFolderDragStart,
    handleFolderDragEnd,
    draggedFolderId,
    handleFolderRename,
    openDocumentPreview,
    handleDocumentTitleUpdate,
    selectedDocumentIds,
    selectedFolderIds,
    focusedRowKey,
    draggedDocumentIds,
    handleDocumentDragStart,
    handleDocumentDragEnd,
    searchLoading,
    tagLookupById,
    activeCorrespondentFilters,
    selectedEntries,
    setFocusedRowKey,
    ensureAssetUrl,
    getDocumentAsset,
    toggleTagFilter,
    toggleCorrespondentFilter,
    handleDocumentTagDrop,
    documentsViewMode,
    documentsSortField,
    documentsSortDirection,
    handleDocumentsSortFieldChange,
    handleDocumentsSortDirectionToggle,
    searchIncludeDescendants,
    toggleSearchIncludeDescendants,
    handleDocumentsViewModeChange,
    clearDocumentSelection,
    handleDeleteSelection,
    handleEntryPointerCore,
    inspectDocument,
    handleEntrySelection,
    tags,
    correspondents,
    documentLookup,
    handleBulkTagAddFromDetail,
    handleBulkTagRemoveFromDetail,
    handleBulkCorrespondentAdd,
    handleBulkCorrespondentRemove,
    handleBulkSelectionReanalyze,
    folderOptions,
    moveDocumentsToFolder,
  } = props;

  return useMemo(
    () => ({
      currentFolderName,
      breadcrumbs,
      onRefresh: refreshCurrentFolder,
      subfolders: currentSubfolders,
      documents,
      searchResults,
      isFilterActive,
      onFolderSelect: selectFolder,
      onFolderDrop: folderClickHandlers.onDrop,
      onFolderDragOver: folderClickHandlers.onDragOver,
      onFolderDragLeave: folderClickHandlers.onDragLeave,
      onFolderDragStart: handleFolderDragStart,
      onFolderDragEnd: handleFolderDragEnd,
      draggedFolderId,
      onFolderRename: handleFolderRename,
      onDocumentOpen: openDocumentPreview,
      onDocumentRename: handleDocumentTitleUpdate,
      selectedDocumentIds,
      selectedFolderIds,
      focusedRowKey,
      draggingDocumentIds: draggedDocumentIds,
      onDocumentDragStart: handleDocumentDragStart,
      onDocumentDragEnd: handleDocumentDragEnd,
      isSearchLoading: searchLoading,
      tagLookupById,
      activeCorrespondentIds: activeCorrespondentFilters,
      selectedEntries,
      onFocusedRowChange: setFocusedRowKey,
      ensureAssetUrl,
      getDocumentAsset,
      onTagClick: toggleTagFilter,
      onCorrespondentClick: toggleCorrespondentFilter,
      onDocumentTagDrop: handleDocumentTagDrop,
      viewMode: documentsViewMode,
      sortField: documentsSortField,
      sortDirection: documentsSortDirection,
      onSortFieldChange: handleDocumentsSortFieldChange,
      onSortDirectionToggle: handleDocumentsSortDirectionToggle,
      searchIncludeDescendants,
      onToggleSearchIncludeDescendants: toggleSearchIncludeDescendants,
      onViewModeChange: handleDocumentsViewModeChange,
      onClearSelection: clearDocumentSelection,
      onDeleteSelection: handleDeleteSelection,
      onEntryPointer: handleEntryPointerCore,
      onInspectDocument: inspectDocument,
      onEntrySelection: handleEntrySelection,
      tags,
      correspondents,
      documentLookup,
      onBulkTagAdd: handleBulkTagAddFromDetail,
      onBulkTagRemove: handleBulkTagRemoveFromDetail,
      onBulkCorrespondentAdd: handleBulkCorrespondentAdd,
      onBulkCorrespondentRemove: handleBulkCorrespondentRemove,
      onBulkReanalyze: handleBulkSelectionReanalyze,
      folderOptions,
      onMoveDocumentsToFolder: moveDocumentsToFolder,
    }),
    [
      activeCorrespondentFilters,
      breadcrumbs,
      clearDocumentSelection,
      correspondents,
      currentFolderName,
      currentSubfolders,
      documents,
      documentsSortDirection,
      documentsSortField,
      documentsViewMode,
      documentLookup,
      draggedDocumentIds,
      draggedFolderId,
      focusedRowKey,
      folderClickHandlers,
      handleBulkCorrespondentAdd,
      handleBulkCorrespondentRemove,
      handleBulkSelectionReanalyze,
      handleBulkTagAddFromDetail,
      handleBulkTagRemoveFromDetail,
      handleDeleteSelection,
      handleDocumentDragEnd,
      handleDocumentDragStart,
      handleDocumentTagDrop,
      handleDocumentTitleUpdate,
      handleDocumentsSortDirectionToggle,
      handleDocumentsSortFieldChange,
      handleDocumentsViewModeChange,
      handleEntryPointerCore,
      handleEntrySelection,
      handleFolderDragEnd,
      handleFolderDragStart,
      handleFolderRename,
      inspectDocument,
      isFilterActive,
      moveDocumentsToFolder,
      openDocumentPreview,
      refreshCurrentFolder,
      searchIncludeDescendants,
      searchLoading,
      searchResults,
      selectedDocumentIds,
      selectedEntries,
      selectedFolderIds,
      selectFolder,
      setFocusedRowKey,
      tagLookupById,
      tags,
      toggleCorrespondentFilter,
      toggleSearchIncludeDescendants,
      toggleTagFilter,
      ensureAssetUrl,
      getDocumentAsset,
      folderOptions,
    ],
  );
};

export default useDocumentsPanelProps;
