import { describe, it, expect } from 'vitest';
import { findAlignmentGuides, type NodeRect } from '../core/snap-store';

describe('findAlignmentGuides', () => {
	const base: NodeRect = { x: 100, y: 100, width: 200, height: 100 };

	it('returns empty guides when no other nodes', () => {
		const result = findAlignmentGuides(base, []);
		expect(result.verticalGuides).toEqual([]);
		expect(result.horizontalGuides).toEqual([]);
	});

	it('detects left-edge alignment', () => {
		const other: NodeRect = { x: 100, y: 300, width: 150, height: 80 };
		const result = findAlignmentGuides(base, [other], 5);
		expect(result.verticalGuides).toContain(100);
	});

	it('detects center-X alignment', () => {
		// base center-X = 200, other center-X = 200
		const other: NodeRect = { x: 125, y: 300, width: 150, height: 80 };
		const result = findAlignmentGuides(base, [other], 5);
		expect(result.verticalGuides).toContain(200);
	});

	it('detects right-edge alignment', () => {
		// base right = 300, other right = 300
		const other: NodeRect = { x: 150, y: 300, width: 150, height: 80 };
		const result = findAlignmentGuides(base, [other], 5);
		expect(result.verticalGuides).toContain(300);
	});

	it('detects top-edge alignment', () => {
		const other: NodeRect = { x: 400, y: 100, width: 150, height: 80 };
		const result = findAlignmentGuides(base, [other], 5);
		expect(result.horizontalGuides).toContain(100);
	});

	it('detects center-Y alignment', () => {
		// base center-Y = 150, other center-Y = 150
		const other: NodeRect = { x: 400, y: 110, width: 150, height: 80 };
		const result = findAlignmentGuides(base, [other], 5);
		expect(result.horizontalGuides).toContain(150);
	});

	it('does not detect alignment outside tolerance', () => {
		// base: x=100 cx=200 right=300 / y=100 cy=150 bottom=200
		// other: x=350 cx=400 right=450 / y=350 cy=400 bottom=450
		// Every edge/center is ≥50px apart, well above 2px tolerance
		const other: NodeRect = { x: 350, y: 350, width: 100, height: 100 };
		const result = findAlignmentGuides(base, [other], 2);
		expect(result.verticalGuides).toEqual([]);
		expect(result.horizontalGuides).toEqual([]);
	});

	it('detects multiple guides from multiple nodes', () => {
		const others: NodeRect[] = [
			{ x: 100, y: 300, width: 150, height: 80 },   // left-left match
			{ x: 400, y: 100, width: 150, height: 80 },   // top-top match
		];
		const result = findAlignmentGuides(base, others, 5);
		expect(result.verticalGuides.length).toBeGreaterThanOrEqual(1);
		expect(result.horizontalGuides.length).toBeGreaterThanOrEqual(1);
	});
});
