/**
 * @file useFolderSectionHotkeys.ts - Registers keyboard shortcuts for the FolderSection components.
 */

import { type RefObject } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import { assembleHotKeyString } from '../../utils/hotKeys.ts';
import { Key } from 'ts-key-enum';
import { PdfViewerComponent } from '@syncfusion/ej2-react-pdfviewer';
import {
  duplicateDocument,
  mergeDocumentWithPrevious,
  printDocument,
  rotateDocument,
  type DocumentLevelActionsProps,
} from '../../utils/documentLevelActions.ts';
import type { FileUploadDropzoneHandle } from '../../components/Processing/DetailsFolderPanel/sections/FolderSection/FileUploadDropzone.tsx';
import i18next from 'i18next';

/** Props required by {@link useFolderSectionHotkeys}. */
interface UseFolderSectionHotkeysProps {
  /** When true, document action hotkeys (duplicate, merge, rotate, etc.) are suppressed */
  documentActionButtonsDisabled: boolean;

  /** Ref to the file upload dropzone, used to programmatically open the file picker */
  uploadDocumentDropzoneRef: RefObject<FileUploadDropzoneHandle>;

  /** Props forwarded to document-level action utilities */
  documentLevelActionProps: DocumentLevelActionsProps;

  /** Reference to the Syncfusion PDF Viewer component for download support */
  pdfViewerRef: RefObject<PdfViewerComponent>;

  /** Callback to open the delete-document confirmation modal */
  showDeleteDocumentModal: () => void;
}

/**
 * Registers hotkeys used in the FolderSection components.
 */
export const useFolderSectionHotkeys = ({
  documentActionButtonsDisabled,
  uploadDocumentDropzoneRef,
  documentLevelActionProps,
  pdfViewerRef,
  showDeleteDocumentModal,
}: UseFolderSectionHotkeysProps) => {
  const modifiers = [Key.Control, Key.Shift];

  const tKey = 'hooks.hotkeys.folder_section';

  // Upload document hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['u']),
    () => {
      uploadDocumentDropzoneRef.current?.open();
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.upload_document')}`,
    }
  );

  // Duplicate document hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['d']),
    () => {
      if (!documentActionButtonsDisabled) {
        duplicateDocument(documentLevelActionProps);
      }
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.duplicate_document')}`,
    }
  );

  // Merge document hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['m']),
    () => {
      if (!documentActionButtonsDisabled) {
        mergeDocumentWithPrevious(documentLevelActionProps);
      }
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.merge_document')}`,
    }
  );

  // Rotate document hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, [Key.ArrowRight]),
    () => {
      if (!documentActionButtonsDisabled) {
        rotateDocument(documentLevelActionProps);
      }
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.rotate_document')}`,
    }
  );

  // Download document hotkey
  useHotkeys(
    assembleHotKeyString([Key.Control], ['d']),
    () => {
      if (!documentActionButtonsDisabled) {
        pdfViewerRef.current?.download();
      }
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.download_document')}`,
    }
  );

  // Print document hotkey
  useHotkeys(
    assembleHotKeyString([Key.Control], ['p']),
    () => {
      if (!documentActionButtonsDisabled) {
        printDocument(documentLevelActionProps);
      }
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.print_document')}`,
    }
  );

  // Delete document hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, [Key.Delete]),
    () => {
      if (!documentActionButtonsDisabled) {
        showDeleteDocumentModal();
      }
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.delete_document')}`,
    }
  );
};
