"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadQuickOpen = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_components_1 = require("@opensumi/ide-components");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const quick_open_1 = require("@opensumi/ide-core-browser/lib/quick-open");
const ide_quick_open_1 = require("@opensumi/ide-quick-open");
const quick_open_item_service_1 = require("@opensumi/ide-quick-open/lib/browser/quick-open-item.service");
const quick_title_bar_1 = require("@opensumi/ide-quick-open/lib/browser/quick-title-bar");
const quickInput_inputBox_1 = require("@opensumi/ide-quick-open/lib/browser/quickInput.inputBox");
const vscode_1 = require("../../../common/vscode");
let MainThreadQuickOpen = class MainThreadQuickOpen extends ide_core_browser_1.Disposable {
    constructor(rpcProtocol) {
        super();
        this.rpcProtocol = rpcProtocol;
        this.createdInputBox = new Map();
        this.proxy = this.rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostQuickOpen);
        this.addDispose([
            this.quickTitleBarService.onDidTriggerButton((button) => {
                this.proxy.$onDidTriggerButton(button.handler);
            }),
            this.quickOpenItemService.onDidTriggerItemButton(({ item, button }) => {
                if (button.handle !== undefined) {
                    this.proxy.$onDidTriggerItemButton(item.handle, button.handle);
                }
            }),
        ]);
    }
    $showQuickPick(_session, items, options) {
        return this.quickPickService.show(items, {
            ...options,
            onSelect: (_, index) => {
                // value 为 handle
                this.proxy.$onItemSelected(items[index].value);
            },
            onChangeValue: (value) => {
                this.proxy.$onDidChangeValue(_session, value);
            },
        });
    }
    $hideQuickPick() {
        this.quickPickService.hide();
    }
    /**
     * 将插件 severity 转换为前端组件识别的 VALIDATE_TYPE
     * 由于插件进程不能依赖 @opensumi/ide-components 模块，所以在前端进行转换
     * @param severity
     * @returns
     */
    severityToValidateType(severity) {
        switch (severity) {
            case vscode_1.Severity.Info:
                return ide_components_1.VALIDATE_TYPE.INFO;
            case vscode_1.Severity.Warning:
                return ide_components_1.VALIDATE_TYPE.WARNING;
            case vscode_1.Severity.Error:
                return ide_components_1.VALIDATE_TYPE.ERROR;
            case vscode_1.Severity.Ignore:
                return ide_components_1.VALIDATE_TYPE.IGNORE;
            default:
                return ide_components_1.VALIDATE_TYPE.ERROR;
        }
    }
    $showQuickInput(options, validateInput) {
        // 校验逻辑在扩展进程中执行
        if (validateInput) {
            options.validateInput = async (val) => {
                const result = await this.proxy.$validateInput(val);
                if (!result) {
                    return result;
                }
                return typeof result === 'string'
                    ? result
                    : {
                        message: result.message,
                        type: this.severityToValidateType(result.severity),
                    };
            };
        }
        return this.quickInputService.open(options);
    }
    $createOrUpdateInputBox(id, inputOptions) {
        const options = {
            ...inputOptions,
            validationType: this.severityToValidateType(inputOptions === null || inputOptions === void 0 ? void 0 : inputOptions.severity),
        };
        if (this.createdInputBox.has(id)) {
            // 已经存在，需要更新
            const box = this.createdInputBox.get(id);
            box === null || box === void 0 ? void 0 : box.updateOptions(options);
        }
        else {
            const inputBox = this.injector.get(quickInput_inputBox_1.InputBoxImpl, [options]);
            inputBox.open();
            inputBox.onDidChangeValue((e) => {
                this.proxy.$onCreatedInputBoxDidChangeValue(id, e);
            });
            inputBox.onDidAccept(() => {
                this.proxy.$onCreatedInputBoxDidAccept(id);
            });
            inputBox.onDidHide(() => {
                this.proxy.$onCreatedInputBoxDidHide(id);
            });
            inputBox.onDidTriggerButton((btnHandle) => {
                this.proxy.$onCreatedInputBoxDidTriggerButton(id, btnHandle);
            });
            this.createdInputBox.set(id, inputBox);
        }
    }
    $updateQuickPick(options) {
        this.quickPickService.updateOptions(options);
    }
    $hideInputBox(id) {
        if (this.createdInputBox.has(id)) {
            const box = this.createdInputBox.get(id);
            box === null || box === void 0 ? void 0 : box.hide();
        }
    }
    $disposeInputBox(id) {
        if (this.createdInputBox.has(id)) {
            const box = this.createdInputBox.get(id);
            box === null || box === void 0 ? void 0 : box.dispose();
            this.createdInputBox.delete(id);
        }
    }
    $hideQuickInput() {
        this.quickInputService.hide();
    }
};
exports.MainThreadQuickOpen = MainThreadQuickOpen;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_quick_open_1.QuickPickService),
    tslib_1.__metadata("design:type", Object)
], MainThreadQuickOpen.prototype, "quickPickService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(quick_open_1.IQuickInputService),
    tslib_1.__metadata("design:type", Object)
], MainThreadQuickOpen.prototype, "quickInputService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(quick_title_bar_1.QuickTitleBar),
    tslib_1.__metadata("design:type", quick_title_bar_1.QuickTitleBar)
], MainThreadQuickOpen.prototype, "quickTitleBarService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(quick_open_item_service_1.QuickOpenItemService),
    tslib_1.__metadata("design:type", quick_open_item_service_1.QuickOpenItemService)
], MainThreadQuickOpen.prototype, "quickOpenItemService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(di_1.INJECTOR_TOKEN),
    tslib_1.__metadata("design:type", di_1.Injector)
], MainThreadQuickOpen.prototype, "injector", void 0);
exports.MainThreadQuickOpen = MainThreadQuickOpen = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(ide_connection_1.IRPCProtocol)),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadQuickOpen);
//# sourceMappingURL=main.thread.quickopen.js.map