/**
 * @file useScanInsertHotkeys.ts - Registers keyboard shortcuts for the ScanInsertModal component that trigger scan insertion functionality through the RWP Companion application.
 * These hotkeys are only enabled when running on Windows and the `ENABLE_RWP_COMPANION` feature flag is set to `'true'` in the application config.
 */

import { useHotkeys } from 'react-hotkeys-hook';
import { assembleHotKeyString } from '../../utils/hotKeys.ts';
import { Key } from 'ts-key-enum';
import { scanInsertDropdownExists } from '../../utils/pdfViewerInjections.tsx';
import i18next from 'i18next';

/** Props required by {@link useScanInsertHotkeys}. */
interface UseScanInsertHotkeysProps {
  /** Sets the scan-insert modal into automatic (hotkey-triggered) mode */
  setAuto: (auto: boolean) => void;

  /** Controls the visibility of the scan-insert modal */
  setShow: (show: boolean) => void;

  /** Whether the current platform is Windows (required for RWP Companion support) */
  isWindows: boolean;
}

/**
 * Registers hotkeys used in the ScanInsertModal component.
 */
export const useScanInsertHotkeys = ({
  setAuto,
  setShow,
  isWindows,
}: UseScanInsertHotkeysProps) => {
  const tKey = 'hooks.hotkeys.scan_insert';

  // Scan insert hotkey
  useHotkeys(
    assembleHotKeyString([Key.Control, Key.Shift], [Key.Insert]),
    () => {
      // Only trigger if the scan-insert dropdown has been injected into the toolbar
      if (scanInsertDropdownExists()) {
        setAuto(true);
        setShow(true);
      }
    },
    {
      preventDefault: true,
      description: `[doc]${i18next.t(tKey + '.after')}`,
      enabled:
        // Only activate when both the feature flag and platform requirement are met
        window.__APP_CONFIG__?.ENABLE_RWP_COMPANION === 'true' && isWindows,
    }
  );
};
