"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowCommandPaletteActionProvider = void 0;
/********************************************************************************
 * Copyright (c) 2022-2023 STMicroelectronics and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/
const server_1 = require("@eclipse-glsp/server");
const inversify_1 = require("inversify");
const graph_extension_1 = require("../graph-extension");
const model_types_1 = require("../util/model-types");
let WorkflowCommandPaletteActionProvider = class WorkflowCommandPaletteActionProvider extends server_1.CommandPaletteActionProvider {
    getPaletteActions(selectedElementIds, selectedElements, position, args) {
        const actions = [];
        if (this.modelState.isReadonly) {
            return actions;
        }
        const index = this.modelState.index;
        // Create actions
        const location = position !== null && position !== void 0 ? position : server_1.Point.ORIGIN;
        actions.push({
            label: 'Create Automated Task',
            actions: [server_1.CreateNodeOperation.create(model_types_1.ModelTypes.AUTOMATED_TASK, { location })],
            icon: 'fa-plus-square'
        }, {
            label: 'Create Manual Task',
            actions: [server_1.CreateNodeOperation.create(model_types_1.ModelTypes.MANUAL_TASK, { location })],
            icon: 'fa-plus-square'
        }, {
            label: 'Create Merge Node',
            actions: [server_1.CreateNodeOperation.create(model_types_1.ModelTypes.MERGE_NODE, { location })],
            icon: 'fa-plus-square'
        }, {
            label: 'Create Decision Node',
            actions: [server_1.CreateNodeOperation.create(model_types_1.ModelTypes.DECISION_NODE, { location })],
            icon: 'fa-plus-square'
        }, {
            label: 'Create Category',
            actions: [server_1.CreateNodeOperation.create(model_types_1.ModelTypes.CATEGORY, { location })],
            icon: 'fa-plus-square'
        });
        // Create edge action between two nodes
        if (selectedElements.length === 1) {
            const element = selectedElements[0];
            if (element instanceof server_1.GNode) {
                actions.push(...this.createEdgeActions(element, index.getAllByClass(graph_extension_1.TaskNode)));
            }
        }
        else if (selectedElements.length === 2) {
            const source = selectedElements[0];
            const target = selectedElements[1];
            if (source instanceof graph_extension_1.TaskNode && target instanceof graph_extension_1.TaskNode) {
                actions.push(this.createEdgeAction(`Create Edge from ${this.getLabel(source)} to ${this.getLabel(target)}`, source, target));
                actions.push(this.createWeightedEdgeAction(`Create Weighted Edge from ${this.getLabel(source)} to ${this.getLabel(target)}`, source, target));
            }
        }
        // Delete action
        const label = selectedElementIds.length === 1 ? 'Delete' : 'Delete All';
        actions.push({ label, actions: [server_1.DeleteElementOperation.create(selectedElementIds)], icon: 'fa-minus-square' });
        return actions;
    }
    createEdgeActions(source, targets) {
        const actions = [];
        targets.forEach(node => actions.push(this.createEdgeAction(`Create Edge to ${this.getLabel(node)}`, source, node)));
        targets.forEach(node => actions.push(this.createWeightedEdgeAction(`Create Weighted Edge to ${this.getLabel(node)}`, source, node)));
        return actions;
    }
    createWeightedEdgeAction(label, source, node) {
        return {
            label,
            actions: [
                server_1.CreateEdgeOperation.create({
                    elementTypeId: model_types_1.ModelTypes.WEIGHTED_EDGE,
                    sourceElementId: source.id,
                    targetElementId: node.id
                })
            ],
            icon: 'fa-plus-square'
        };
    }
    createEdgeAction(label, source, node) {
        return {
            label,
            actions: [
                server_1.CreateEdgeOperation.create({ elementTypeId: server_1.DefaultTypes.EDGE, sourceElementId: source.id, targetElementId: node.id })
            ],
            icon: 'fa-plus-square'
        };
    }
    getLabel(node) {
        if (node instanceof graph_extension_1.TaskNode) {
            return node.name;
        }
        return node.id;
    }
};
__decorate([
    (0, inversify_1.inject)(server_1.ModelState),
    __metadata("design:type", Object)
], WorkflowCommandPaletteActionProvider.prototype, "modelState", void 0);
WorkflowCommandPaletteActionProvider = __decorate([
    (0, inversify_1.injectable)()
], WorkflowCommandPaletteActionProvider);
exports.WorkflowCommandPaletteActionProvider = WorkflowCommandPaletteActionProvider;
//# sourceMappingURL=workflow-command-palette-action-provider.js.map