import { describe, it, expect, beforeEach } from 'vitest';
import { createStore } from 'jotai';
import {
	canvasSettingsAtom,
	eventMappingsAtom,
	activePresetIdAtom,
	isPanelOpenAtom,
	virtualizationEnabledAtom,
	hasUnsavedChangesAtom,
	setEventMappingAtom,
	resetSettingsAtom,
	togglePanelAtom,
	setPanelOpenAtom,
	toggleVirtualizationAtom,
} from '../core/settings-store';
import { CanvasEventType, BuiltInActionId, DEFAULT_MAPPINGS } from '../core/settings-types';

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

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

	it('default activePresetId is "default"', () => {
		expect(store.get(activePresetIdAtom)).toBe('default');
	});

	it('default panel is closed', () => {
		expect(store.get(isPanelOpenAtom)).toBe(false);
	});

	it('default virtualization is enabled', () => {
		expect(store.get(virtualizationEnabledAtom)).toBe(true);
	});

	it('default mappings match DEFAULT_MAPPINGS', () => {
		const mappings = store.get(eventMappingsAtom);
		expect(mappings[CanvasEventType.NodeDoubleClick]).toBe(DEFAULT_MAPPINGS[CanvasEventType.NodeDoubleClick]);
	});

	it('setEventMappingAtom updates a single mapping', () => {
		store.set(setEventMappingAtom, { event: CanvasEventType.NodeClick, actionId: 'custom-action' });
		const mappings = store.get(eventMappingsAtom);
		expect(mappings[CanvasEventType.NodeClick]).toBe('custom-action');
	});

	it('setEventMappingAtom clears activePresetId', () => {
		store.set(setEventMappingAtom, { event: CanvasEventType.NodeClick, actionId: 'custom-action' });
		expect(store.get(activePresetIdAtom)).toBeNull();
	});

	it('resetSettingsAtom restores default mappings and preset', () => {
		store.set(setEventMappingAtom, { event: CanvasEventType.NodeClick, actionId: 'custom-action' });
		store.set(resetSettingsAtom);
		expect(store.get(activePresetIdAtom)).toBe('default');
		const mappings = store.get(eventMappingsAtom);
		expect(mappings[CanvasEventType.NodeClick]).toBe(DEFAULT_MAPPINGS[CanvasEventType.NodeClick]);
	});

	it('togglePanelAtom toggles panel open', () => {
		expect(store.get(isPanelOpenAtom)).toBe(false);
		store.set(togglePanelAtom);
		expect(store.get(isPanelOpenAtom)).toBe(true);
		store.set(togglePanelAtom);
		expect(store.get(isPanelOpenAtom)).toBe(false);
	});

	it('setPanelOpenAtom sets panel directly', () => {
		store.set(setPanelOpenAtom, true);
		expect(store.get(isPanelOpenAtom)).toBe(true);
		store.set(setPanelOpenAtom, false);
		expect(store.get(isPanelOpenAtom)).toBe(false);
	});

	it('toggleVirtualizationAtom toggles', () => {
		expect(store.get(virtualizationEnabledAtom)).toBe(true);
		store.set(toggleVirtualizationAtom);
		expect(store.get(virtualizationEnabledAtom)).toBe(false);
	});

	it('hasUnsavedChangesAtom is false with default preset', () => {
		expect(store.get(hasUnsavedChangesAtom)).toBe(false);
	});

	it('hasUnsavedChangesAtom is true after modifying mappings', () => {
		store.set(setEventMappingAtom, { event: CanvasEventType.NodeClick, actionId: 'custom' });
		expect(store.get(hasUnsavedChangesAtom)).toBe(true);
	});
});
