import { jsx as _jsx } from "react/jsx-runtime";
import { act, renderHook } from '@testing-library/react';
import { vi } from 'vitest';
import { useStdin } from 'ink';
import { EventEmitter } from 'node:events';
import { MouseProvider, useMouseContext } from './MouseContext.js';
import { useMouse } from '../hooks/useMouse.js';
vi.mock('ink', async (importOriginal) => {
    const original = await importOriginal();
    return {
        ...original,
        useStdin: vi.fn(),
    };
});
class MockStdin extends EventEmitter {
    isTTY = true;
    setRawMode = vi.fn();
    on = this.addListener;
    removeListener = super.removeListener;
    resume = vi.fn();
    pause = vi.fn();
    write(text) {
        this.emit('data', text);
    }
}
describe('MouseContext', () => {
    let stdin;
    let wrapper;
    beforeEach(() => {
        stdin = new MockStdin();
        useStdin.mockReturnValue({
            stdin,
            setRawMode: vi.fn(),
        });
        wrapper = ({ children }) => (_jsx(MouseProvider, { mouseEventsEnabled: true, children: children }));
    });
    it('subscribes and unsubscribes handlers', () => {
        const handler = vi.fn();
        const { result } = renderHook(() => useMouseContext(), { wrapper });
        act(() => result.current.subscribe(handler));
        act(() => stdin.write('\x1b[<0;10;20M'));
        expect(handler).toHaveBeenCalledTimes(1);
        act(() => result.current.unsubscribe(handler));
        act(() => stdin.write('\x1b[<0;10;20M'));
        expect(handler).toHaveBeenCalledTimes(1);
    });
    it('does not call handler when hook is inactive', () => {
        const handler = vi.fn();
        renderHook(() => useMouse(handler, { isActive: false }), { wrapper });
        act(() => stdin.write('\x1b[<0;10;20M'));
        expect(handler).not.toHaveBeenCalled();
    });
    it('does not listen when mouseEventsEnabled is false', () => {
        const handler = vi.fn();
        const disabledWrapper = ({ children }) => (_jsx(MouseProvider, { mouseEventsEnabled: false, children: children }));
        const { result } = renderHook(() => useMouseContext(), {
            wrapper: disabledWrapper,
        });
        act(() => result.current.subscribe(handler));
        act(() => stdin.write('\x1b[<0;10;20M'));
        expect(handler).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=MouseContext.test.js.map