/**
 * Node & Selection Actions
 *
 * Registration functions for node-related and selection-related built-in actions.
 */

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

export function registerSelectionActions(): void {
	registerAction({
		id: BuiltInActionId.SelectNode,
		label: 'Select Node',
		description: 'Select this node (replacing current selection)',
		category: ActionCategory.Selection,
		icon: 'pointer',
		requiresNode: true,
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.nodeId) {
				helpers.selectNode(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.SelectEdge,
		label: 'Select Edge',
		description: 'Select this edge',
		category: ActionCategory.Selection,
		icon: 'git-commit',
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.edgeId) {
				helpers.selectEdge(context.edgeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.AddToSelection,
		label: 'Add to Selection',
		description: 'Add this node to the current selection',
		category: ActionCategory.Selection,
		icon: 'plus-square',
		requiresNode: true,
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.nodeId) {
				helpers.addToSelection(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.ClearSelection,
		label: 'Clear Selection',
		description: 'Deselect all nodes',
		category: ActionCategory.Selection,
		icon: 'x-square',
		isBuiltIn: true,
		handler: (_context: ActionContext, helpers: ActionHelpers) => {
			helpers.clearSelection();
		},
	});

	registerAction({
		id: BuiltInActionId.DeleteSelected,
		label: 'Delete Selected',
		description: 'Delete all selected nodes',
		category: ActionCategory.Selection,
		icon: 'trash-2',
		isBuiltIn: true,
		handler: async (_context: ActionContext, helpers: ActionHelpers) => {
			const selectedIds = helpers.getSelectedNodeIds();
			for (const nodeId of selectedIds) {
				await helpers.deleteNode(nodeId);
			}
		},
	});
}

export function registerNodeActions(): void {
	registerAction({
		id: BuiltInActionId.LockNode,
		label: 'Lock Node',
		description: 'Prevent this node from being moved',
		category: ActionCategory.Node,
		icon: 'lock',
		requiresNode: true,
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.nodeId) {
				helpers.lockNode(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.UnlockNode,
		label: 'Unlock Node',
		description: 'Allow this node to be moved',
		category: ActionCategory.Node,
		icon: 'unlock',
		requiresNode: true,
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.nodeId) {
				helpers.unlockNode(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.ToggleLock,
		label: 'Toggle Lock',
		description: 'Toggle whether this node can be moved',
		category: ActionCategory.Node,
		icon: 'lock',
		requiresNode: true,
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.nodeId) {
				helpers.toggleLock(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.OpenContextMenu,
		label: 'Open Context Menu',
		description: 'Show the context menu for this node',
		category: ActionCategory.Node,
		icon: 'more-vertical',
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (helpers.openContextMenu) {
				helpers.openContextMenu(context.screenPosition, context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.CreateNode,
		label: 'Create Node',
		description: 'Create a new node at this position',
		category: ActionCategory.Node,
		icon: 'plus',
		isBuiltIn: true,
		handler: async (context: ActionContext, helpers: ActionHelpers) => {
			if (helpers.createNode) {
				await helpers.createNode(context.worldPosition);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.SplitNode,
		label: 'Split Node',
		description: 'Split a node into two separate nodes',
		category: ActionCategory.Node,
		icon: 'split',
		isBuiltIn: true,
		handler: async (context: ActionContext, helpers: ActionHelpers) => {
			if (helpers.splitNode && context.nodeId) {
				await helpers.splitNode(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.GroupNodes,
		label: 'Group Nodes',
		description: 'Group selected nodes into a parent container',
		category: ActionCategory.Node,
		icon: 'group',
		isBuiltIn: true,
		handler: async (context: ActionContext, helpers: ActionHelpers) => {
			if (helpers.groupNodes) {
				await helpers.groupNodes(context.selectedNodeIds ?? helpers.getSelectedNodeIds());
			}
		},
	});

	registerAction({
		id: BuiltInActionId.MergeNodes,
		label: 'Merge Nodes',
		description: 'Merge selected nodes into one',
		category: ActionCategory.Node,
		icon: 'merge',
		isBuiltIn: true,
		handler: async (context: ActionContext, helpers: ActionHelpers) => {
			if (helpers.mergeNodes) {
				await helpers.mergeNodes(context.selectedNodeIds ?? helpers.getSelectedNodeIds());
			}
		},
	});
}
