/**
 * @file Imperative hook for activating/deactivating a shortcut scope.
 *
 * Use when a wrapper `<ShortcutScope>` component isn't desired.
 */

import { useCallback, useEffect, useRef } from 'react';
import { ShortcutManager } from '../../utils/shortcuts';

/**
 * @param {string} name - Scope name
 * @param {boolean} [active=true] - Whether the scope is initially active
 * @returns {{ enable: () => void, disable: () => void }}
 */
export function useShortcutScope(name, active = true) {
  const activeRef = useRef(active);

  useEffect(() => {
    const manager = ShortcutManager.getInstance();

    if (active) {
      manager.enableScope(name);
    } else {
      manager.disableScope(name);
    }

    activeRef.current = active;

    return () => {
      manager.disableScope(name);
    };
  }, [name, active]);

  const enable = useCallback(() => {
    ShortcutManager.getInstance().enableScope(name);
  }, [name]);

  const disable = useCallback(() => {
    ShortcutManager.getInstance().disableScope(name);
  }, [name]);

  return { enable, disable };
}
