import { describe, it, expect, beforeEach } from 'vitest';
import { createStore } from 'jotai';
import {
	zoomAtom,
	panAtom,
	viewportRectAtom,
	screenToWorldAtom,
	worldToScreenAtom,
	resetViewportAtom,
	ZOOM_TRANSITION_THRESHOLD,
	ZOOM_EXIT_THRESHOLD,
} from '../core/viewport-store';

describe('viewport-store actions', () => {
	let store: ReturnType<typeof createStore>;

	beforeEach(() => {
		store = createStore();
	});

	it('zoom defaults to 1', () => {
		expect(store.get(zoomAtom)).toBe(1);
	});

	it('pan defaults to { x: 0, y: 0 }', () => {
		expect(store.get(panAtom)).toEqual({ x: 0, y: 0 });
	});

	it('zoom can be set', () => {
		store.set(zoomAtom, 2.5);
		expect(store.get(zoomAtom)).toBe(2.5);
	});

	it('pan can be set', () => {
		store.set(panAtom, { x: 100, y: -50 });
		expect(store.get(panAtom)).toEqual({ x: 100, y: -50 });
	});

	it('resetViewportAtom resets zoom and pan', () => {
		store.set(zoomAtom, 3);
		store.set(panAtom, { x: 500, y: 300 });
		store.set(resetViewportAtom);
		expect(store.get(zoomAtom)).toBe(1);
		expect(store.get(panAtom)).toEqual({ x: 0, y: 0 });
	});

	it('screenToWorldAtom returns identity without viewport rect', () => {
		const screenToWorld = store.get(screenToWorldAtom);
		const result = screenToWorld(100, 200);
		expect(result).toEqual({ x: 100, y: 200 });
	});

	it('worldToScreenAtom returns identity without viewport rect', () => {
		const worldToScreen = store.get(worldToScreenAtom);
		const result = worldToScreen(100, 200);
		expect(result).toEqual({ x: 100, y: 200 });
	});

	it('ZOOM_TRANSITION_THRESHOLD > ZOOM_EXIT_THRESHOLD', () => {
		expect(ZOOM_TRANSITION_THRESHOLD).toBeGreaterThan(ZOOM_EXIT_THRESHOLD);
	});
});
