import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { RunOnceScheduler } from '../../../base/common/async.js';
import { Emitter, DebounceEmitter } from '../../../base/common/event.js';
import { DisposableStore } from '../../../base/common/lifecycle.js';
import { MenuRegistry, isIMenuItem, MenuItemAction, SubmenuItemAction, isISubmenuItem } from './actions.js';
import { ICommandService } from '../../commands/common/commands.service.js';
import { IContextKeyService } from '../../contextkey/common/contextkey.service.js';
import { toAction, Separator } from '../../../base/common/actions.js';
import { StorageScope, StorageTarget } from '../../storage/common/storage.js';
import { IStorageService } from '../../storage/common/storage.service.js';
import { removeFastWithoutKeepingOrder } from '../../../base/common/arrays.js';
import { localizeWithPath } from '../../../nls.js';
import { IKeybindingService } from '../../keybinding/common/keybinding.service.js';
var PersistedMenuHideState_1, MenuInfo_1;
const _moduleId = "vs/platform/actions/common/menuService";
let MenuService = class MenuService {
    constructor(_commandService, _keybindingService, storageService) {
        this._commandService = _commandService;
        this._keybindingService = _keybindingService;
        this._hiddenStates = ( (new PersistedMenuHideState(storageService)));
    }
    createMenu(id, contextKeyService, options) {
        return (
             (new MenuImpl(
                id,
                this._hiddenStates,
                { emitEventsForSubmenuChanges: false, eventDebounceDelay: 50, ...options },
                this._commandService,
                this._keybindingService,
                contextKeyService
            ))
        );
    }
    resetHiddenStates(ids) {
        this._hiddenStates.reset(ids);
    }
};
MenuService = ( (__decorate([
    ( (__param(0, ICommandService))),
    ( (__param(1, IKeybindingService))),
    ( (__param(2, IStorageService)))
], MenuService)));
let PersistedMenuHideState = class PersistedMenuHideState {
    static { PersistedMenuHideState_1 = this; }
    static { this._key = 'menu.hiddenCommands'; }
    constructor(_storageService) {
        this._storageService = _storageService;
        this._disposables = ( (new DisposableStore()));
        this._onDidChange = ( (new Emitter()));
        this.onDidChange = this._onDidChange.event;
        this._ignoreChangeEvent = false;
        this._hiddenByDefaultCache = ( (new Map()));
        try {
            const raw = _storageService.get(PersistedMenuHideState_1._key, StorageScope.PROFILE, '{}');
            this._data = JSON.parse(raw);
        }
        catch (err) {
            this._data = Object.create(null);
        }
        this._disposables.add(_storageService.onDidChangeValue(StorageScope.PROFILE, PersistedMenuHideState_1._key, this._disposables)(() => {
            if (!this._ignoreChangeEvent) {
                try {
                    const raw = _storageService.get(PersistedMenuHideState_1._key, StorageScope.PROFILE, '{}');
                    this._data = JSON.parse(raw);
                }
                catch (err) {
                    console.log('FAILED to read storage after UPDATE', err);
                }
            }
            this._onDidChange.fire();
        }));
    }
    dispose() {
        this._onDidChange.dispose();
        this._disposables.dispose();
    }
    _isHiddenByDefault(menu, commandId) {
        return this._hiddenByDefaultCache.get(`${menu.id}/${commandId}`) ?? false;
    }
    setDefaultState(menu, commandId, hidden) {
        this._hiddenByDefaultCache.set(`${menu.id}/${commandId}`, hidden);
    }
    isHidden(menu, commandId) {
        const hiddenByDefault = this._isHiddenByDefault(menu, commandId);
        const state = this._data[menu.id]?.includes(commandId) ?? false;
        return hiddenByDefault ? !state : state;
    }
    updateHidden(menu, commandId, hidden) {
        const hiddenByDefault = this._isHiddenByDefault(menu, commandId);
        if (hiddenByDefault) {
            hidden = !hidden;
        }
        const entries = this._data[menu.id];
        if (!hidden) {
            if (entries) {
                const idx = entries.indexOf(commandId);
                if (idx >= 0) {
                    removeFastWithoutKeepingOrder(entries, idx);
                }
                if (entries.length === 0) {
                    delete this._data[menu.id];
                }
            }
        }
        else {
            if (!entries) {
                this._data[menu.id] = [commandId];
            }
            else {
                const idx = entries.indexOf(commandId);
                if (idx < 0) {
                    entries.push(commandId);
                }
            }
        }
        this._persist();
    }
    reset(menus) {
        if (menus === undefined) {
            this._data = Object.create(null);
            this._persist();
        }
        else {
            for (const { id } of menus) {
                if (this._data[id]) {
                    delete this._data[id];
                }
            }
            this._persist();
        }
    }
    _persist() {
        try {
            this._ignoreChangeEvent = true;
            const raw = JSON.stringify(this._data);
            this._storageService.store(PersistedMenuHideState_1._key, raw, StorageScope.PROFILE, StorageTarget.USER);
        }
        finally {
            this._ignoreChangeEvent = false;
        }
    }
};
PersistedMenuHideState = PersistedMenuHideState_1 = ( (__decorate([
    ( (__param(0, IStorageService)))
], PersistedMenuHideState)));
let MenuInfo = MenuInfo_1 = class MenuInfo {
    constructor(_id, _hiddenStates, _collectContextKeysForSubmenus, _commandService, _keybindingService, _contextKeyService) {
        this._id = _id;
        this._hiddenStates = _hiddenStates;
        this._collectContextKeysForSubmenus = _collectContextKeysForSubmenus;
        this._commandService = _commandService;
        this._keybindingService = _keybindingService;
        this._contextKeyService = _contextKeyService;
        this._menuGroups = [];
        this._structureContextKeys = ( (new Set()));
        this._preconditionContextKeys = ( (new Set()));
        this._toggledContextKeys = ( (new Set()));
        this.refresh();
    }
    get structureContextKeys() {
        return this._structureContextKeys;
    }
    get preconditionContextKeys() {
        return this._preconditionContextKeys;
    }
    get toggledContextKeys() {
        return this._toggledContextKeys;
    }
    refresh() {
        this._menuGroups.length = 0;
        this._structureContextKeys.clear();
        this._preconditionContextKeys.clear();
        this._toggledContextKeys.clear();
        const menuItems = MenuRegistry.getMenuItems(this._id);
        let group;
        menuItems.sort(MenuInfo_1._compareMenuItems);
        for (const item of menuItems) {
            const groupName = item.group || '';
            if (!group || group[0] !== groupName) {
                group = [groupName, []];
                this._menuGroups.push(group);
            }
            group[1].push(item);
            this._collectContextKeys(item);
        }
    }
    _collectContextKeys(item) {
        MenuInfo_1._fillInKbExprKeys(item.when, this._structureContextKeys);
        if (isIMenuItem(item)) {
            if (item.command.precondition) {
                MenuInfo_1._fillInKbExprKeys(item.command.precondition, this._preconditionContextKeys);
            }
            if (item.command.toggled) {
                const toggledExpression = item.command.toggled.condition || item.command.toggled;
                MenuInfo_1._fillInKbExprKeys(toggledExpression, this._toggledContextKeys);
            }
        }
        else if (this._collectContextKeysForSubmenus) {
            MenuRegistry.getMenuItems(item.submenu).forEach(this._collectContextKeys, this);
        }
    }
    createActionGroups(options) {
        const result = [];
        for (const group of this._menuGroups) {
            const [id, items] = group;
            let activeActions;
            for (const item of items) {
                if (this._contextKeyService.contextMatchesRules(item.when)) {
                    const isMenuItem = isIMenuItem(item);
                    if (isMenuItem) {
                        this._hiddenStates.setDefaultState(this._id, item.command.id, !!item.isHiddenByDefault);
                    }
                    const menuHide = createMenuHide(this._id, isMenuItem ? item.command : item, this._hiddenStates);
                    if (isMenuItem) {
                        const menuKeybinding = createConfigureKeybindingAction(this._commandService, this._keybindingService, item.command.id, item.when);
                        (activeActions ??= []).push(( (new MenuItemAction(
                            item.command,
                            item.alt,
                            options,
                            menuHide,
                            menuKeybinding,
                            this._contextKeyService,
                            this._commandService
                        ))));
                    }
                    else {
                        const groups = ( (new MenuInfo_1(
                            item.submenu,
                            this._hiddenStates,
                            this._collectContextKeysForSubmenus,
                            this._commandService,
                            this._keybindingService,
                            this._contextKeyService
                        ))).createActionGroups(options);
                        const submenuActions = Separator.join(...( (groups.map(g => g[1]))));
                        if (submenuActions.length > 0) {
                            (activeActions ??= []).push(( (new SubmenuItemAction(item, menuHide, submenuActions))));
                        }
                    }
                }
            }
            if (activeActions && activeActions.length > 0) {
                result.push([id, activeActions]);
            }
        }
        return result;
    }
    static _fillInKbExprKeys(exp, set) {
        if (exp) {
            for (const key of ( (exp.keys()))) {
                set.add(key);
            }
        }
    }
    static _compareMenuItems(a, b) {
        const aGroup = a.group;
        const bGroup = b.group;
        if (aGroup !== bGroup) {
            if (!aGroup) {
                return 1;
            }
            else if (!bGroup) {
                return -1;
            }
            if (aGroup === 'navigation') {
                return -1;
            }
            else if (bGroup === 'navigation') {
                return 1;
            }
            const value = aGroup.localeCompare(bGroup);
            if (value !== 0) {
                return value;
            }
        }
        const aPrio = a.order || 0;
        const bPrio = b.order || 0;
        if (aPrio < bPrio) {
            return -1;
        }
        else if (aPrio > bPrio) {
            return 1;
        }
        return MenuInfo_1._compareTitles(isIMenuItem(a) ? a.command.title : a.title, isIMenuItem(b) ? b.command.title : b.title);
    }
    static _compareTitles(a, b) {
        const aStr = typeof a === 'string' ? a : a.original;
        const bStr = typeof b === 'string' ? b : b.original;
        return aStr.localeCompare(bStr);
    }
};
MenuInfo = MenuInfo_1 = ( (__decorate([
    ( (__param(3, ICommandService))),
    ( (__param(4, IKeybindingService))),
    ( (__param(5, IContextKeyService)))
], MenuInfo)));
let MenuImpl = class MenuImpl {
    constructor(id, hiddenStates, options, commandService, keybindingService, contextKeyService) {
        this._disposables = ( (new DisposableStore()));
        this._menuInfo = ( (new MenuInfo(
            id,
            hiddenStates,
            options.emitEventsForSubmenuChanges,
            commandService,
            keybindingService,
            contextKeyService
        )));
        const rebuildMenuSoon = ( (new RunOnceScheduler(() => {
            this._menuInfo.refresh();
            this._onDidChange.fire({ menu: this, isStructuralChange: true, isEnablementChange: true, isToggleChange: true });
        }, options.eventDebounceDelay)));
        this._disposables.add(rebuildMenuSoon);
        this._disposables.add(MenuRegistry.onDidChangeMenu(e => {
            if (( (e.has(id)))) {
                rebuildMenuSoon.schedule();
            }
        }));
        const lazyListener = this._disposables.add(( (new DisposableStore())));
        const merge = (events) => {
            let isStructuralChange = false;
            let isEnablementChange = false;
            let isToggleChange = false;
            for (const item of events) {
                isStructuralChange = isStructuralChange || item.isStructuralChange;
                isEnablementChange = isEnablementChange || item.isEnablementChange;
                isToggleChange = isToggleChange || item.isToggleChange;
                if (isStructuralChange && isEnablementChange && isToggleChange) {
                    break;
                }
            }
            return { menu: this, isStructuralChange, isEnablementChange, isToggleChange };
        };
        const startLazyListener = () => {
            lazyListener.add(contextKeyService.onDidChangeContext(e => {
                const isStructuralChange = e.affectsSome(this._menuInfo.structureContextKeys);
                const isEnablementChange = e.affectsSome(this._menuInfo.preconditionContextKeys);
                const isToggleChange = e.affectsSome(this._menuInfo.toggledContextKeys);
                if (isStructuralChange || isEnablementChange || isToggleChange) {
                    this._onDidChange.fire({ menu: this, isStructuralChange, isEnablementChange, isToggleChange });
                }
            }));
            lazyListener.add(hiddenStates.onDidChange(e => {
                this._onDidChange.fire({ menu: this, isStructuralChange: true, isEnablementChange: false, isToggleChange: false });
            }));
        };
        this._onDidChange = ( (new DebounceEmitter({
            onWillAddFirstListener: startLazyListener,
            onDidRemoveLastListener: lazyListener.clear.bind(lazyListener),
            delay: options.eventDebounceDelay,
            merge
        })));
        this.onDidChange = this._onDidChange.event;
    }
    getActions(options) {
        return this._menuInfo.createActionGroups(options);
    }
    dispose() {
        this._disposables.dispose();
        this._onDidChange.dispose();
    }
};
MenuImpl = ( (__decorate([
    ( (__param(3, ICommandService))),
    ( (__param(4, IKeybindingService))),
    ( (__param(5, IContextKeyService)))
], MenuImpl)));
function createMenuHide(menu, command, states) {
    const id = isISubmenuItem(command) ? command.submenu.id : command.id;
    const title = typeof command.title === 'string' ? command.title : command.title.value;
    const hide = toAction({
        id: `hide/${menu.id}/${id}`,
        label: ( localizeWithPath(_moduleId, 0, 'Hide \'{0}\'', title)),
        run() { states.updateHidden(menu, id, true); }
    });
    const toggle = toAction({
        id: `toggle/${menu.id}/${id}`,
        label: title,
        get checked() { return !states.isHidden(menu, id); },
        run() { states.updateHidden(menu, id, !!this.checked); }
    });
    return {
        hide,
        toggle,
        get isHidden() { return !toggle.checked; },
    };
}
function createConfigureKeybindingAction(commandService, keybindingService, commandId, when = undefined, enabled = true) {
    return toAction({
        id: `configureKeybinding/${commandId}`,
        label: ( localizeWithPath(_moduleId, 1, "Configure Keybinding")),
        enabled,
        run() {
            const hasKeybinding = !!keybindingService.lookupKeybinding(commandId);
            const whenValue = !hasKeybinding && when ? when.serialize() : undefined;
            commandService.executeCommand('workbench.action.openGlobalKeybindings', `@command:${commandId}` + (whenValue ? ` +when:${whenValue}` : ''));
        }
    });
}
export { MenuService, createConfigureKeybindingAction };
