"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatusBarItemImpl = exports.ExtHostStatusBar = void 0;
const tslib_1 = require("tslib");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
const converter_1 = require("../../../common/vscode/converter");
const ext_types_1 = require("../../../common/vscode/ext-types");
const types = tslib_1.__importStar(require("../../../common/vscode/ext-types"));
class ExtHostStatusBar {
    constructor(rpcProtocol) {
        this.argumentProcessors = [];
        this.rpcProtocol = rpcProtocol;
        this.proxy = this.rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadStatusBar);
    }
    setStatusBarMessage(text, arg) {
        // step3
        this.proxy.$setStatusBarMessage(text);
        let handle;
        if (typeof arg === 'number') {
            handle = setTimeout(() => this.proxy.$dispose(), arg);
        }
        else if (typeof arg !== 'undefined') {
            arg.then(() => this.proxy.$dispose(), () => this.proxy.$dispose());
        }
        return ext_types_1.Disposable.create(() => {
            this.proxy.$dispose();
            if (handle) {
                clearTimeout(handle);
            }
        });
    }
    createStatusBarItem(extension, id, alignment, priority) {
        const statusBarItem = new StatusBarItemImpl(this.rpcProtocol, extension, id, alignment, priority);
        this.proxy.$createStatusBarItem(statusBarItem.entryId, statusBarItem.id, statusBarItem.alignment, statusBarItem.priority);
        return statusBarItem;
    }
}
exports.ExtHostStatusBar = ExtHostStatusBar;
class StatusBarItemImpl {
    constructor(_rpcProtocol, _extension, _id, _alignment = types.StatusBarAlignment.Left, _priority = 0) {
        this._rpcProtocol = _rpcProtocol;
        this._extension = _extension;
        this._id = _id;
        this._alignment = _alignment;
        this._priority = _priority;
        this._entryId = StatusBarItemImpl.nextId();
        this._text = '';
        this._proxy = this._rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadStatusBar);
    }
    get id() {
        var _a;
        return (_a = this._id) !== null && _a !== void 0 ? _a : this._extension.identifier.value;
    }
    get entryId() {
        return this._entryId;
    }
    get alignment() {
        return this._alignment;
    }
    get priority() {
        return this._priority;
    }
    get text() {
        return this._text;
    }
    set text(text) {
        this._text = text;
        this.update();
    }
    get name() {
        return this._name;
    }
    set name(name) {
        this._name = name;
        this.update();
    }
    get tooltip() {
        return this._tooltip;
    }
    set tooltip(tooltip) {
        this._tooltip = tooltip;
        this.update();
    }
    get color() {
        return this._color;
    }
    set color(color) {
        this._color = color;
        this.update();
    }
    get backgroundColor() {
        return this._backgroundColor;
    }
    set backgroundColor(color) {
        if (!color) {
            color = undefined;
        }
        else if ((0, ide_core_common_1.isString)(color)) {
            if (!StatusBarItemImpl.ALLOWED_BACKGROUND_CSS_VARS.has(color)) {
                color = undefined;
            }
        }
        else if (!StatusBarItemImpl.ALLOWED_BACKGROUND_COLORS.has(color.id)) {
            color = undefined;
        }
        this._backgroundColor = color;
        this.update();
    }
    get accessibilityInformation() {
        return this._accessibilityInformation;
    }
    set accessibilityInformation(accessibilityInformation) {
        this._accessibilityInformation = accessibilityInformation;
        this.update();
    }
    get command() {
        return this._command;
    }
    set command(command) {
        this._command = command;
        this.update();
    }
    show() {
        this._isVisible = true;
        this.update();
    }
    hide() {
        if (this._timeoutHandle) {
            clearTimeout(this._timeoutHandle);
        }
        this._proxy.$dispose(this.entryId);
        this._isVisible = false;
    }
    update() {
        if (!this._isVisible) {
            return;
        }
        if (this._timeoutHandle) {
            clearTimeout(this._timeoutHandle);
        }
        // Defer the update so that multiple changes to setters don't cause a redraw each
        this._timeoutHandle = setTimeout(() => {
            var _a, _b;
            this._timeoutHandle = undefined;
            const commandId = typeof this.command === 'string' ? this.command : (_a = this.command) === null || _a === void 0 ? void 0 : _a.command;
            const commandArgs = typeof this.command === 'string' ? undefined : (_b = this.command) === null || _b === void 0 ? void 0 : _b.arguments;
            // If the id is not set, derive it from the extension identifier,
            // otherwise make sure to prefix it with the extension identifier
            // to get a more unique value across extensions.
            let id;
            if (this._extension) {
                if (this._id) {
                    id = `${this._extension.identifier.value}.${this._id}`;
                }
                else {
                    id = this._extension.identifier.value;
                }
            }
            else {
                id = this._id;
            }
            // If the name is not set, derive it from the extension descriptor
            let name;
            if (this._name) {
                name = this._name;
            }
            else {
                name = (0, ide_core_common_1.formatLocalize)('extension.label', this._extension.displayName || this._extension.name);
            }
            // If a background color is set, the foreground is determined
            let color = this._color;
            if (this._backgroundColor) {
                if ((0, ide_core_common_1.isString)(this._backgroundColor)) {
                    color = StatusBarItemImpl.ALLOWED_BACKGROUND_CSS_VARS.get(this._backgroundColor);
                }
                else {
                    color = StatusBarItemImpl.ALLOWED_BACKGROUND_COLORS.get(this._backgroundColor.id);
                }
            }
            // Set to status bar
            this._proxy.$setMessage(this._entryId, id, name, this.text, this.priority, this.alignment, color, this._backgroundColor, converter_1.MarkdownString.fromStrict(this.tooltip), this.accessibilityInformation, commandId, commandArgs);
        }, 0);
    }
    dispose() {
        this.hide();
    }
    static nextId() {
        return StatusBarItemImpl.ID_PREFIX + ':' + (0, ide_core_common_1.uuid)();
    }
}
exports.StatusBarItemImpl = StatusBarItemImpl;
StatusBarItemImpl.ALLOWED_BACKGROUND_COLORS = new Map([
    ['statusBarItem.errorBackground', new ext_types_1.ThemeColor('statusBarItem.errorForeground')],
    ['statusBarItem.warningBackground', new ext_types_1.ThemeColor('statusBarItem.warningForeground')],
]);
StatusBarItemImpl.ALLOWED_BACKGROUND_CSS_VARS = new Map([
    ['var(--statusBarItem-errorBackground)', 'var(--statusBarItem-errorForeground)'],
    ['var(--statusBarItem-warningBackground)', 'var(--statusBarItem-warningForeground)'],
]);
StatusBarItemImpl.ID_PREFIX = 'plugin-status-bar-item';
//# sourceMappingURL=ext.host.statusbar.js.map