{"version":3,"file":"handle-control-key-down.mjs","names":[],"sources":["../../../src/utils/handle-control-key-down/handle-control-key-down.ts"],"sourcesContent":["import { RefObject } from 'react';\n\ntype ControlsRef = RefObject<HTMLButtonElement[][][]>;\ntype Direction = 'up' | 'down' | 'left' | 'right';\ntype Edge = 'previous' | 'next';\n\nexport interface ControlEdgeCrossPayload {\n  /** Grid edge that was crossed */\n  edge: Edge;\n\n  /** Arrow direction that caused the crossing */\n  direction: Direction;\n\n  /** Cell index of the control that had focus */\n  cellIndex: number;\n}\n\ntype NextIndexInput = Omit<ShiftFocusInput, 'controlsRef'>;\n\nfunction getNextIndex({ direction, levelIndex, rowIndex, cellIndex, size }: NextIndexInput) {\n  switch (direction) {\n    case 'up':\n      if (levelIndex === 0 && rowIndex === 0) {\n        return null;\n      }\n      if (rowIndex === 0) {\n        return {\n          levelIndex: levelIndex - 1,\n          rowIndex:\n            cellIndex <= size[levelIndex - 1][size[levelIndex - 1].length - 1] - 1\n              ? size[levelIndex - 1].length - 1\n              : size[levelIndex - 1].length - 2,\n          cellIndex,\n        };\n      }\n      return {\n        levelIndex,\n        rowIndex: rowIndex - 1,\n        cellIndex,\n      };\n\n    case 'down':\n      if (rowIndex === size[levelIndex].length - 1) {\n        return {\n          levelIndex: levelIndex + 1,\n          rowIndex: 0,\n          cellIndex,\n        };\n      }\n      if (\n        rowIndex === size[levelIndex].length - 2 &&\n        cellIndex >= size[levelIndex][size[levelIndex].length - 1]\n      ) {\n        return {\n          levelIndex: levelIndex + 1,\n          rowIndex: 0,\n          cellIndex,\n        };\n      }\n      return {\n        levelIndex,\n        rowIndex: rowIndex + 1,\n        cellIndex,\n      };\n\n    case 'left':\n      if (levelIndex === 0 && rowIndex === 0 && cellIndex === 0) {\n        return null;\n      }\n      if (rowIndex === 0 && cellIndex === 0) {\n        return {\n          levelIndex: levelIndex - 1,\n          rowIndex: size[levelIndex - 1].length - 1,\n          cellIndex: size[levelIndex - 1][size[levelIndex - 1].length - 1] - 1,\n        };\n      }\n      if (cellIndex === 0) {\n        return {\n          levelIndex,\n          rowIndex: rowIndex - 1,\n          cellIndex: size[levelIndex][rowIndex - 1] - 1,\n        };\n      }\n      return {\n        levelIndex,\n        rowIndex,\n        cellIndex: cellIndex - 1,\n      };\n\n    case 'right':\n      if (\n        rowIndex === size[levelIndex].length - 1 &&\n        cellIndex === size[levelIndex][rowIndex] - 1\n      ) {\n        return {\n          levelIndex: levelIndex + 1,\n          rowIndex: 0,\n          cellIndex: 0,\n        };\n      }\n      if (cellIndex === size[levelIndex][rowIndex] - 1) {\n        return {\n          levelIndex,\n          rowIndex: rowIndex + 1,\n          cellIndex: 0,\n        };\n      }\n      return {\n        levelIndex,\n        rowIndex,\n        cellIndex: cellIndex + 1,\n      };\n\n    default:\n      return { levelIndex, rowIndex, cellIndex };\n  }\n}\n\ninterface ShiftFocusInput {\n  controlsRef: ControlsRef;\n  direction: Direction;\n  levelIndex: number;\n  rowIndex: number;\n  cellIndex: number;\n  size: number[][];\n}\n\nfunction isControlFocusable(control: HTMLButtonElement) {\n  return (\n    !control.disabled &&\n    !control.getAttribute('data-hidden') &&\n    !control.getAttribute('data-outside')\n  );\n}\n\nfunction focusOnNextFocusableControl({\n  controlsRef,\n  direction,\n  levelIndex,\n  rowIndex,\n  cellIndex,\n  size,\n}: ShiftFocusInput): Edge | null {\n  const nextIndex = getNextIndex({ direction, size, rowIndex, cellIndex, levelIndex });\n\n  if (!nextIndex) {\n    return 'previous';\n  }\n\n  if (nextIndex.levelIndex >= size.length) {\n    return 'next';\n  }\n\n  const controlToFocus =\n    controlsRef.current?.[nextIndex.levelIndex]?.[nextIndex.rowIndex]?.[nextIndex.cellIndex];\n\n  if (!controlToFocus) {\n    return null;\n  }\n\n  if (!isControlFocusable(controlToFocus)) {\n    return focusOnNextFocusableControl({\n      controlsRef,\n      direction,\n      levelIndex: nextIndex.levelIndex,\n      cellIndex: nextIndex.cellIndex,\n      rowIndex: nextIndex.rowIndex,\n      size,\n    });\n  }\n\n  controlToFocus.focus();\n  return null;\n}\n\ninterface FocusControlInput {\n  controlsRef: ControlsRef;\n  direction: Direction;\n  levelIndex: number;\n  rowIndex: number;\n  cellIndex: number;\n}\n\n/** Focuses control at the given position or the next focusable control in the given direction */\nexport function focusControl({\n  controlsRef,\n  direction,\n  levelIndex,\n  rowIndex,\n  cellIndex,\n}: FocusControlInput) {\n  const size = getControlsSize(controlsRef);\n  const control = controlsRef.current?.[levelIndex]?.[rowIndex]?.[cellIndex];\n\n  if (!size || !control) {\n    return;\n  }\n\n  if (isControlFocusable(control)) {\n    control.focus();\n    return;\n  }\n\n  focusOnNextFocusableControl({ controlsRef, direction, levelIndex, rowIndex, cellIndex, size });\n}\n\nfunction getDirection(key: KeyboardEvent['key'], dir: 'ltr' | 'rtl'): Direction {\n  switch (key) {\n    case 'ArrowDown':\n      return 'down';\n    case 'ArrowUp':\n      return 'up';\n    case 'ArrowRight':\n      return dir === 'rtl' ? 'left' : 'right';\n    case 'ArrowLeft':\n      return dir === 'rtl' ? 'right' : 'left';\n    default:\n      return null!;\n  }\n}\n\nfunction getControlsSize(controlsRef: ControlsRef) {\n  return controlsRef.current?.map((column) => column.map((row) => row.length));\n}\n\ninterface HandleControlKeyDownInput {\n  controlsRef: ControlsRef;\n  levelIndex: number;\n  rowIndex: number;\n  cellIndex: number;\n  event: React.KeyboardEvent<HTMLButtonElement>;\n\n  /** Called when arrow navigation runs off the first or the last grid */\n  onEdgeCross?: (payload: ControlEdgeCrossPayload) => void;\n  /** Text direction, `ArrowLeft` and `ArrowRight` are mirrored in `rtl`, `'ltr'` by default */\n  dir?: 'ltr' | 'rtl';\n}\n\nexport function handleControlKeyDown({\n  controlsRef,\n  levelIndex,\n  rowIndex,\n  cellIndex,\n  event,\n  onEdgeCross,\n  dir = 'ltr',\n}: HandleControlKeyDownInput) {\n  const direction = getDirection(event.key, dir);\n\n  if (direction) {\n    event.preventDefault();\n\n    const size = getControlsSize(controlsRef)!;\n\n    const edge = focusOnNextFocusableControl({\n      controlsRef,\n      direction,\n      levelIndex,\n      rowIndex,\n      cellIndex,\n      size,\n    });\n\n    if (edge) {\n      onEdgeCross?.({ edge, direction, cellIndex });\n    }\n  }\n}\n"],"mappings":";;AAmBA,SAAS,aAAa,EAAE,WAAW,YAAY,UAAU,WAAW,QAAwB;CAC1F,QAAQ,WAAR;EACE,KAAK;GACH,IAAI,eAAe,KAAK,aAAa,GACnC,OAAO;GAET,IAAI,aAAa,GACf,OAAO;IACL,YAAY,aAAa;IACzB,UACE,aAAa,KAAK,aAAa,EAAE,CAAC,KAAK,aAAa,EAAE,CAAC,SAAS,KAAK,IACjE,KAAK,aAAa,EAAE,CAAC,SAAS,IAC9B,KAAK,aAAa,EAAE,CAAC,SAAS;IACpC;GACF;GAEF,OAAO;IACL;IACA,UAAU,WAAW;IACrB;GACF;EAEF,KAAK;GACH,IAAI,aAAa,KAAK,WAAW,CAAC,SAAS,GACzC,OAAO;IACL,YAAY,aAAa;IACzB,UAAU;IACV;GACF;GAEF,IACE,aAAa,KAAK,WAAW,CAAC,SAAS,KACvC,aAAa,KAAK,WAAW,CAAC,KAAK,WAAW,CAAC,SAAS,IAExD,OAAO;IACL,YAAY,aAAa;IACzB,UAAU;IACV;GACF;GAEF,OAAO;IACL;IACA,UAAU,WAAW;IACrB;GACF;EAEF,KAAK;GACH,IAAI,eAAe,KAAK,aAAa,KAAK,cAAc,GACtD,OAAO;GAET,IAAI,aAAa,KAAK,cAAc,GAClC,OAAO;IACL,YAAY,aAAa;IACzB,UAAU,KAAK,aAAa,EAAE,CAAC,SAAS;IACxC,WAAW,KAAK,aAAa,EAAE,CAAC,KAAK,aAAa,EAAE,CAAC,SAAS,KAAK;GACrE;GAEF,IAAI,cAAc,GAChB,OAAO;IACL;IACA,UAAU,WAAW;IACrB,WAAW,KAAK,WAAW,CAAC,WAAW,KAAK;GAC9C;GAEF,OAAO;IACL;IACA;IACA,WAAW,YAAY;GACzB;EAEF,KAAK;GACH,IACE,aAAa,KAAK,WAAW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