/**
 * @file useImageCleanupHotkeys.ts - Registers keyboard shortcuts for the ImageCleanupModal component that trigger image cleanup operations 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 type { AutomaticOperation } from '../../types/companionTool.ts';
import i18next from 'i18next';

/** Props required by {@link useImageCleanupHotkeys}. */
interface UseImageCleanupHotkeysProps {
  /** Whether the current platform is Windows (required for RWP Companion support) */
  isWindows: boolean;

  /** Callback to trigger a specific automatic image cleanup operation */
  activateAutomaticOperation: (operation: AutomaticOperation) => void;
}

/**
 * Registers hotkeys used in the ImageCleanupModal component.
 */
export const useImageCleanupHotkeys = ({
  isWindows,
  activateAutomaticOperation,
}: UseImageCleanupHotkeysProps) => {
  const modifiers = [Key.Control, Key.Alt];
  // Only activate hotkeys when both the feature flag and platform requirement are met
  const enabled =
    window.__APP_CONFIG__?.ENABLE_RWP_COMPANION === 'true' && isWindows;

  const tKey = 'hooks.hotkeys.image_cleanup';

  // Border crop hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['1']),
    () => activateAutomaticOperation('border_crop'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.border_crop')}`,
      enabled,
    }
  );

  // Line removal hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['2']),
    () => activateAutomaticOperation('line_removal'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.line_removal')}`,
      enabled,
    }
  );

  // Deskew hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['3']),
    () => activateAutomaticOperation('deskew'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.deskew')}`,
      enabled,
    }
  );

  // Shear hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['4']),
    () => activateAutomaticOperation('shear'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.shear')}`,
      enabled,
    }
  );

  // Erode hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['5']),
    () => activateAutomaticOperation('erode'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.erode')}`,
      enabled,
    }
  );

  // Dilate hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['6']),
    () => activateAutomaticOperation('dilate'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.dilate')}`,
      enabled,
    }
  );

  // Blob removal hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['7']),
    () => activateAutomaticOperation('blob_removal'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.blob_removal')}`,
      enabled,
    }
  );

  // Zoom smooth hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['8']),
    () => activateAutomaticOperation('zoom_smooth'),
    {
      preventDefault: true,
      description: `[page]${i18next.t(tKey + '.zoom_smooth')}`,
      enabled,
    }
  );
};
