"use strict";
/********************************************************************************
 * Copyright (c) 2023 EclipseSource 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
 ********************************************************************************/
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EditTaskOperationHandler = exports.EditTaskOperation = void 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");
var EditTaskOperation;
(function (EditTaskOperation) {
    EditTaskOperation.KIND = 'editTask';
    function is(object) {
        return (server_1.Action.hasKind(object, EditTaskOperation.KIND) &&
            (0, server_1.hasStringProp)(object, 'taskId') &&
            (0, server_1.hasStringProp)(object, 'feature') &&
            (0, server_1.hasStringProp)(object, 'value'));
    }
    EditTaskOperation.is = is;
    function create(options) {
        return Object.assign({ kind: EditTaskOperation.KIND, isOperation: true }, options);
    }
    EditTaskOperation.create = create;
})(EditTaskOperation = exports.EditTaskOperation || (exports.EditTaskOperation = {}));
let EditTaskOperationHandler = class EditTaskOperationHandler extends server_1.GModelOperationHandler {
    constructor() {
        super(...arguments);
        this.operationType = EditTaskOperation.KIND;
    }
    createCommand(operation) {
        const task = (0, server_1.getOrThrow)(this.modelState.index.findByClass(operation.taskId, graph_extension_1.TaskNode), `Cannot find task with id '${operation.taskId}'`);
        switch (operation.feature) {
            case 'duration': {
                const duration = Number.parseInt(operation.value, 10);
                return duration !== task.duration //
                    ? this.commandOf(() => this.editDuration(task, duration))
                    : undefined;
            }
            case 'taskType': {
                return task.taskType !== operation.value //
                    ? this.commandOf(() => this.editTaskType(task, operation.value))
                    : undefined;
            }
        }
    }
    editDuration(task, duration) {
        task.duration = duration;
    }
    editTaskType(task, type) {
        task.taskType = type;
        if (type === 'manual' || type === 'automated') {
            const temp = this.createTempTask(type, task);
            const toAssign = {
                taskType: temp.taskType,
                type: temp.type,
                children: temp.children,
                cssClasses: temp.cssClasses
            };
            Object.assign(task, toAssign);
            return;
        }
        throw new Error(`Could not edit task '${task.id}'. Invalid type: ${type}`);
    }
    createTempTask(type, task) {
        return graph_extension_1.TaskNode.builder() //
            .type(type === 'automated' ? model_types_1.ModelTypes.AUTOMATED_TASK : model_types_1.ModelTypes.MANUAL_TASK)
            .taskType(type)
            .name(task.name)
            .addCssClass(type)
            .children()
            .build();
    }
};
EditTaskOperationHandler = __decorate([
    (0, inversify_1.injectable)()
], EditTaskOperationHandler);
exports.EditTaskOperationHandler = EditTaskOperationHandler;
//# sourceMappingURL=edit-task-operation-handler.js.map