"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadToolbar = exports.KaitianExtensionToolbarService = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_theme_1 = require("@opensumi/ide-theme");
const common_1 = require("../../common");
const toolbar_1 = require("../../common/sumi/toolbar");
const components_1 = require("../components");
let KaitianExtensionToolbarService = class KaitianExtensionToolbarService {
    constructor() {
        this.btnDelegates = new Map();
        this.selectDelegates = new Map();
        this.dropdownButtonDelegates = new Map();
        this.connected = new Set();
        this.commandRegistry.registerCommand({
            id: toolbar_1.BUTTON_SET_STATE_ID,
        }, {
            execute: (id, state, title) => {
                if (this.btnDelegates.has(id)) {
                    this.btnDelegates.get(id).setState(state, title);
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.BUTTON_SET_CONTEXT_ID,
        }, {
            execute: (id, context) => {
                if (this.btnDelegates.has(id)) {
                    const delegate = this.btnDelegates.get(id);
                    if (delegate) {
                        delegate.setContext(context);
                    }
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.BUTTON_CONNECT_HANDLE_ID,
        }, {
            execute: (id) => {
                if (!this.connected.has(id)) {
                    this.doConnectToolbarButtonHandle(id);
                    this.connected.add(id);
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.SELECT_SET_STATE_ID,
        }, {
            execute: (id, state) => {
                if (this.selectDelegates.has(id)) {
                    this.selectDelegates.get(id).setState(state);
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.SELECT_SET_OPTIONS,
        }, {
            execute: (id, extensionBasePath, options) => {
                if (this.selectDelegates.has(id)) {
                    options.forEach((o) => {
                        if (o.iconPath) {
                            o.iconClass = this.iconService.fromIcon(extensionBasePath, o.iconPath, o.iconMaskMode ? ide_theme_1.IconType.Mask : ide_theme_1.IconType.Background);
                        }
                    });
                    this.selectDelegates.get(id).setOptions(options);
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.SELECT_SET_SELECT_ID,
        }, {
            execute: (id, value) => {
                if (this.selectDelegates.has(id)) {
                    this.selectDelegates.get(id).setSelect(value);
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.SELECT_CONNECT_HANDLE_ID,
        }, {
            execute: (id) => {
                if (!this.connected.has(id)) {
                    this.doConnectToolbarSelectHandle(id);
                    this.connected.add(id);
                }
                if (this.selectDelegates.get(id)) {
                    return this.selectDelegates.get(id).getValue();
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.SHOW_POPOVER_ID,
        }, {
            execute: (id, style) => {
                if (this.btnDelegates.has(id)) {
                    this.btnDelegates.get(id).showPopOver(style);
                }
            },
        });
        this.commandRegistry.registerCommand({
            id: toolbar_1.HIDE_POPOVER_ID,
        }, {
            execute: (id) => {
                if (this.btnDelegates.has(id)) {
                    this.btnDelegates.get(id).hidePopOver();
                }
            },
        });
    }
    getPopoverComponent(id, contribution) {
        const PlaceHolderComponent = () => {
            var _a, _b, _c, _d;
            return (0, components_1.ExtensionLoadingView)({
                style: {
                    minHeight: ((_a = contribution.popoverStyle) === null || _a === void 0 ? void 0 : _a.minHeight) ? Number((_b = contribution.popoverStyle) === null || _b === void 0 ? void 0 : _b.minHeight) : 200,
                    minWidth: ((_c = contribution.popoverStyle) === null || _c === void 0 ? void 0 : _c.minWidth) ? Number((_d = contribution.popoverStyle) === null || _d === void 0 ? void 0 : _d.minWidth) : 300,
                },
            });
        };
        return this.toolbarPopover.getComponent(id) || PlaceHolderComponent;
    }
    registerToolbarButton(extensionId, extensionBasePath, contribution) {
        const id = extensionId + '.' + contribution.id;
        const styles = {};
        if (contribution.states) {
            Object.keys(contribution.states).forEach((state) => {
                const o = contribution.states[state];
                styles[state] = {
                    ...o,
                };
                if (o.iconPath) {
                    styles[state].iconClass = this.iconService.fromIcon(extensionBasePath, o.iconPath, o.iconMaskMode ? ide_theme_1.IconType.Mask : ide_theme_1.IconType.Background);
                }
            });
        }
        return this.toolbarRegistry.registerToolbarAction({
            id,
            preferredPosition: contribution.preferredPosition,
            strictPosition: contribution.strictPosition,
            description: contribution.description || contribution.title || id,
            weight: contribution.weight,
            when: contribution.when,
            component: (0, ide_core_browser_1.createToolbarActionBtn)({
                id,
                popoverId: `${extensionId}:${contribution.popoverComponent}`,
                title: contribution.title,
                styles,
                defaultState: contribution.defaultState,
                iconClass: this.iconService.fromIcon(extensionBasePath, contribution.iconPath, contribution.iconMaskMode ? ide_theme_1.IconType.Mask : ide_theme_1.IconType.Background),
                // 这里放一个 LoadingView 用于占位，因为 contributes 执行时插件还没有激活完成
                popoverComponent: contribution.popoverComponent
                    ? this.getPopoverComponent(`${extensionId}:${contribution.popoverComponent}`, contribution)
                    : undefined,
                popoverStyle: contribution.popoverStyle || {
                    noContainerStyle: false,
                },
                delegate: (delegate) => {
                    if (delegate) {
                        this.btnDelegates.set(id, delegate);
                        if (contribution.command) {
                            delegate.onClick(async () => {
                                delegate.showPopOver();
                                await this.eventBus.fireAndAwait(new ide_core_browser_1.ExtensionActivateEvent({ topic: 'onAction', data: contribution.id }));
                                this.commandService.executeCommand(contribution.command);
                            });
                        }
                        if (this.connected.has(id)) {
                            this.doConnectToolbarButtonHandle(id);
                        }
                    }
                },
            }),
        });
    }
    doConnectToolbarButtonHandle(id) {
        const delegate = this.btnDelegates.get(id);
        if (delegate) {
            delegate.onClick(() => this.commandService.executeCommand(common_1.EMIT_EXT_HOST_EVENT.id, toolbar_1.BUTTON_CLICK_ID, id));
            delegate.onChangeState((args) => this.commandService.executeCommand(common_1.EMIT_EXT_HOST_EVENT.id, toolbar_1.BUTTON_STATE_CHANGE_ID, id, args));
        }
    }
    registerToolbarSelect(extensionId, extensionBasePath, contribution) {
        const id = extensionId + '.' + contribution.id;
        const options = contribution.options || [];
        options.forEach((o) => {
            if (o.iconPath) {
                o.iconClass = this.iconService.fromIcon(extensionBasePath, o.iconPath, o.iconMaskMode ? ide_theme_1.IconType.Mask : ide_theme_1.IconType.Background);
            }
        });
        return this.toolbarRegistry.registerToolbarAction({
            id,
            preferredPosition: contribution.preferredPosition,
            strictPosition: contribution.strictPosition,
            description: contribution.description,
            component: (0, ide_core_browser_1.createToolbarActionSelect)({
                styles: contribution.states,
                options,
                defaultState: contribution.defaultState,
                defaultValue: contribution.defaultValue,
                equals: contribution.optionEqualityKey
                    ? (v1, v2) => {
                        const key = contribution.optionEqualityKey;
                        if (!v1 || !v2) {
                            return v1 === v2;
                        }
                        else {
                            return v1[key] === v2[key];
                        }
                    }
                    : undefined,
                delegate: (delegate) => {
                    if (delegate) {
                        this.selectDelegates.set(id, delegate);
                        if (contribution.command) {
                            delegate.onSelect((v) => {
                                this.commandService.executeCommand(contribution.command, v);
                            });
                        }
                        if (this.connected.has(id)) {
                            this.doConnectToolbarSelectHandle(id);
                        }
                    }
                },
            }),
        });
    }
    doConnectToolbarSelectHandle(id) {
        const delegate = this.selectDelegates.get(id);
        if (delegate) {
            delegate.onSelect((value) => this.commandService.executeCommand(common_1.EMIT_EXT_HOST_EVENT.id, toolbar_1.SELECT_ON_SELECT_ID, id, value));
            delegate.onChangeState((args) => this.commandService.executeCommand(common_1.EMIT_EXT_HOST_EVENT.id, toolbar_1.SELECT_STATE_CHANGE_ID, id, args));
        }
    }
    registerToolbarDropdownButton(extensionId, extensionBasePath, contribution) {
        const id = extensionId + '.' + contribution.id;
        const options = contribution.options || [];
        return this.toolbarRegistry.registerToolbarAction({
            id,
            preferredPosition: contribution.preferredPosition,
            strictPosition: contribution.strictPosition,
            description: contribution.description,
            component: (0, ide_core_browser_1.createToolbarActionDropdownButton)({
                options,
                trigger: contribution.trigger,
                delegate: (delegate) => {
                    if (delegate) {
                        this.dropdownButtonDelegates.set(id, delegate);
                        if (contribution.command) {
                            delegate.onSelect((v) => {
                                this.commandService.executeCommand(contribution.command, v);
                            });
                        }
                        if (this.connected.has(id)) {
                            this.doConnectToolbarDropdownButtonHandle(id);
                        }
                    }
                },
            }),
        });
    }
    doConnectToolbarDropdownButtonHandle(id) {
        const delegate = this.selectDelegates.get(id);
        if (delegate) {
            delegate.onSelect((value) => this.commandService.executeCommand(common_1.EMIT_EXT_HOST_EVENT.id, toolbar_1.DROPDOWN_BUTTON_ON_SELECT_ID, id, value));
        }
    }
};
exports.KaitianExtensionToolbarService = KaitianExtensionToolbarService;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IEventBus),
    tslib_1.__metadata("design:type", Object)
], KaitianExtensionToolbarService.prototype, "eventBus", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IToolbarPopoverRegistry),
    tslib_1.__metadata("design:type", Object)
], KaitianExtensionToolbarService.prototype, "toolbarPopover", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IToolbarRegistry),
    tslib_1.__metadata("design:type", Object)
], KaitianExtensionToolbarService.prototype, "toolbarRegistry", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IIconService),
    tslib_1.__metadata("design:type", Object)
], KaitianExtensionToolbarService.prototype, "iconService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.CommandService),
    tslib_1.__metadata("design:type", Object)
], KaitianExtensionToolbarService.prototype, "commandService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.CommandRegistry),
    tslib_1.__metadata("design:type", Object)
], KaitianExtensionToolbarService.prototype, "commandRegistry", void 0);
exports.KaitianExtensionToolbarService = KaitianExtensionToolbarService = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    tslib_1.__metadata("design:paramtypes", [])
], KaitianExtensionToolbarService);
// 与 KaitianExtensionToolbarService 区分一下，只是为了给插件进程调用注册 actions
let MainThreadToolbar = class MainThreadToolbar extends ide_core_browser_1.Disposable {
    $registerToolbarButtonAction(extensionId, extensionPath, contribution) {
        this.addDispose(this.toolbarService.registerToolbarButton(extensionId, extensionPath, contribution));
        return Promise.resolve();
    }
    $registerToolbarSelectAction(extensionId, extensionPath, contribution) {
        this.addDispose(this.toolbarService.registerToolbarSelect(extensionId, extensionPath, contribution));
        return Promise.resolve();
    }
    $registerDropdownButtonAction(extensionId, extensionPath, contribution) {
        this.addDispose(this.toolbarService.registerToolbarDropdownButton(extensionId, extensionPath, contribution));
        return Promise.resolve();
    }
};
exports.MainThreadToolbar = MainThreadToolbar;
tslib_1.__decorate([
    (0, di_1.Autowired)(KaitianExtensionToolbarService),
    tslib_1.__metadata("design:type", KaitianExtensionToolbarService)
], MainThreadToolbar.prototype, "toolbarService", void 0);
exports.MainThreadToolbar = MainThreadToolbar = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true })
], MainThreadToolbar);
//# sourceMappingURL=main.thread.toolbar.js.map