import { genId } from './IdGenerator.js';
/**
 * An object representing a single menu item in the menu.
 */
export class MenuItem {
    type;
    target;
    visible;
    id;
    label;
    children;
    enabled;
    checked;
    execute;
    beforeRender;
    icon;
    title;
    constructor(init) {
        const { type = 'normal', target, visible = true, id = genId() } = init;
        this.type = type;
        this.target = target;
        this.visible = visible;
        this.id = id;
        if (['normal', 'radio'].includes(type)) {
            const { label, children, enabled, execute, icon, title, beforeRender, checked } = init;
            this.label = label;
            this.children = children;
            this.enabled = enabled;
            this.checked = checked;
            this.execute = execute;
            this.beforeRender = beforeRender;
            this.icon = icon;
            this.title = title;
        }
        else if (type === 'label') {
            const { label, beforeRender } = init;
            this.label = label;
            this.beforeRender = beforeRender;
        }
    }
    /**
     * @returns Whether this command has a sub-command.
     */
    get hasChildren() {
        const { children } = this;
        return Array.isArray(children) && !!children.length;
    }
    /**
     * Executes the `enabled()` function, when specified in the command options.
     *
     * @returns `true` when the command should be active in the UI.
     */
    isEnabled(store, target, workspace, customData) {
        let result = true;
        const { enabled, id } = this;
        if (typeof enabled === 'boolean') {
            result = enabled;
        }
        else if (typeof enabled === 'function') {
            try {
                result = enabled({
                    id,
                    store,
                    target,
                    root: workspace,
                    customData,
                });
            }
            catch (e) {
                // eslint-disable-next-line no-console
                console.warn(e);
            }
        }
        return result;
    }
    /**
     * Executes the `visible()` function, when specified in the command options.
     *
     * @returns `true` when the command should be rendered in the UI.
     */
    isVisible(store, target, workspace, customData) {
        let result = true;
        const { visible, id } = this;
        if (typeof visible === 'boolean') {
            result = visible;
        }
        else if (typeof visible === 'function') {
            try {
                result = visible({
                    id,
                    store,
                    target,
                    root: workspace,
                    customData,
                });
            }
            catch (e) {
                // eslint-disable-next-line no-console
                console.warn(e);
            }
        }
        return result;
    }
    /**
     * Executes the `checked()` function, when specified in the command options.
     *
     * @returns `true` when the command should be in the checked state
     */
    isChecked(store, target, workspace, customData) {
        const { type, checked, id } = this;
        if (type !== 'radio') {
            return false;
        }
        if (typeof checked === 'boolean') {
            return checked;
        }
        let result = false;
        if (typeof checked === 'function') {
            try {
                result = checked({
                    id,
                    store,
                    target,
                    root: workspace,
                    customData,
                });
            }
            catch (e) {
                // eslint-disable-next-line no-console
                console.warn(e);
            }
        }
        return result;
    }
    /**
     * Executes the `beforeRender()` function, when specified in the command options.
     */
    beforeRenderCallback(store, target, workspace, customData) {
        const { beforeRender, id } = this;
        if (!beforeRender) {
            return;
        }
        beforeRender({
            id,
            store,
            target,
            root: workspace,
            customData,
            menu: this,
        });
    }
    /**
     * Executes the `execute()` function, when specified in the command options.
     */
    trigger(item, store, target, workspace, clickPoint, customData, selectedSubcommand) {
        const { execute, id } = this;
        if (typeof execute !== 'function') {
            return;
        }
        execute({
            id,
            store,
            target,
            root: workspace,
            clickPoint,
            customData,
            selectedSubcommand,
            item,
        });
    }
}
//# sourceMappingURL=MenuItem.js.map