"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolbarDropdownButtonActionHandleController = exports.ToolbarSelectActionHandleController = exports.ToolbarBtnActionHandleController = exports.ExtHostToolbarActionService = void 0;
exports.createToolbarAPIFactory = createToolbarAPIFactory;
const ide_core_common_1 = require("@opensumi/ide-core-common");
const sumi_1 = require("../../../common/sumi");
const toolbar_1 = require("../../../common/sumi/toolbar");
function createToolbarAPIFactory(extension, service) {
    return {
        registerToolbarAction: async (contribution) => service.registerToolbarAction(extension.id, extension.path, contribution),
        getToolbarActionButtonHandle: async (id) => service.getToolbarButtonActionHandle(id, extension.id),
        getToolbarActionSelectHandle: async (id) => service.getToolbarSelectActionHandle(id, extension.id),
        getToolbarActionDropdownButtonHandle: async (id) => service.getToolbarDropdownButtonActionHandle(id, extension.id),
    };
}
class ExtHostToolbarActionService {
    constructor(extHostCommands, extHostCommon, rpcProtocol, logger) {
        this.extHostCommands = extHostCommands;
        this.extHostCommon = extHostCommon;
        this.rpcProtocol = rpcProtocol;
        this.logger = logger;
        this.btnHandles = new Map();
        this.selectHandles = new Map();
        this.dropdownButtonHandles = new Map();
        this.proxy = this.rpcProtocol.getProxy(sumi_1.MainThreadSumiAPIIdentifier.MainThreadToolbar);
    }
    async registerToolbarAction(extensionId, extensionPath, contribution) {
        switch (contribution.type) {
            case toolbar_1.TOOLBAR_ACTION_TYPE.BUTTON:
                await this.proxy.$registerToolbarButtonAction(extensionId, extensionPath, contribution);
                return this.getToolbarButtonActionHandle(contribution.id, extensionId);
            case toolbar_1.TOOLBAR_ACTION_TYPE.DROPDOWN_BUTTON:
                await this.proxy.$registerDropdownButtonAction(extensionId, extensionPath, contribution);
                return this.getToolbarDropdownButtonActionHandle(contribution.id, extensionId);
            case toolbar_1.TOOLBAR_ACTION_TYPE.SELECT:
                await this.proxy.$registerToolbarSelectAction(extensionId, extensionPath, contribution);
                return this.getToolbarSelectActionHandle(contribution.id, extensionId);
            default:
                await this.proxy.$registerToolbarSelectAction(extensionId, extensionPath, contribution);
                return this.getToolbarSelectActionHandle(contribution.id, extensionId);
        }
    }
    getToolbarButtonActionHandle(id, extensionId) {
        const compositeKey = extensionId + '.' + id;
        if (!this.btnHandles.has(compositeKey)) {
            const promise = new Promise((resolve, reject) => {
                const h = new ToolbarBtnActionHandleController(compositeKey, this.extHostCommands, this.extHostCommon, this.logger);
                h.init()
                    .then(() => {
                    resolve(h);
                })
                    .catch((e) => {
                    reject(e);
                });
            });
            this.btnHandles.set(compositeKey, promise);
        }
        return this.btnHandles.get(compositeKey).then((h) => h.handle);
    }
    getToolbarSelectActionHandle(id, extensionId) {
        const compositeKey = extensionId + '.' + id;
        if (!this.selectHandles.has(compositeKey)) {
            const promise = new Promise((resolve, reject) => {
                const h = new ToolbarSelectActionHandleController(compositeKey, this.extHostCommands, this.extHostCommon);
                h.init()
                    .then(() => {
                    resolve(h);
                })
                    .catch((e) => {
                    reject(e);
                });
            });
            this.selectHandles.set(compositeKey, promise);
        }
        return this.selectHandles.get(compositeKey).then((h) => h.handle);
    }
    getToolbarDropdownButtonActionHandle(id, extensionId) {
        const compositeKey = extensionId + '.' + id;
        if (!this.dropdownButtonHandles.has(compositeKey)) {
            const promise = new Promise((resolve, reject) => {
                const h = new ToolbarDropdownButtonActionHandleController(compositeKey, this.extHostCommands, this.extHostCommon);
                h.init()
                    .then(() => {
                    resolve(h);
                })
                    .catch((e) => {
                    reject(e);
                });
            });
            this.dropdownButtonHandles.set(compositeKey, promise);
        }
        return this.dropdownButtonHandles.get(compositeKey).then((h) => h.handle);
    }
}
exports.ExtHostToolbarActionService = ExtHostToolbarActionService;
class ToolbarBtnActionHandleController extends ide_core_common_1.Disposable {
    constructor(id, extHostCommands, kaitianCommon, logger) {
        super();
        this.id = id;
        this.extHostCommands = extHostCommands;
        this.kaitianCommon = kaitianCommon;
        this.logger = logger;
        this._onClick = new ide_core_common_1.Emitter();
        this._onStateChange = new ide_core_common_1.Emitter();
    }
    get handle() {
        if (!this._handle) {
            this._handle = {
                onClick: this._onClick.event,
                onStateChanged: this._onStateChange.event,
                setState: (state, title) => this.extHostCommands.executeCommand(toolbar_1.BUTTON_SET_STATE_ID, this.id, state, title),
                showPopover: async (style) => this.extHostCommands.executeCommand(toolbar_1.SHOW_POPOVER_ID, this.id, style),
                hidePopover: async () => this.extHostCommands.executeCommand(toolbar_1.HIDE_POPOVER_ID, this.id),
                /**
                 * 由插件 API 负责更新的 context 对象
                 * 在自定义 popover 场景下，该 context 对象会被序列化后从 popover 组件 props 传入
                 * @param context {T}
                 */
                setContext: (context) => this.extHostCommands.executeCommand(toolbar_1.BUTTON_SET_CONTEXT_ID, this.id, context),
            };
        }
        return this._handle;
    }
    async init() {
        this.addDispose(this.kaitianCommon.onEvent(toolbar_1.BUTTON_CLICK_ID, (id) => {
            if (id === this.id) {
                this._onClick.fire();
            }
        }));
        this.addDispose(this.kaitianCommon.onEvent(toolbar_1.BUTTON_STATE_CHANGE_ID, (id, from, to) => {
            if (id === this.id) {
                this._onStateChange.fire({ from, to });
            }
        }));
        this.logger.log('init button handle', this.id);
        return this.extHostCommands.executeCommand(toolbar_1.BUTTON_CONNECT_HANDLE_ID, this.id);
    }
}
exports.ToolbarBtnActionHandleController = ToolbarBtnActionHandleController;
class ToolbarSelectActionHandleController extends ide_core_common_1.Disposable {
    constructor(id, extHostCommands, kaitianCommon) {
        super();
        this.id = id;
        this.extHostCommands = extHostCommands;
        this.kaitianCommon = kaitianCommon;
        this._onSelect = new ide_core_common_1.Emitter();
        this._onStateChange = new ide_core_common_1.Emitter();
    }
    get handle() {
        if (!this._handle) {
            this._handle = {
                onSelect: this._onSelect.event,
                onStateChanged: this._onStateChange.event,
                setState: (state, title) => this.extHostCommands.executeCommand(toolbar_1.SELECT_SET_STATE_ID, this.id, state, title),
                setOptions: (options, iconBasePath) => this.extHostCommands.executeCommand(toolbar_1.SELECT_SET_OPTIONS, this.id, iconBasePath, options),
                setSelect: (value) => this.extHostCommands.executeCommand(toolbar_1.SELECT_SET_SELECT_ID, this.id, value),
                getValue: () => this._value,
            };
        }
        return this._handle;
    }
    async init() {
        this.addDispose(this.kaitianCommon.onEvent(toolbar_1.SELECT_ON_SELECT_ID, (id, value) => {
            if (id === this.id) {
                this._onSelect.fire(value);
            }
        }));
        this.addDispose(this.kaitianCommon.onEvent(toolbar_1.SELECT_STATE_CHANGE_ID, (id, from, to) => {
            if (id === this.id) {
                this._onStateChange.fire({ from, to });
            }
        }));
        this._value = (await this.extHostCommands.executeCommand(toolbar_1.SELECT_CONNECT_HANDLE_ID, this.id));
    }
}
exports.ToolbarSelectActionHandleController = ToolbarSelectActionHandleController;
class ToolbarDropdownButtonActionHandleController extends ide_core_common_1.Disposable {
    constructor(id, _extHostCommands, extHostCommon) {
        super();
        this.id = id;
        this._extHostCommands = _extHostCommands;
        this.extHostCommon = extHostCommon;
        this._onSelect = new ide_core_common_1.Emitter();
    }
    get handle() {
        if (!this._handle) {
            this._handle = {
                onSelect: this._onSelect.event,
            };
        }
        return this._handle;
    }
    async init() {
        this.addDispose(this.extHostCommon.onEvent(toolbar_1.DROPDOWN_BUTTON_ON_SELECT_ID, (id, value) => {
            if (id === this.id) {
                this._onSelect.fire(value);
            }
        }));
    }
}
exports.ToolbarDropdownButtonActionHandleController = ToolbarDropdownButtonActionHandleController;
//# sourceMappingURL=ext.host.toolbar.js.map