import { useMemo } from 'react';
import type { WorkspaceSelectionValue } from '../../app/WorkspaceSelectionContext';
import type { Identifier } from '../../types/identifiers';

interface DocumentLinkLike {
  url?: string | null;
  mimeType?: string | null;
}

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[];
  searchQuery?: string;
  searchResultIds?: Identifier[] | null;
  folderClickHandlers: FolderClickHandlers;
  selectedFolder?: Identifier | 'root' | null;
  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;
  focusedRowKey?: Identifier | string | null;
  draggedDocumentIds?: Identifier[];
  handleDocumentDragStart?: (...args: unknown[]) => void;
  handleDocumentDragEnd?: (...args: unknown[]) => void;
  searchLoading?: boolean;
  tagLookupById?: unknown;
  activeTagFilters?: Identifier[];
  activeCorrespondentFilters?: Identifier[];
  ensureAssetUrl?: (...args: unknown[]) => void;
  getDocumentAsset?: (...args: unknown[]) => unknown;
  handleDocumentTagAttach?: (docId: Identifier, tagId: Identifier) => void;
  handleDocumentTagDetach?: (docId: Identifier, tagId: Identifier) => void;
  documentsViewMode?: string;
  documentsSortField?: string;
  documentsSortDirection?: string;
  handleDocumentsSortFieldChange?: (field: string) => void;
  handleDocumentsSortDirectionToggle?: () => void;
  handleDocumentsViewModeChange?: (mode: string) => void;
  clearDocumentSelection?: () => void;
  handleDeleteSelection?: () => void;
  handleEntryPointerCore?: (...args: unknown[]) => void;
  onDocumentActivate?: (docId: Identifier | null, metadata?: 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;
  documentLinks?: Map<Identifier, DocumentLinkLike>;
  ensureDownloadUrl?: (documentId: Identifier, options?: { force?: boolean }) => Promise<DocumentLinkLike | null>;
  selectionValue: WorkspaceSelectionValue;
  currentTenantId?: Identifier | null;
}

export type DocumentsPanelProps = ReturnType<typeof useDocumentsPanelProps>;

const useDocumentsPanelProps = (props: UseDocumentsPanelPropsArgs) => {
  const {
    currentFolderName,
    breadcrumbs,
    refreshCurrentFolder,
    currentSubfolders,
    documents,
    searchQuery,
    searchResultIds,
    folderClickHandlers,
    selectedFolder,
    selectFolder,
    handleFolderDragStart,
    handleFolderDragEnd,
    draggedFolderId,
    handleFolderRename,
    openDocumentPreview,
    handleDocumentTitleUpdate,
    focusedRowKey,
    draggedDocumentIds,
    handleDocumentDragStart,
    handleDocumentDragEnd,
    searchLoading,
    tagLookupById,
    activeTagFilters,
    activeCorrespondentFilters,
    ensureAssetUrl,
    getDocumentAsset,
    handleDocumentTagAttach,
    handleDocumentTagDetach,
    documentsViewMode,
    documentsSortField,
    documentsSortDirection,
    handleDocumentsSortFieldChange,
    handleDocumentsSortDirectionToggle,
    handleDocumentsViewModeChange,
    handleDeleteSelection,
    handleEntryPointerCore,
    onDocumentActivate,
    tags,
    correspondents,
    documentLookup,
    handleBulkTagAddFromDetail,
    handleBulkTagRemoveFromDetail,
    handleBulkCorrespondentAdd,
    handleBulkCorrespondentRemove,
    handleBulkSelectionReanalyze,
    folderOptions,
    moveDocumentsToFolder,
    documentLinks,
    ensureDownloadUrl,
    selectionValue,
    currentTenantId,
  } = props;

  return useMemo(
    () => ({
      currentFolderName,
      breadcrumbs,
      onRefresh: refreshCurrentFolder,
      subfolders: currentSubfolders,
      documents,
      searchQuery,
      searchResultIds,
      selectedFolder,
      onFolderSelect: selectFolder,
      onFolderDrop: folderClickHandlers.onDrop,
      onFolderDragOver: folderClickHandlers.onDragOver,
      onFolderDragLeave: folderClickHandlers.onDragLeave,
      onFolderDragStart: handleFolderDragStart,
      onFolderDragEnd: handleFolderDragEnd,
      draggedFolderId,
      onFolderRename: handleFolderRename,
      onDocumentOpen: openDocumentPreview,
      onDocumentRename: handleDocumentTitleUpdate,
      focusedRowKey,
      draggingDocumentIds: draggedDocumentIds,
      onDocumentDragStart: handleDocumentDragStart,
      onDocumentDragEnd: handleDocumentDragEnd,
      isSearchLoading: searchLoading,
      tagLookupById,
      activeTagFilters,
      activeCorrespondentFilters,
      activeCorrespondentIds: activeCorrespondentFilters,
      ensureAssetUrl,
      getDocumentAsset,
      onDocumentTagAttach: handleDocumentTagAttach,
      onDocumentTagDetach: handleDocumentTagDetach,
      viewMode: documentsViewMode,
      sortField: documentsSortField,
      sortDirection: documentsSortDirection,
      onSortFieldChange: handleDocumentsSortFieldChange,
      onSortDirectionToggle: handleDocumentsSortDirectionToggle,
      onViewModeChange: handleDocumentsViewModeChange,
      onDeleteSelection: handleDeleteSelection,
      onEntryPointer: handleEntryPointerCore,
      onDocumentActivate,
      tags,
      correspondents,
      documentLookup,
      onBulkTagAdd: handleBulkTagAddFromDetail,
      onBulkTagRemove: handleBulkTagRemoveFromDetail,
      onBulkCorrespondentAdd: handleBulkCorrespondentAdd,
      onBulkCorrespondentRemove: handleBulkCorrespondentRemove,
      onBulkReanalyze: handleBulkSelectionReanalyze,
      folderOptions,
      onMoveDocumentsToFolder: moveDocumentsToFolder,
      documentLinks,
      ensureDownloadUrl,
      selectionValue,
      currentTenantId,
    }),
    [
      activeCorrespondentFilters,
      breadcrumbs,
      correspondents,
      currentFolderName,
      currentSubfolders,
      documents,
      documentsSortDirection,
      documentsSortField,
      documentsViewMode,
      searchQuery,
      documentLookup,
      draggedDocumentIds,
      draggedFolderId,
      focusedRowKey,
      folderClickHandlers,
      handleBulkCorrespondentAdd,
      handleBulkCorrespondentRemove,
      handleBulkSelectionReanalyze,
      handleBulkTagAddFromDetail,
      handleBulkTagRemoveFromDetail,
      handleDeleteSelection,
      handleDocumentDragEnd,
      handleDocumentDragStart,
      handleDocumentTagAttach,
      handleDocumentTagDetach,
      handleDocumentTitleUpdate,
      handleDocumentsSortDirectionToggle,
      handleDocumentsSortFieldChange,
      handleDocumentsViewModeChange,
      handleEntryPointerCore,
      handleFolderDragEnd,
      handleFolderDragStart,
      handleFolderRename,
      onDocumentActivate,
      moveDocumentsToFolder,
      openDocumentPreview,
      refreshCurrentFolder,
      searchLoading,
      searchResultIds,
      selectedFolder,
      selectFolder,
      tagLookupById,
      activeTagFilters,
      tags,
      ensureAssetUrl,
      getDocumentAsset,
      folderOptions,
      documentLinks,
      ensureDownloadUrl,
      selectionValue,
      currentTenantId,
    ],
  );
};

export default useDocumentsPanelProps;
