import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { getSidebarState, getShortcutsState, setSubItems } from './sidebar-state.svelte';
import { setContext } from 'svelte';
vi.mock('svelte', async () => {
    const actual = await vi.importActual('svelte');
    return {
        ...actual,
        setContext: vi.fn(),
    };
});
describe('getSidebarState', () => {
    let sidebar;
    let element;
    let cleanup;
    beforeEach(() => {
        sidebar = document.createElement('m-sidebar');
        element = document.createElement('div');
        document.body.appendChild(sidebar);
    });
    afterEach(() => {
        cleanup?.();
        sidebar.remove();
        element.remove();
    });
    it('sets expanded state based on attribute', () => {
        sidebar.setAttribute('expanded', '');
        const setExpanded = vi.fn();
        cleanup = getSidebarState({ setExpanded })(element);
        expect(setExpanded).toHaveBeenCalledWith(true);
    });
    it('updates expanded state on event', () => {
        const setExpanded = vi.fn();
        cleanup = getSidebarState({ setExpanded })(element);
        setExpanded.mockClear();
        document.dispatchEvent(new CustomEvent('update:expanded', { detail: false }));
        expect(setExpanded).toHaveBeenCalledWith(false);
    });
    it('removes event listener on cleanup', () => {
        const setExpanded = vi.fn();
        cleanup = getSidebarState({ setExpanded })(element);
        setExpanded.mockClear();
        cleanup?.();
        document.dispatchEvent(new CustomEvent('update:expanded', { detail: true }));
        expect(setExpanded).not.toHaveBeenCalled();
    });
});
describe('getShortcutsState', () => {
    let shortcuts;
    let element;
    let cleanup;
    beforeEach(() => {
        shortcuts = document.createElement('m-sortcuts');
        element = document.createElement('div');
        document.body.appendChild(shortcuts);
    });
    afterEach(() => {
        cleanup?.();
        shortcuts.remove();
        element.remove();
    });
    it('sets stacked state based on attribute', () => {
        shortcuts.setAttribute('stacked', '');
        const setState = vi.fn();
        cleanup = getShortcutsState({ setState })(element);
        expect(setState).toHaveBeenCalledWith(true);
    });
    it('updates stacked state on event', () => {
        const setState = vi.fn();
        cleanup = getShortcutsState({ setState })(element);
        setState.mockClear();
        document.dispatchEvent(new CustomEvent('update:stacked', { detail: true }));
        expect(setState).toHaveBeenCalledWith(true);
    });
    it('removes event listener on cleanup', () => {
        const setState = vi.fn();
        cleanup = getShortcutsState({ setState })(element);
        setState.mockClear();
        cleanup?.();
        document.dispatchEvent(new CustomEvent('update:stacked', { detail: false }));
        expect(setState).not.toHaveBeenCalled();
    });
});
describe('setSubItems', () => {
    let element;
    beforeEach(() => {
        document.body.innerHTML = '';
        element = document.createElement('div');
    });
    afterEach(() => {
        element.remove();
    });
    it('calls setContext with SUB_ITEM', () => {
        setSubItems('M-SIDEBAR')(element);
        expect(setContext).toHaveBeenCalledWith('SUB_ITEM', true);
    });
    it('marks sub items when parent node matches tag name', () => {
        const parent = document.createElement('m-sidebar');
        const child = document.createElement('m-sidebar-nav-item');
        parent.appendChild(child);
        document.body.appendChild(parent);
        setSubItems('M-SIDEBAR')(element);
        expect(child.sub).toBe(true);
    });
    it('does not mark items with a different parent', () => {
        const parent = document.createElement('div');
        const child = document.createElement('m-sidebar-nav-item');
        parent.appendChild(child);
        document.body.appendChild(parent);
        setSubItems('M-SIDEBAR')(element);
        expect(child.sub).toBeUndefined();
    });
});
