import type { Direction, Maybe } from '../types';
import { isElement } from './domUtils';

// https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
const nonInputKeys = new Set([
  // Special keys
  'Unidentified',
  // Modifier keys
  'Alt',
  'AltGraph',
  'CapsLock',
  'Control',
  'Fn',
  'FnLock',
  'Meta',
  'NumLock',
  'ScrollLock',
  'Shift',
  // Whitespace keys
  'Tab',
  // Navigation keys
  'ArrowDown',
  'ArrowLeft',
  'ArrowRight',
  'ArrowUp',
  'End',
  'Home',
  'PageDown',
  'PageUp',
  // Editing
  'Insert',
  // UI keys
  'ContextMenu',
  'Escape',
  'Pause',
  'Play',
  // Device keys
  'PrintScreen',
  // Function keys
  'F1',
  // 'F2', /!\ specifically allowed, do not edit
  'F3',
  'F4',
  'F5',
  'F6',
  'F7',
  'F8',
  'F9',
  'F10',
  'F11',
  'F12'
]);

export function isCtrlKeyHeldDown(e: React.KeyboardEvent): boolean {
  return (e.ctrlKey || e.metaKey) && e.key !== 'Control';
}

// event.key may differ by keyboard input language, so we use event.keyCode instead.
// event.nativeEvent.code cannot be used either, as it would break copy/paste for the Dvorak layout.
const vKey = 86;

export function isDefaultCellInput(
  event: React.KeyboardEvent<HTMLDivElement>,
  isUserHandlingPaste: boolean
): boolean {
  // oxlint-disable-next-line typescript/no-deprecated
  if (isCtrlKeyHeldDown(event) && (event.keyCode !== vKey || isUserHandlingPaste)) return false;
  return !nonInputKeys.has(event.key);
}

/**
 * By default, the following navigation keys are enabled while an editor is open, under specific conditions:
 * - Tab:
 *   - The editor must be an <input>, a <textarea>, or a <select> element.
 *   - The editor element must be the only <input>, <textarea>, or <select> element in the editor container.
 */
export function onEditorNavigation({ key, target }: React.KeyboardEvent<HTMLDivElement>): boolean {
  if (key === 'Tab' && isElement(target) && target.matches('input, textarea, select')) {
    return (
      target.closest('.rdg-editor-container')?.querySelectorAll('input, textarea, select')
        .length === 1
    );
  }
  return false;
}

export function getLeftRightKey(direction: Maybe<Direction>) {
  const isRtl = direction === 'rtl';

  return {
    leftKey: isRtl ? 'ArrowRight' : 'ArrowLeft',
    rightKey: isRtl ? 'ArrowLeft' : 'ArrowRight'
  } as const;
}
