/**
 * Tests for pluginToolbarStore — Phase 7 Task 7.7
 *
 * Tests: Initial state, registerItems, unregisterPlugin, setActivePlugin, updateItem
 */
import { describe, it, expect, beforeEach } from 'vitest';
import { usePluginToolbarStore, type ToolbarItem } from '../pluginToolbarStore';

const mockItems: ToolbarItem[] = [
  {
    id: 'btn-1',
    plugin: 'plugin-a',
    label: 'Button A',
    onClick: () => {},
    group: 'group-1',
    type: 'button',
  },
  {
    id: 'btn-2',
    plugin: 'plugin-a',
    label: 'Button B',
    onClick: () => {},
    group: 'group-1',
    type: 'button',
  },
];

const mockItemsB: ToolbarItem[] = [
  {
    id: 'btn-3',
    plugin: 'plugin-b',
    label: 'Button B1',
    onClick: () => {},
    type: 'button',
  },
];

describe('pluginToolbarStore', () => {
  beforeEach(() => {
    usePluginToolbarStore.setState({ items: [], activePlugin: null });
  });

  describe('initial state', () => {
    it('starts with empty items', () => {
      expect(usePluginToolbarStore.getState().items).toEqual([]);
    });

    it('starts with null activePlugin', () => {
      expect(usePluginToolbarStore.getState().activePlugin).toBeNull();
    });
  });

  describe('registerItems', () => {
    it('registers items for a plugin', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      expect(usePluginToolbarStore.getState().items).toHaveLength(2);
    });

    it('sets activePlugin when registering', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      expect(usePluginToolbarStore.getState().activePlugin).toBe('plugin-a');
    });

    it('replaces existing items for same plugin', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().registerItems('plugin-a', [mockItems[0]]);
      expect(usePluginToolbarStore.getState().items).toHaveLength(1);
    });

    it('adds items for different plugin without removing others', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().registerItems('plugin-b', mockItemsB);
      expect(usePluginToolbarStore.getState().items).toHaveLength(3);
    });
  });

  describe('unregisterPlugin', () => {
    it('removes all items for a plugin', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().registerItems('plugin-b', mockItemsB);
      usePluginToolbarStore.getState().unregisterPlugin('plugin-a');
      expect(usePluginToolbarStore.getState().items).toHaveLength(1);
      expect(usePluginToolbarStore.getState().items[0].plugin).toBe('plugin-b');
    });

    it('clears activePlugin when unregistering active plugin', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().unregisterPlugin('plugin-a');
      expect(usePluginToolbarStore.getState().activePlugin).toBeNull();
    });

    it('does not clear activePlugin when unregistering different plugin', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().registerItems('plugin-b', mockItemsB);
      usePluginToolbarStore.getState().unregisterPlugin('plugin-a');
      expect(usePluginToolbarStore.getState().activePlugin).toBe('plugin-b');
    });
  });

  describe('setActivePlugin', () => {
    it('sets active plugin', () => {
      usePluginToolbarStore.getState().setActivePlugin('plugin-a');
      expect(usePluginToolbarStore.getState().activePlugin).toBe('plugin-a');
    });

    it('sets active plugin to null', () => {
      usePluginToolbarStore.getState().setActivePlugin('plugin-a');
      usePluginToolbarStore.getState().setActivePlugin(null);
      expect(usePluginToolbarStore.getState().activePlugin).toBeNull();
    });
  });

  describe('updateItem', () => {
    it('updates a specific item by plugin and id', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().updateItem('plugin-a', 'btn-1', { disabled: true });
      expect(usePluginToolbarStore.getState().items[0].disabled).toBe(true);
    });

    it('does not affect items from other plugins', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().registerItems('plugin-b', mockItemsB);
      usePluginToolbarStore.getState().updateItem('plugin-a', 'btn-1', { disabled: true });
      const pluginBItem = usePluginToolbarStore.getState().items.find((i) => i.plugin === 'plugin-b');
      expect(pluginBItem?.disabled).toBeUndefined();
    });

    it('does nothing when item id does not match', () => {
      usePluginToolbarStore.getState().registerItems('plugin-a', mockItems);
      usePluginToolbarStore.getState().updateItem('plugin-a', 'nonexistent', { disabled: true });
      expect(usePluginToolbarStore.getState().items[0].disabled).toBeUndefined();
    });
  });
});
