import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { renderHook } from '@testing-library/react';
import React from 'react';
import { usePlugin, usePlugins } from '../hooks/usePlugin';
import { hasPlugin, clearPlugins } from '../core/plugin-registry';
import type { CanvasPlugin } from '../core/plugin-types';

const testPlugin: CanvasPlugin = {
	id: 'test-plugin',
	name: 'Test Plugin',
};

const pluginA: CanvasPlugin = { id: 'plugin-a', name: 'Plugin A' };
const pluginB: CanvasPlugin = { id: 'plugin-b', name: 'Plugin B' };

describe('usePlugin hook', () => {
	afterEach(() => {
		clearPlugins();
	});

	it('registers plugin on mount', () => {
		const { unmount } = renderHook(() => usePlugin(testPlugin));
		expect(hasPlugin('test-plugin')).toBe(true);
		unmount();
	});

	it('unregisters plugin on unmount', () => {
		const { unmount } = renderHook(() => usePlugin(testPlugin));
		expect(hasPlugin('test-plugin')).toBe(true);
		unmount();
		expect(hasPlugin('test-plugin')).toBe(false);
	});

	it('does not re-register on re-render', () => {
		const { rerender, unmount } = renderHook(() => usePlugin(testPlugin));
		rerender();
		expect(hasPlugin('test-plugin')).toBe(true);
		unmount();
	});
});

describe('usePlugins hook', () => {
	afterEach(() => {
		clearPlugins();
	});

	it('registers multiple plugins on mount', () => {
		const { unmount } = renderHook(() => usePlugins([pluginA, pluginB]));
		expect(hasPlugin('plugin-a')).toBe(true);
		expect(hasPlugin('plugin-b')).toBe(true);
		unmount();
	});

	it('unregisters all on unmount', () => {
		const { unmount } = renderHook(() => usePlugins([pluginA, pluginB]));
		unmount();
		expect(hasPlugin('plugin-a')).toBe(false);
		expect(hasPlugin('plugin-b')).toBe(false);
	});
});
