"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.separatorItem = void 0;
const reducer_1 = require("../../reducer");
const focusEditorIfEditorCommand_1 = require("./focusEditorIfEditorCommand");
const Logger_1 = require("@joplin/utils/Logger");
const locale_1 = require("../../locale");
const logger = Logger_1.default.create('ToolbarButtonUtils');
exports.separatorItem = {
    type: 'separator',
};
class ToolbarButtonUtils {
    constructor(service) {
        this.toolbarButtonCache_ = {};
        this.service_ = service;
    }
    get service() {
        return this.service_;
    }
    commandToToolbarButton(commandName, whenClauseContext, keymapService) {
        const newEnabled = this.service.isEnabled(commandName, whenClauseContext);
        const newVisible = this.service.isVisible(commandName, whenClauseContext);
        const newTitle = this.service.title(commandName);
        const newIcon = this.service.iconName(commandName);
        let newLabel = this.service.label(commandName);
        if (keymapService && keymapService.acceleratorExists(commandName)) {
            const accelerator = keymapService.getDefaultAccelerator(commandName);
            if (accelerator) {
                const label = (0, locale_1._)('%s (%s)', newLabel, accelerator);
                newLabel = newLabel ? label : accelerator;
            }
        }
        if (this.toolbarButtonCache_[commandName] &&
            this.toolbarButtonCache_[commandName].info.enabled === newEnabled &&
            this.toolbarButtonCache_[commandName].info.visible === newVisible &&
            this.toolbarButtonCache_[commandName].info.title === newTitle &&
            this.toolbarButtonCache_[commandName].info.iconName === newIcon &&
            this.toolbarButtonCache_[commandName].info.tooltip === newLabel) {
            return this.toolbarButtonCache_[commandName].info;
        }
        const output = {
            type: 'button',
            name: commandName,
            tooltip: newLabel,
            iconName: newIcon,
            enabled: newEnabled,
            visible: newVisible,
            onClick: async () => {
                await this.service.execute(commandName);
                void (0, focusEditorIfEditorCommand_1.default)(commandName, this.service);
            },
            title: newTitle,
        };
        this.toolbarButtonCache_[commandName] = {
            info: output,
        };
        return this.toolbarButtonCache_[commandName].info;
    }
    // This method ensures that if the provided commandNames and state hasn't changed
    // the output also won't change. Invididual toolbarButtonInfo also won't changed
    // if the state they use hasn't changed. This is to avoid useless renders of the toolbars.
    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- WhenClauseContext can be partial in tests
    commandsToToolbarButtons(commandNames, whenClauseContext, keymapService = null) {
        const output = [];
        for (const commandName of commandNames) {
            if (commandName === '-') {
                const lastItem = output.length > 0 && output[output.length - 1];
                if (lastItem !== exports.separatorItem) {
                    output.push(exports.separatorItem);
                }
                continue;
            }
            try {
                const button = this.commandToToolbarButton(commandName, whenClauseContext, keymapService);
                if (button.visible) {
                    output.push(button);
                }
            }
            catch (error) {
                logger.error('Unable to add toolbar button for command', commandName, '. Error: ', error);
            }
        }
        return reducer_1.stateUtils.selectArrayShallow({ array: output }, commandNames.join('_'));
    }
}
exports.default = ToolbarButtonUtils;
//# sourceMappingURL=ToolbarButtonUtils.js.map