/**
 * Framework hook bus — the JS counterpart of WordPress PHP hooks, powering
 * extensibility across cliq-ui (admin builder, renderer, fields) AND the public
 * frontend. Built on @wordpress/hooks.
 *
 * It is a SINGLETON shared across every bundle on the page (the framework admin
 * bundle, the host's admin-extension bundle, and the host's frontend bundle each
 * import their own copy of this module via the @cliqui alias). The first to load
 * creates the instance on `window.cliqHooks`; the rest reuse it — so a filter
 * added by the host bundle is seen by callbacks invoked inside the framework
 * bundle. Also mirrored to PHP-style helpers so usage reads the same everywhere:
 *
 *   import { addFilter, applyFilters, addAction, doAction } from '@cliqui/shared/hooks';
 *   addFilter( 'cliqui.token_value', 'my-plugin/distance', ( value, ctx ) => … );
 *   const out = applyFilters( 'cliqui.token_value', value, ctx );
 *
 * Convention: filter/action names are namespaced `cliqui.*` (snake_case to mirror
 * the PHP filter names), callback namespaces are `vendor/feature`.
 */
import { createHooks } from '@wordpress/hooks';

const instance =
	( typeof window !== 'undefined' && window.cliqHooks ) || createHooks();

if ( typeof window !== 'undefined' && ! window.cliqHooks ) {
	window.cliqHooks = instance;
}

// Thin wrappers (preserve the instance binding when imported as bare functions).
export const addFilter = ( ...a ) => instance.addFilter( ...a );
export const removeFilter = ( ...a ) => instance.removeFilter( ...a );
export const applyFilters = ( ...a ) => instance.applyFilters( ...a );
export const addAction = ( ...a ) => instance.addAction( ...a );
export const removeAction = ( ...a ) => instance.removeAction( ...a );
export const doAction = ( ...a ) => instance.doAction( ...a );
export const hasFilter = ( ...a ) => instance.hasFilter( ...a );
export const hasAction = ( ...a ) => instance.hasAction( ...a );

export default instance;
