"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtHostWindow = void 0;
exports.createWindowApiFactory = createWindowApiFactory;
const tslib_1 = require("tslib");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
const types = tslib_1.__importStar(require("../../../common/vscode/ext-types"));
const ext_types_1 = require("../../../common/vscode/ext-types");
const extension_1 = require("../../../common/vscode/extension");
function createWindowApiFactory(extension, extHostEditors, extHostMessage, extHostWebviews, extHostWebviewView, extHostTreeView, extHostWindowState, extHostDecorations, extHostStatusBar, extHostQuickOpen, extHostOutput, extHostTerminal, extHostWindow, extHostProgress, extHostUrls, extHostTheming, extHostCustomEditor, extHostEditorTabs) {
    const extensionInfo = {
        id: extension.id,
        extensionId: extension.extensionId,
        isBuiltin: extension.isBuiltin,
    };
    const _onDidWriteTerminalData = new ide_core_common_1.Emitter();
    return {
        // @deprecated
        withScmProgress(task) {
            return extHostProgress.withProgress(extension, { location: types.ProgressLocation.SourceControl }, (progress, token) => task({
                report(n) {
                    /* noop*/
                },
            }));
        },
        withProgress(options, task) {
            return extHostProgress.withProgress(extension, options, task);
        },
        createStatusBarItem(alignmentOrId, priorityOrAlignment, priorityArg) {
            let id;
            let alignment;
            let priority;
            if (typeof alignmentOrId === 'string') {
                id = alignmentOrId;
                alignment = priorityOrAlignment;
                priority = priorityArg;
            }
            else {
                alignment = alignmentOrId;
                priority = priorityOrAlignment;
            }
            return extHostStatusBar.createStatusBarItem(extension, id, alignment, priority);
        },
        createOutputChannel(name, options) {
            return extHostOutput.createOutputChannel(name, options);
        },
        setStatusBarMessage(text, arg) {
            // step2
            return extHostStatusBar.setStatusBarMessage(text, arg);
        },
        showInformationMessage(message, first, ...rest) {
            return extHostMessage.showMessage(ide_core_common_1.MessageType.Info, message, first, extension.displayName || extension.name, ...rest);
        },
        showWarningMessage(message, first, ...rest) {
            return extHostMessage.showMessage(ide_core_common_1.MessageType.Warning, message, first, extension.displayName || extension.name, ...rest);
        },
        showErrorMessage(message, first, ...rest) {
            return extHostMessage.showMessage(ide_core_common_1.MessageType.Error, message, first, extension.displayName || extension.name, ...rest);
        },
        registerTreeDataProvider(viewId, treeDataProvider) {
            return extHostTreeView.registerTreeDataProvider(viewId, treeDataProvider);
        },
        createTreeView(viewId, options) {
            return extHostTreeView.createTreeView(viewId, options);
        },
        get activeTextEditor() {
            return extHostEditors.activeEditor && extHostEditors.activeEditor.textEditor;
        },
        get visibleTextEditors() {
            return extHostEditors.visibleEditors;
        },
        onDidChangeActiveTextEditor: extHostEditors.onDidChangeActiveTextEditor,
        onDidChangeVisibleTextEditors: extHostEditors.onDidChangeVisibleTextEditors,
        onDidChangeTextEditorSelection: extHostEditors.onDidChangeTextEditorSelection,
        onDidChangeTextEditorVisibleRanges: extHostEditors.onDidChangeTextEditorVisibleRanges,
        onDidChangeTextEditorOptions: extHostEditors.onDidChangeTextEditorOptions,
        onDidChangeTextEditorViewColumn: extHostEditors.onDidChangeTextEditorViewColumn,
        onDidWriteTerminalData: _onDidWriteTerminalData.event,
        showTextDocument(documentOrUri, columnOrOptions, preserveFocus) {
            return extHostEditors.showTextDocument(documentOrUri, columnOrOptions, preserveFocus);
        },
        createTextEditorDecorationType(options) {
            return extHostEditors.createTextEditorDecorationType(extension.id, options);
        },
        showQuickPick(items, options, token) {
            return extHostQuickOpen.showQuickPick(items, options, token);
        },
        showWorkspaceFolderPick(options) {
            return extHostQuickOpen.showWorkspaceFolderPick(options);
        },
        createQuickPick() {
            return extHostQuickOpen.createQuickPick();
        },
        showInputBox(options, token) {
            return extHostQuickOpen.showInputBox(options, token);
        },
        createInputBox() {
            return extHostQuickOpen.createInputBox();
        },
        createWebviewPanel(viewType, title, showOptions, options) {
            return extHostWebviews.createWebview(extension.extensionLocation, viewType, title, showOptions, options, extensionInfo);
        },
        registerWebviewPanelSerializer(viewType, serializer) {
            return extHostWebviews.registerWebviewPanelSerializer(viewType, serializer);
        },
        registerFileDecorationProvider: (provider) => extHostDecorations.registerFileDecorationProvider(provider, extension.id),
        registerUriHandler(handler) {
            return extHostUrls.registerUriHandler(extension.id, handler);
        },
        showOpenDialog: (options) => extHostWindow.openDialog(options),
        showSaveDialog: (options) => extHostWindow.showSaveDialog(options),
        get onDidChangeWindowState() {
            return extHostWindowState.onDidChangeWindowState;
        },
        get state() {
            return extHostWindowState.state;
        },
        /**
         * Terminal
         */
        get activeTerminal() {
            return extHostTerminal.activeTerminal;
        },
        get terminals() {
            return extHostTerminal.terminals;
        },
        onDidChangeActiveTerminal: extHostTerminal.onDidChangeActiveTerminal,
        onDidCloseTerminal: extHostTerminal.onDidCloseTerminal,
        onDidOpenTerminal: extHostTerminal.onDidOpenTerminal,
        onDidChangeTerminalState: extHostTerminal.onDidChangeTerminalState,
        createTerminal(nameOrOptions, shellPath, shellArgs) {
            if (typeof nameOrOptions === 'object') {
                if ('pty' in nameOrOptions) {
                    return extHostTerminal.createExtensionTerminal(nameOrOptions);
                }
                return extHostTerminal.createTerminalFromOptions(nameOrOptions);
            }
            return extHostTerminal.createTerminal(nameOrOptions, shellPath, shellArgs);
        },
        get activeColorTheme() {
            return extHostTheming.activeColorTheme;
        },
        onDidChangeActiveColorTheme(listener, thisArg, disposables) {
            return extHostTheming.onDidChangeActiveColorTheme(listener, thisArg, disposables);
        },
        registerCustomEditorProvider(viewType, provider, options = {}) {
            return extHostCustomEditor.registerCustomEditorProvider(viewType, provider, options, extensionInfo);
        },
        registerTerminalLinkProvider(handler) {
            return extHostTerminal.registerLinkProvider(handler);
        },
        registerTerminalProfileProvider(id, provider) {
            return extHostTerminal.registerTerminalProfileProvider(extension, id, provider);
        },
        registerWebviewViewProvider(viewId, provider, options) {
            return extHostWebviewView.registerWebviewViewProvider(extension, viewId, provider, options === null || options === void 0 ? void 0 : options.webviewOptions);
        },
        get openEditors() {
            return extHostEditorTabs.tabs;
        },
        get onDidChangeOpenEditors() {
            return extHostEditorTabs.onDidChangeTabs;
        },
        get tabGroups() {
            return extHostEditorTabs.tabGroups;
        },
        /** @stubbed Terminal Shell Ingration */
        onDidChangeTerminalShellIntegration: ide_core_common_1.Event.None,
        /** @stubbed Terminal Shell Ingration */
        onDidEndTerminalShellExecution: ide_core_common_1.Event.None,
        /** @stubbed Terminal Shell Ingration */
        onDidStartTerminalShellExecution: ide_core_common_1.Event.None,
    };
}
function proposedApiFunction(extension, fn) {
    if (extension.enableProposedApi) {
        return fn;
    }
    else {
        return extension_1.throwProposedApiError.bind(null, extension);
    }
}
class ExtHostWindow {
    constructor(rpcProtocol) {
        this.id = 0;
        this._onOpenedResult = new ide_core_common_1.Emitter();
        this._onSavedResult = new ide_core_common_1.Emitter();
        this.proxy = rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadWindow);
    }
    openDialog(options) {
        return new Promise((resolve) => {
            const id = (this.id++).toString();
            this.proxy.$showOpenDialog(id, options);
            const disposer = this._onOpenedResult.event((res) => {
                if (res.id === id) {
                    disposer.dispose();
                    resolve(res.result ? res.result.map((r) => ext_types_1.Uri.revive(r)) : undefined);
                }
            });
        });
    }
    showSaveDialog(options) {
        return new Promise((resolve) => {
            const id = (this.id++).toString();
            this.proxy.$showSaveDialog(id, options);
            const disposer = this._onSavedResult.event((res) => {
                if (res.id === id) {
                    disposer.dispose();
                    resolve(res.result ? ext_types_1.Uri.revive(res.result) : undefined);
                }
            });
        });
    }
    $onOpenDialogResult(id, result) {
        this._onOpenedResult.fire({ id, result });
    }
    $onSaveDialogResult(id, result) {
        this._onSavedResult.fire({ id, result });
    }
}
exports.ExtHostWindow = ExtHostWindow;
//# sourceMappingURL=ext.host.window.api.impl.js.map