class HookSystem {
  filters = /* @__PURE__ */ new Map();
  actions = /* @__PURE__ */ new Map();
  hookStats = /* @__PURE__ */ new Map();
  /**
   * Add a filter hook
   * Filters allow plugins to modify data as it passes through the system
   */
  addFilter(hookName, callback, priority = 10) {
    if (!this.filters.has(hookName)) {
      this.filters.set(hookName, []);
    }
    const hooks2 = this.filters.get(hookName);
    const insertIndex = hooks2.findIndex((_, index) => {
      return index >= priority;
    });
    if (insertIndex === -1) {
      hooks2.push(callback);
    } else {
      hooks2.splice(insertIndex, 0, callback);
    }
    console.log(`[Hooks] Added filter hook: ${hookName} (priority: ${priority})`);
  }
  /**
   * Apply filter hooks to data
   */
  async applyFilters(hookName, data, ...args) {
    const startTime = performance.now();
    const hooks2 = this.filters.get(hookName) || [];
    let result = data;
    console.log(`[Hooks] Applying ${hooks2.length} filter hooks for: ${hookName}`);
    for (const callback of hooks2) {
      try {
        const hookResult = await callback(result, ...args);
        if (hookResult !== void 0) {
          result = hookResult;
        }
      } catch (error) {
        console.error(`[Hooks] Error in filter hook ${hookName}:`, error);
      }
    }
    this.recordHookStats(hookName, startTime);
    return result;
  }
  /**
   * Add an action hook
   * Actions allow plugins to execute code at specific points
   */
  addAction(hookName, callback, priority = 10) {
    if (!this.actions.has(hookName)) {
      this.actions.set(hookName, []);
    }
    const hooks2 = this.actions.get(hookName);
    const insertIndex = hooks2.findIndex((_, index) => {
      return index >= priority;
    });
    if (insertIndex === -1) {
      hooks2.push(callback);
    } else {
      hooks2.splice(insertIndex, 0, callback);
    }
    console.log(`[Hooks] Added action hook: ${hookName} (priority: ${priority})`);
  }
  /**
   * Execute action hooks
   */
  async doAction(hookName, data, ...args) {
    const startTime = performance.now();
    const hooks2 = this.actions.get(hookName) || [];
    console.log(`[Hooks] Executing ${hooks2.length} action hooks for: ${hookName}`);
    const promises = hooks2.map(async (callback) => {
      try {
        await callback(data, ...args);
      } catch (error) {
        console.error(`[Hooks] Error in action hook ${hookName}:`, error);
      }
    });
    await Promise.allSettled(promises);
    this.recordHookStats(hookName, startTime);
  }
  /**
   * Emit an event (alias for doAction for semantic clarity)
   */
  async emit(eventName, data, ...args) {
    return this.doAction(eventName, data, ...args);
  }
  /**
   * Remove a filter hook
   */
  removeFilter(hookName, callback) {
    const hooks2 = this.filters.get(hookName);
    if (!hooks2) return false;
    const index = hooks2.indexOf(callback);
    if (index === -1) return false;
    hooks2.splice(index, 1);
    if (hooks2.length === 0) {
      this.filters.delete(hookName);
    }
    console.log(`[Hooks] Removed filter hook: ${hookName}`);
    return true;
  }
  /**
   * Remove an action hook
   */
  removeAction(hookName, callback) {
    const hooks2 = this.actions.get(hookName);
    if (!hooks2) return false;
    const index = hooks2.indexOf(callback);
    if (index === -1) return false;
    hooks2.splice(index, 1);
    if (hooks2.length === 0) {
      this.actions.delete(hookName);
    }
    console.log(`[Hooks] Removed action hook: ${hookName}`);
    return true;
  }
  /**
   * Check if a hook has any callbacks
   */
  hasHook(hookName) {
    return this.filters.has(hookName) && this.filters.get(hookName).length > 0 || this.actions.has(hookName) && this.actions.get(hookName).length > 0;
  }
  /**
   * Get all registered hooks
   */
  getAllHooks() {
    return {
      filters: Array.from(this.filters.keys()),
      actions: Array.from(this.actions.keys())
    };
  }
  /**
   * Record hook execution statistics
   */
  recordHookStats(hookName, startTime) {
    const endTime = performance.now();
    const duration = endTime - startTime;
    if (!this.hookStats.has(hookName)) {
      this.hookStats.set(hookName, { calls: 0, totalTime: 0 });
    }
    const stats = this.hookStats.get(hookName);
    stats.calls++;
    stats.totalTime += duration;
  }
  /**
   * Get hook execution statistics
   */
  getHookStats() {
    const result = {};
    for (const [hookName, stats] of this.hookStats) {
      result[hookName] = {
        calls: stats.calls,
        totalTime: stats.totalTime,
        avgTime: stats.calls > 0 ? stats.totalTime / stats.calls : 0
      };
    }
    return result;
  }
  /**
   * Clear all hooks (useful for testing)
   */
  clear() {
    this.filters.clear();
    this.actions.clear();
    this.hookStats.clear();
    console.log("[Hooks] Cleared all hooks");
  }
  /**
   * Get hook count
   */
  getHookCount() {
    const filterCount = Array.from(this.filters.values()).reduce((sum, hooks2) => sum + hooks2.length, 0);
    const actionCount = Array.from(this.actions.values()).reduce((sum, hooks2) => sum + hooks2.length, 0);
    return { filters: filterCount, actions: actionCount };
  }
}
const HOOK_SYSTEM_KEY = "__HOOK_SYSTEM__";
function createHookSystem() {
  if (!globalThis[HOOK_SYSTEM_KEY]) {
    globalThis[HOOK_SYSTEM_KEY] = new HookSystem();
  }
  return globalThis[HOOK_SYSTEM_KEY];
}
function getGlobalHooks() {
  return createHookSystem();
}
const hooks = createHookSystem();
const addFilter = (hookName, callback, priority = 10) => hooks.addFilter(hookName, callback, priority);
const applyFilters = (hookName, data, ...args) => hooks.applyFilters(hookName, data, ...args);
const addAction = (hookName, callback, priority = 10) => hooks.addAction(hookName, callback, priority);
const doAction = (hookName, data, ...args) => hooks.doAction(hookName, data, ...args);
const emit = (eventName, data, ...args) => hooks.emit(eventName, data, ...args);
const removeFilter = (hookName, callback) => hooks.removeFilter(hookName, callback);
const removeAction = (hookName, callback) => hooks.removeAction(hookName, callback);
const hasHook = (hookName) => hooks.hasHook(hookName);
const getAllHooks = () => hooks.getAllHooks();
const getHookStats = () => hooks.getHookStats();
export {
  HookSystem,
  addAction,
  addFilter,
  applyFilters,
  createHookSystem,
  doAction,
  emit,
  getAllHooks,
  getGlobalHooks,
  getHookStats,
  hasHook,
  removeAction,
  removeFilter
};
