import { eventBus } from '../lib/eventBus.js';
import { saveState } from './SaveState.js';

class HiddenHookTracker {
  constructor() {
    this._discovered = new Set();
  }

  initialize(state) {
    const saved = state?.hidden_hooks_discovered;
    this._discovered = new Set(Array.isArray(saved) ? saved : []);
  }

  discover(hookId) {
    if (this._discovered.has(hookId)) return;
    this._discovered.add(hookId);
    this._persist();
    eventBus.emit('hidden_hook:discovered', { hookId });
  }

  isDiscovered(hookId) {
    return this._discovered.has(hookId);
  }

  getDiscovered() {
    return [...this._discovered].sort();
  }

  _persist() {
    saveState.set({ hidden_hooks_discovered: this.getDiscovered() });
  }
}

export const hiddenHookTracker = new HiddenHookTracker();
