import { describe, it, expect } from 'vitest';
import {
	createActionContext,
	createActionContextFromTouchEvent,
} from '../core/action-executor';
import { CanvasEventType } from '../core/settings-types';

describe('action-executor', () => {
	describe('createActionContext', () => {
		it('creates context with correct eventType', () => {
			const ctx = createActionContext(
				CanvasEventType.NodeClick,
				{ clientX: 100, clientY: 200 },
				{ x: 50, y: 75 },
			);
			expect(ctx.eventType).toBe(CanvasEventType.NodeClick);
		});

		it('maps screen position from clientX/Y', () => {
			const ctx = createActionContext(
				CanvasEventType.BackgroundClick,
				{ clientX: 300, clientY: 400 },
				{ x: 0, y: 0 },
			);
			expect(ctx.screenPosition).toEqual({ x: 300, y: 400 });
		});

		it('passes world position through', () => {
			const ctx = createActionContext(
				CanvasEventType.BackgroundClick,
				{ clientX: 0, clientY: 0 },
				{ x: 42, y: 99 },
			);
			expect(ctx.worldPosition).toEqual({ x: 42, y: 99 });
		});

		it('includes nodeId from options', () => {
			const ctx = createActionContext(
				CanvasEventType.NodeClick,
				{ clientX: 0, clientY: 0 },
				{ x: 0, y: 0 },
				{ nodeId: 'n1' },
			);
			expect(ctx.nodeId).toBe('n1');
		});

		it('defaults modifiers to false', () => {
			const ctx = createActionContext(
				CanvasEventType.BackgroundClick,
				{ clientX: 0, clientY: 0 },
				{ x: 0, y: 0 },
			);
			expect(ctx.modifiers).toEqual({ shift: false, ctrl: false, alt: false, meta: false });
		});

		it('nodeId is undefined when not provided', () => {
			const ctx = createActionContext(
				CanvasEventType.BackgroundClick,
				{ clientX: 0, clientY: 0 },
				{ x: 0, y: 0 },
			);
			expect(ctx.nodeId).toBeUndefined();
		});
	});

	describe('createActionContextFromTouchEvent', () => {
		it('maps touch position to screen position', () => {
			const ctx = createActionContextFromTouchEvent(
				CanvasEventType.BackgroundLongPress,
				{ clientX: 150, clientY: 250 },
				{ x: 10, y: 20 },
			);
			expect(ctx.screenPosition).toEqual({ x: 150, y: 250 });
			expect(ctx.worldPosition).toEqual({ x: 10, y: 20 });
		});

		it('touch modifiers default to false', () => {
			const ctx = createActionContextFromTouchEvent(
				CanvasEventType.NodeLongPress,
				{ clientX: 0, clientY: 0 },
				{ x: 0, y: 0 },
			);
			expect(ctx.modifiers.shift).toBe(false);
			expect(ctx.modifiers.meta).toBe(false);
		});
	});
});
