/**
 * Action Registry
 *
 * Extensible registry for actions that can be triggered by canvas events.
 * Similar pattern to node-type-registry.ts - apps can register custom actions.
 */

import {
	type ActionDefinition,
	type ActionContext,
	type ActionHelpers,
	ActionCategory,
	BuiltInActionId,
} from './settings-types';

// =============================================================================
// Registry Storage
// =============================================================================

const actionRegistry = new Map<string, ActionDefinition>();

// =============================================================================
// Registry Functions
// =============================================================================

/**
 * Register a new action.
 * If an action with the same ID exists, it will be overwritten.
 */
export function registerAction(action: ActionDefinition): void {
	actionRegistry.set(action.id, action);
}

/**
 * Get an action by ID.
 */
export function getAction(id: string): ActionDefinition | undefined {
	return actionRegistry.get(id);
}

/**
 * Check if an action exists.
 */
export function hasAction(id: string): boolean {
	return actionRegistry.has(id);
}

/**
 * Get all registered actions.
 */
export function getAllActions(): ActionDefinition[] {
	return Array.from(actionRegistry.values());
}

/**
 * Get actions by category.
 */
export function getActionsByCategory(category: ActionCategory): ActionDefinition[] {
	return getAllActions().filter((action) => action.category === category);
}

/**
 * Unregister an action (mainly for custom actions).
 */
export function unregisterAction(id: string): boolean {
	return actionRegistry.delete(id);
}

/**
 * Clear all actions (mainly for testing).
 */
export function clearActions(): void {
	actionRegistry.clear();
}

// =============================================================================
// Built-in Actions — registered on first import
// =============================================================================

import { registerBuiltInActions } from './built-in-actions';
registerBuiltInActions();

// =============================================================================
// Export Action Utilities
// =============================================================================

/**
 * Get all action categories with their actions, for UI rendering.
 */
export function getActionsByCategories(): Array<{
	category: ActionCategory;
	label: string;
	actions: ActionDefinition[];
}> {
	const categoryLabels: Record<ActionCategory, string> = {
		[ActionCategory.None]: 'None',
		[ActionCategory.Selection]: 'Selection',
		[ActionCategory.Viewport]: 'Viewport',
		[ActionCategory.Node]: 'Node',
		[ActionCategory.Layout]: 'Layout',
		[ActionCategory.History]: 'History',
		[ActionCategory.Custom]: 'Custom',
	};

	const categoryOrder: ActionCategory[] = [
		ActionCategory.None,
		ActionCategory.Selection,
		ActionCategory.Viewport,
		ActionCategory.Node,
		ActionCategory.Layout,
		ActionCategory.History,
		ActionCategory.Custom,
	];

	return categoryOrder
		.map((category) => ({
			category,
			label: categoryLabels[category],
			actions: getActionsByCategory(category),
		}))
		.filter((group) => group.actions.length > 0);
}
