import { jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { afterEach, beforeEach, describe, expect, it, vi, } from 'vitest';
import { act } from 'react';
import { renderHook } from '@testing-library/react';
import { useEditorSettings } from './useEditorSettings.js';
import { SettingScope } from '../../config/settings.js';
import { MessageType } from '../types.js';
import { checkHasEditorType, allowEditorTypeInSandbox, } from '@vybestack/llxprt-code-core';
import { AppDispatchProvider } from '../contexts/AppDispatchContext.js';
vi.mock('@vybestack/llxprt-code-core', async () => {
    const actual = await vi.importActual('@vybestack/llxprt-code-core');
    return {
        ...actual,
        checkHasEditorType: vi.fn(() => true),
        allowEditorTypeInSandbox: vi.fn(() => true),
    };
});
const mockCheckHasEditorType = vi.mocked(checkHasEditorType);
const mockAllowEditorTypeInSandbox = vi.mocked(allowEditorTypeInSandbox);
describe('useEditorSettings', () => {
    let mockLoadedSettings;
    let mockAppState;
    let mockAddItem;
    let mockDispatch;
    beforeEach(() => {
        vi.resetAllMocks();
        mockLoadedSettings = {
            setValue: vi.fn(),
        };
        mockAppState = {
            openDialogs: {
                theme: false,
                auth: false,
                editor: false,
                providerModel: false,
                provider: false,
                privacy: false,
                loadProfile: false,
                tools: false,
                oauthCode: false,
            },
            warnings: new Map(),
            errors: {
                theme: null,
                auth: null,
                editor: null,
            },
            lastAddItemAction: null,
        };
        mockAddItem = vi.fn();
        mockDispatch = vi.fn();
        // Reset mock implementations to default
        mockCheckHasEditorType.mockReturnValue(true);
        mockAllowEditorTypeInSandbox.mockReturnValue(true);
    });
    afterEach(() => {
        vi.restoreAllMocks();
    });
    it('should initialize with dialog closed', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        expect(result.current.isEditorDialogOpen).toBe(false);
    });
    it('should open editor dialog when openEditorDialog is called', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        act(() => {
            result.current.openEditorDialog();
        });
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'OPEN_DIALOG',
            payload: 'editor',
        });
    });
    it('should close editor dialog when exitEditorDialog is called', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        act(() => {
            result.current.exitEditorDialog();
        });
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'CLOSE_DIALOG',
            payload: 'editor',
        });
    });
    it('should handle editor selection successfully', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        const editorType = 'vscode';
        const scope = SettingScope.User;
        act(() => {
            result.current.handleEditorSelect(editorType, scope);
        });
        expect(mockLoadedSettings.setValue).toHaveBeenCalledWith(scope, 'preferredEditor', editorType);
        expect(mockAddItem).toHaveBeenCalledWith({
            type: MessageType.INFO,
            text: 'Editor preference set to "vscode" in User settings.',
        }, expect.any(Number));
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'SET_EDITOR_ERROR',
            payload: null,
        });
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'CLOSE_DIALOG',
            payload: 'editor',
        });
    });
    it('should handle clearing editor preference (undefined editor)', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        const scope = SettingScope.Workspace;
        act(() => {
            result.current.handleEditorSelect(undefined, scope);
        });
        expect(mockLoadedSettings.setValue).toHaveBeenCalledWith(scope, 'preferredEditor', undefined);
        expect(mockAddItem).toHaveBeenCalledWith({
            type: MessageType.INFO,
            text: 'Editor preference cleared in Workspace settings.',
        }, expect.any(Number));
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'SET_EDITOR_ERROR',
            payload: null,
        });
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'CLOSE_DIALOG',
            payload: 'editor',
        });
    });
    it('should handle different editor types', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        const editorTypes = ['cursor', 'windsurf', 'vim'];
        const scope = SettingScope.User;
        editorTypes.forEach((editorType) => {
            act(() => {
                result.current.handleEditorSelect(editorType, scope);
            });
            expect(mockLoadedSettings.setValue).toHaveBeenCalledWith(scope, 'preferredEditor', editorType);
            expect(mockAddItem).toHaveBeenCalledWith({
                type: MessageType.INFO,
                text: `Editor preference set to "${editorType}" in User settings.`,
            }, expect.any(Number));
        });
    });
    it('should handle different setting scopes', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        const editorType = 'vscode';
        const scopes = [SettingScope.User, SettingScope.Workspace];
        scopes.forEach((scope) => {
            act(() => {
                result.current.handleEditorSelect(editorType, scope);
            });
            expect(mockLoadedSettings.setValue).toHaveBeenCalledWith(scope, 'preferredEditor', editorType);
            expect(mockAddItem).toHaveBeenCalledWith({
                type: MessageType.INFO,
                text: `Editor preference set to "vscode" in ${scope} settings.`,
            }, expect.any(Number));
        });
    });
    it('should not set preference for unavailable editors', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        mockCheckHasEditorType.mockReturnValue(false);
        const editorType = 'vscode';
        const scope = SettingScope.User;
        act(() => {
            result.current.handleEditorSelect(editorType, scope);
        });
        expect(mockLoadedSettings.setValue).not.toHaveBeenCalled();
        expect(mockAddItem).not.toHaveBeenCalled();
        expect(mockDispatch).not.toHaveBeenCalled();
    });
    it('should not set preference for editors not allowed in sandbox', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        mockAllowEditorTypeInSandbox.mockReturnValue(false);
        const editorType = 'vscode';
        const scope = SettingScope.User;
        act(() => {
            result.current.handleEditorSelect(editorType, scope);
        });
        expect(mockLoadedSettings.setValue).not.toHaveBeenCalled();
        expect(mockAddItem).not.toHaveBeenCalled();
        expect(mockDispatch).not.toHaveBeenCalled();
    });
    it('should handle errors during editor selection', () => {
        const { result } = renderHook(() => useEditorSettings(mockLoadedSettings, mockAppState, mockAddItem), {
            wrapper: ({ children }) => (_jsx(AppDispatchProvider, { value: mockDispatch, children: children })),
        });
        const errorMessage = 'Failed to save settings';
        mockLoadedSettings.setValue.mockImplementation(() => {
            throw new Error(errorMessage);
        });
        const editorType = 'vscode';
        const scope = SettingScope.User;
        act(() => {
            result.current.handleEditorSelect(editorType, scope);
        });
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'SET_EDITOR_ERROR',
            payload: `Failed to set editor preference: Error: ${errorMessage}`,
        });
        expect(mockAddItem).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=useEditorSettings.test.js.map