/**
 * @file useDetailsSectionHotkeys.ts - Registers keyboard shortcuts for the DetailsSection component.
 */

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

/** Props required by {@link useDetailsSectionHotkeys}. */
interface UseDetailsSectionHotkeysProps {
  /** Props required by {@link useDetailsSectionHotkeys}. */
  closeButtonRef: RefObject<HTMLButtonElement>;
}

/**
 * Registers hotkeys used in the DetailsSection component.
 */
export const useDetailsSectionHotkeys = ({
  closeButtonRef,
}: UseDetailsSectionHotkeysProps) => {
  const modifiers = [Key.Control];

  const tKey = 'hooks.hotkeys.details_section';

  // Close work item hotkey
  useHotkeys(
    assembleHotKeyString(modifiers, ['q']),
    () => {
      closeButtonRef.current?.click();
    },
    {
      preventDefault: true,
      description: `[nav]${i18next.t(tKey + '.close_work_item')}`,
    }
  );
};
