/**
 * Viewport Actions
 *
 * Registration functions for viewport-related built-in actions.
 */

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

export function registerViewportActions(): void {
	registerAction({
		id: BuiltInActionId.FitToView,
		label: 'Fit to View',
		description: 'Zoom and pan to fit this node in view',
		category: ActionCategory.Viewport,
		icon: 'maximize-2',
		requiresNode: true,
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.nodeId) {
				helpers.centerOnNode(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.FitAllToView,
		label: 'Fit All to View',
		description: 'Zoom and pan to fit all nodes in view',
		category: ActionCategory.Viewport,
		icon: 'maximize',
		isBuiltIn: true,
		handler: (_context: ActionContext, helpers: ActionHelpers) => {
			helpers.fitToBounds('graph');
		},
	});

	registerAction({
		id: BuiltInActionId.CenterOnNode,
		label: 'Center on Node',
		description: 'Center the viewport on this node',
		category: ActionCategory.Viewport,
		icon: 'crosshair',
		requiresNode: true,
		isBuiltIn: true,
		handler: (context: ActionContext, helpers: ActionHelpers) => {
			if (context.nodeId) {
				helpers.centerOnNode(context.nodeId);
			}
		},
	});

	registerAction({
		id: BuiltInActionId.ResetViewport,
		label: 'Reset Viewport',
		description: 'Reset zoom to 100% and center on origin',
		category: ActionCategory.Viewport,
		icon: 'home',
		isBuiltIn: true,
		handler: (_context: ActionContext, helpers: ActionHelpers) => {
			helpers.resetViewport();
		},
	});
}

export function registerHistoryActions(): void {
	registerAction({
		id: BuiltInActionId.Undo,
		label: 'Undo',
		description: 'Undo the last action',
		category: ActionCategory.History,
		icon: 'undo-2',
		isBuiltIn: true,
		handler: (_context: ActionContext, helpers: ActionHelpers) => {
			if (helpers.canUndo()) {
				helpers.undo();
			}
		},
	});

	registerAction({
		id: BuiltInActionId.Redo,
		label: 'Redo',
		description: 'Redo the last undone action',
		category: ActionCategory.History,
		icon: 'redo-2',
		isBuiltIn: true,
		handler: (_context: ActionContext, helpers: ActionHelpers) => {
			if (helpers.canRedo()) {
				helpers.redo();
			}
		},
	});

	registerAction({
		id: BuiltInActionId.ApplyForceLayout,
		label: 'Apply Force Layout',
		description: 'Automatically arrange nodes using force-directed layout',
		category: ActionCategory.Layout,
		icon: 'layout-grid',
		isBuiltIn: true,
		handler: async (_context: ActionContext, helpers: ActionHelpers) => {
			await helpers.applyForceLayout();
		},
	});
}
