/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useCallback } from 'react';
import { MessageType } from '../types.js';
import { allowEditorTypeInSandbox, checkHasEditorType, } from '@vybestack/llxprt-code-core';
import { useAppDispatch } from '../contexts/AppDispatchContext.js';
export const useEditorSettings = (loadedSettings, appState, addItem) => {
    const appDispatch = useAppDispatch();
    const isEditorDialogOpen = appState.openDialogs.editor;
    const openEditorDialog = useCallback(() => {
        appDispatch({ type: 'OPEN_DIALOG', payload: 'editor' });
    }, [appDispatch]);
    const handleEditorSelect = useCallback((editorType, scope) => {
        if (editorType &&
            (!checkHasEditorType(editorType) ||
                !allowEditorTypeInSandbox(editorType))) {
            return;
        }
        try {
            loadedSettings.setValue(scope, 'ui.preferredEditor', editorType);
            addItem({
                type: MessageType.INFO,
                text: `Editor preference ${editorType ? `set to "${editorType}"` : 'cleared'} in ${scope} settings.`,
            }, Date.now());
            appDispatch({ type: 'SET_EDITOR_ERROR', payload: null });
            appDispatch({ type: 'CLOSE_DIALOG', payload: 'editor' });
        }
        catch (error) {
            appDispatch({
                type: 'SET_EDITOR_ERROR',
                payload: `Failed to set editor preference: ${error}`,
            });
        }
    }, [loadedSettings, appDispatch, addItem]);
    const exitEditorDialog = useCallback(() => {
        appDispatch({ type: 'CLOSE_DIALOG', payload: 'editor' });
    }, [appDispatch]);
    return {
        isEditorDialogOpen,
        openEditorDialog,
        handleEditorSelect,
        exitEditorDialog,
    };
};
//# sourceMappingURL=useEditorSettings.js.map