import { theme } from "../tui/components/index.js";
const REMOTE_MODE_ERROR = "not available in remote mode";
export function createPeerExtensionUIContext(deps) {
    const extensionStatuses = new Map();
    const widgetFactories = new Map();
    const widgetComponents = new Map();
    let _footerFactory;
    let _headerFactory;
    let editorFactory;
    const terminalInputHandlers = [];
    return {
        select: async (_title, _options, _opts) => {
            throw new Error(`select ${REMOTE_MODE_ERROR}`);
        },
        confirm: async (_title, _message, _opts) => {
            throw new Error(`confirm ${REMOTE_MODE_ERROR}`);
        },
        input: async (_title, _placeholder, _opts) => {
            throw new Error(`input ${REMOTE_MODE_ERROR}`);
        },
        notify: (message, type) => {
            const color = type === "error" ? "error" : type === "warning" ? "warning" : "dim";
            deps.statusContainer.addChild({ render: () => [theme.fg(color, message)] });
            deps.requestRender();
        },
        onTerminalInput: (handler) => {
            terminalInputHandlers.push(handler);
            return () => {
                const index = terminalInputHandlers.indexOf(handler);
                if (index >= 0) {
                    terminalInputHandlers.splice(index, 1);
                }
            };
        },
        setStatus: (key, text) => {
            if (text === undefined) {
                extensionStatuses.delete(key);
            }
            else {
                extensionStatuses.set(key, text);
            }
        },
        setWorkingMessage: (_message) => {
            // Working message is managed by ForkedInteractiveMode's status area
        },
        setWorkingVisible: (_visible) => {
            // Working visibility is managed by ForkedInteractiveMode's status area
        },
        setWorkingIndicator: (_options) => {
            // Working indicator is managed by ForkedInteractiveMode's status area
        },
        setHiddenThinkingLabel: (_label) => {
            // No-op for remote mode
        },
        setWidget: (key, content, options) => {
            const existing = widgetComponents.get(key);
            if (existing?.dispose) {
                existing.dispose();
            }
            widgetComponents.delete(key);
            widgetFactories.delete(key);
            if (content === undefined) {
                return;
            }
            if (Array.isArray(content)) {
                // String array widget — render as text lines
                const lines = content;
                const widget = {
                    render: () => lines,
                    invalidate: () => { },
                    dispose: () => { },
                };
                widgetComponents.set(key, widget);
                widgetFactories.set(key, { factory: () => widget, options });
                return;
            }
            if (typeof content === "function") {
                const factory = content;
                widgetFactories.set(key, { factory, options });
                const widget = factory(deps.ui, theme);
                widgetComponents.set(key, widget);
            }
        },
        setFooter: (factory) => {
            _footerFactory = factory ?? undefined;
        },
        setHeader: (factory) => {
            _headerFactory = factory ?? undefined;
        },
        setTitle: (title) => {
            deps.setTerminalTitle(title);
        },
        custom: async (factory, _options) => {
            return new Promise((resolve) => {
                const done = (result) => {
                    deps.editorContainer.clear();
                    deps.editorContainer.addChild(deps.editor);
                    deps.ui.setFocus(deps.editor);
                    deps.requestRender();
                    resolve(result);
                };
                const componentOrPromise = factory(deps.ui, theme, deps.keybindings, done);
                void Promise.resolve(componentOrPromise).then((component) => {
                    deps.editorContainer.clear();
                    deps.editorContainer.addChild(component);
                    deps.ui.setFocus(component);
                    deps.requestRender();
                });
            });
        },
        pasteToEditor: (text) => {
            deps.editor.handleInput(`\x1b[200~${text}\x1b[201~`);
        },
        setEditorText: (text) => {
            deps.editor.setText(text);
        },
        getEditorText: () => {
            return deps.editor.getExpandedText?.() ?? deps.editor.getText();
        },
        editor: async (_title, _prefill) => {
            throw new Error(`editor ${REMOTE_MODE_ERROR}`);
        },
        addAutocompleteProvider: (_factory) => {
            // No-op for now — autocomplete is managed by ForkedInteractiveMode
        },
        setEditorComponent: (factory) => {
            editorFactory = factory ?? undefined;
        },
        getEditorComponent: () => {
            return editorFactory;
        },
        get theme() {
            return theme;
        },
        getAllThemes: () => {
            return [];
        },
        getTheme: (_name) => {
            return undefined;
        },
        setTheme: (_themeOrName) => {
            throw new Error(`setTheme ${REMOTE_MODE_ERROR}`);
        },
        getToolsExpanded: () => false,
        setToolsExpanded: (_expanded) => {
            // No-op for remote mode
        },
    };
}
//# sourceMappingURL=peer-extension-ui-context.js.map