/**
 * DevExtreme (cjs/ui/gantt/ui.gantt.actions.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.GanttActionsManager = void 0;
var _renderer = _interopRequireDefault(require("../../core/renderer"));
var _element = require("../../core/element");
var _extend = require("../../core/utils/extend");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const Actions = {
    onContextMenuPreparing: "onContextMenuPreparing",
    onCustomCommand: "onCustomCommand",
    onDependencyDeleted: "onDependencyDeleted",
    onDependencyDeleting: "onDependencyDeleting",
    onDependencyInserted: "onDependencyInserted",
    onDependencyInserting: "onDependencyInserting",
    onResourceAssigned: "onResourceAssigned",
    onResourceAssigning: "onResourceAssigning",
    onResourceDeleted: "onResourceDeleted",
    onResourceDeleting: "onResourceDeleting",
    onResourceInserted: "onResourceInserted",
    onResourceInserting: "onResourceInserting",
    onResourceManagerDialogShowing: "onResourceManagerDialogShowing",
    onResourceUnassigned: "onResourceUnassigned",
    onResourceUnassigning: "onResourceUnassigning",
    onSelectionChanged: "onSelectionChanged",
    onTaskClick: "onTaskClick",
    onTaskDblClick: "onTaskDblClick",
    onTaskDeleted: "onTaskDeleted",
    onTaskDeleting: "onTaskDeleting",
    onTaskEditDialogShowing: "onTaskEditDialogShowing",
    onTaskInserted: "onTaskInserted",
    onTaskInserting: "onTaskInserting",
    onTaskMoving: "onTaskMoving",
    onTaskUpdated: "onTaskUpdated",
    onTaskUpdating: "onTaskUpdating",
    onScaleCellPrepared: "onScaleCellPrepared"
};
const GANTT_TASKS = "tasks";
const GANTT_DEPENDENCIES = "dependencies";
const GANTT_RESOURCES = "resources";
const GANTT_RESOURCE_ASSIGNMENTS = "resourceAssignments";
const GANTT_NEW_TASK_CACHE_KEY = "gantt_new_task_key";
let GanttActionsManager = function() {
    function GanttActionsManager(gantt) {
        this._gantt = gantt;
        this._mappingHelper = gantt._mappingHelper;
        this._customFieldsManager = gantt._customFieldsManager
    }
    var _proto = GanttActionsManager.prototype;
    _proto._createActionByOption = function(optionName) {
        return this._gantt._createActionByOption(optionName)
    };
    _proto._getTaskData = function(key) {
        return this._gantt.getTaskData(key)
    };
    _proto._convertCoreToMappedData = function(optionName, coreData) {
        return this._mappingHelper.convertCoreToMappedData(optionName, coreData)
    };
    _proto._convertMappedToCoreData = function(optionName, mappedData) {
        return this._mappingHelper.convertMappedToCoreData(optionName, mappedData)
    };
    _proto._convertMappedToCoreFields = function(optionName, fields) {
        return this._mappingHelper.convertMappedToCoreFields(optionName, fields)
    };
    _proto._convertCoreToMappedFields = function(optionName, fields) {
        return this._mappingHelper.convertCoreToMappedFields(optionName, fields)
    };
    _proto._saveCustomFieldsDataToCache = function(key, data) {
        let forceUpdateOnKeyExpire = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : false;
        let isCustomFieldsUpdateOnly = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : false;
        this._customFieldsManager.saveCustomFieldsDataToCache(key, data, forceUpdateOnKeyExpire, isCustomFieldsUpdateOnly)
    };
    _proto.createTaskDblClickAction = function() {
        this._taskDblClickAction = this._createActionByOption(Actions.onTaskDblClick)
    };
    _proto.taskDblClickAction = function(args) {
        if (!this._taskDblClickAction) {
            this.createTaskDblClickAction()
        }
        this._taskDblClickAction(args)
    };
    _proto.raiseTaskDblClickAction = function(key, event) {
        const args = {
            cancel: false,
            data: this._getTaskData(key),
            event: event,
            key: key
        };
        this.taskDblClickAction(args);
        return !args.cancel
    };
    _proto.createTaskClickAction = function() {
        this._taskClickAction = this._createActionByOption(Actions.onTaskClick)
    };
    _proto.taskClickAction = function(args) {
        if (!this._taskClickAction) {
            this.createTaskClickAction()
        }
        this._taskClickAction(args)
    };
    _proto.raiseTaskClickAction = function(key, event) {
        const args = {
            key: key,
            event: event,
            data: this._getTaskData(key)
        };
        this.taskClickAction(args)
    };
    _proto.createSelectionChangedAction = function() {
        this._selectionChangedAction = this._createActionByOption(Actions.onSelectionChanged)
    };
    _proto.selectionChangedAction = function(args) {
        if (!this._selectionChangedAction) {
            this.createSelectionChangedAction()
        }
        this._selectionChangedAction(args)
    };
    _proto.raiseSelectionChangedAction = function(selectedRowKey) {
        this.selectionChangedAction({
            selectedRowKey: selectedRowKey
        })
    };
    _proto.createCustomCommandAction = function() {
        this._customCommandAction = this._createActionByOption(Actions.onCustomCommand)
    };
    _proto.customCommandAction = function(args) {
        if (!this._customCommandAction) {
            this.createCustomCommandAction()
        }
        this._customCommandAction(args)
    };
    _proto.raiseCustomCommand = function(commandName) {
        this.customCommandAction({
            name: commandName
        })
    };
    _proto.createContextMenuPreparingAction = function() {
        this._contextMenuPreparingAction = this._createActionByOption(Actions.onContextMenuPreparing)
    };
    _proto.contextMenuPreparingAction = function(args) {
        if (!this._contextMenuPreparingAction) {
            this.createContextMenuPreparingAction()
        }
        this._contextMenuPreparingAction(args)
    };
    _proto.raiseContextMenuPreparing = function(options) {
        this.contextMenuPreparingAction(options)
    };
    _proto._getInsertingAction = function(optionName) {
        switch (optionName) {
            case "tasks":
                return this._getTaskInsertingAction();
            case "dependencies":
                return this._getDependencyInsertingAction();
            case "resources":
                return this._getResourceInsertingAction();
            case "resourceAssignments":
                return this._getResourceAssigningAction()
        }
        return () => {}
    };
    _proto.raiseInsertingAction = function(optionName, coreArgs) {
        const action = this._getInsertingAction(optionName);
        if (action) {
            const args = {
                cancel: false,
                values: this._convertCoreToMappedData(optionName, coreArgs.values)
            };
            action(args);
            coreArgs.cancel = args.cancel;
            (0, _extend.extend)(coreArgs.values, this._convertMappedToCoreData(optionName, args.values));
            if ("tasks" === optionName) {
                this._saveCustomFieldsDataToCache("gantt_new_task_key", args.values)
            }
        }
    };
    _proto.createTaskInsertingAction = function() {
        this._taskInsertingAction = this._createActionByOption(Actions.onTaskInserting)
    };
    _proto.taskInsertingAction = function(args) {
        const action = this._getTaskInsertingAction();
        action(args)
    };
    _proto._getTaskInsertingAction = function() {
        if (!this._taskInsertingAction) {
            this.createTaskInsertingAction()
        }
        return this._taskInsertingAction
    };
    _proto.createDependencyInsertingAction = function() {
        this._dependencyInsertingAction = this._createActionByOption(Actions.onDependencyInserting)
    };
    _proto.dependencyInsertingAction = function(args) {
        const action = this._getDependencyInsertingAction();
        action(args)
    };
    _proto._getDependencyInsertingAction = function() {
        if (!this._dependencyInsertingAction) {
            this.createDependencyInsertingAction()
        }
        return this._dependencyInsertingAction
    };
    _proto.createResourceInsertingAction = function() {
        this._resourceInsertingAction = this._createActionByOption(Actions.onResourceInserting)
    };
    _proto.resourceInsertingAction = function(args) {
        const action = this._getResourceInsertingAction();
        action(args)
    };
    _proto._getResourceInsertingAction = function() {
        if (!this._resourceInsertingAction) {
            this.createResourceInsertingAction()
        }
        return this._resourceInsertingAction
    };
    _proto.createResourceAssigningAction = function() {
        this._resourceAssigningAction = this._createActionByOption(Actions.onResourceAssigning)
    };
    _proto.resourceAssigningAction = function(args) {
        const action = this._getResourceAssigningAction();
        action(args)
    };
    _proto._getResourceAssigningAction = function() {
        if (!this._resourceAssigningAction) {
            this.createResourceAssigningAction()
        }
        return this._resourceAssigningAction
    };
    _proto._getInsertedAction = function(optionName) {
        switch (optionName) {
            case "tasks":
                return this._getTaskInsertedAction();
            case "dependencies":
                return this._getDependencyInsertedAction();
            case "resources":
                return this._getResourceInsertedAction();
            case "resourceAssignments":
                return this._getResourceAssignedAction()
        }
        return () => {}
    };
    _proto.raiseInsertedAction = function(optionName, data, key) {
        const action = this._getInsertedAction(optionName);
        if (action) {
            const args = {
                values: data,
                key: key
            };
            action(args)
        }
    };
    _proto.createTaskInsertedAction = function() {
        this._taskInsertedAction = this._createActionByOption(Actions.onTaskInserted)
    };
    _proto.taskInsertedAction = function(args) {
        const action = this._getTaskInsertedAction();
        action(args)
    };
    _proto._getTaskInsertedAction = function() {
        if (!this._taskInsertedAction) {
            this.createTaskInsertedAction()
        }
        return this._taskInsertedAction
    };
    _proto.createDependencyInsertedAction = function() {
        this._dependencyInsertedAction = this._createActionByOption(Actions.onDependencyInserted)
    };
    _proto.dependencyInsertedAction = function(args) {
        const action = this._getDependencyInsertedAction();
        action(args)
    };
    _proto._getDependencyInsertedAction = function() {
        if (!this._dependencyInsertedAction) {
            this.createDependencyInsertedAction()
        }
        return this._dependencyInsertedAction
    };
    _proto.createResourceInsertedAction = function() {
        this._resourceInsertedAction = this._createActionByOption(Actions.onResourceInserted)
    };
    _proto.resourceInsertedAction = function(args) {
        const action = this._getResourceInsertedAction();
        action(args)
    };
    _proto._getResourceInsertedAction = function() {
        if (!this._resourceInsertedAction) {
            this.createResourceInsertedAction()
        }
        return this._resourceInsertedAction
    };
    _proto.createResourceAssignedAction = function() {
        this._resourceAssignedAction = this._createActionByOption(Actions.onResourceAssigned)
    };
    _proto.resourceAssignedAction = function(args) {
        const action = this._getResourceAssignedAction();
        action(args)
    };
    _proto._getResourceAssignedAction = function() {
        if (!this._resourceAssignedAction) {
            this.createResourceAssignedAction()
        }
        return this._resourceAssignedAction
    };
    _proto._getDeletingAction = function(optionName) {
        switch (optionName) {
            case "tasks":
                return this._getTaskDeletingAction();
            case "dependencies":
                return this._getDependencyDeletingAction();
            case "resources":
                return this._getResourceDeletingAction();
            case "resourceAssignments":
                return this._getResourceUnassigningAction()
        }
        return () => {}
    };
    _proto.raiseDeletingAction = function(optionName, coreArgs) {
        const action = this._getDeletingAction(optionName);
        if (action) {
            const args = {
                cancel: false,
                key: coreArgs.key,
                values: this._convertCoreToMappedData(optionName, coreArgs.values)
            };
            action(args);
            coreArgs.cancel = args.cancel
        }
    };
    _proto.createTaskDeletingAction = function() {
        this._taskDeletingAction = this._createActionByOption(Actions.onTaskDeleting)
    };
    _proto.taskDeletingAction = function(args) {
        const action = this._getTaskDeletingAction();
        action(args)
    };
    _proto._getTaskDeletingAction = function() {
        if (!this._taskDeletingAction) {
            this.createTaskDeletingAction()
        }
        return this._taskDeletingAction
    };
    _proto.createDependencyDeletingAction = function() {
        this._dependencyDeletingAction = this._createActionByOption(Actions.onDependencyDeleting)
    };
    _proto.dependencyDeletingAction = function(args) {
        const action = this._getDependencyDeletingAction();
        action(args)
    };
    _proto._getDependencyDeletingAction = function() {
        if (!this._dependencyDeletingAction) {
            this.createDependencyDeletingAction()
        }
        return this._dependencyDeletingAction
    };
    _proto.createResourceDeletingAction = function() {
        this._resourceDeletingAction = this._createActionByOption(Actions.onResourceDeleting)
    };
    _proto.resourceDeletingAction = function(args) {
        const action = this._getResourceDeletingAction();
        action(args)
    };
    _proto._getResourceDeletingAction = function() {
        if (!this._resourceDeletingAction) {
            this.createResourceDeletingAction()
        }
        return this._resourceDeletingAction
    };
    _proto.createResourceUnassigningAction = function() {
        this._resourceUnassigningAction = this._createActionByOption(Actions.onResourceUnassigning)
    };
    _proto.resourceUnassigningAction = function(args) {
        const action = this._getResourceUnassigningAction();
        action(args)
    };
    _proto._getResourceUnassigningAction = function() {
        if (!this._resourceUnassigningAction) {
            this.createResourceUnassigningAction()
        }
        return this._resourceUnassigningAction
    };
    _proto._getDeletedAction = function(optionName) {
        switch (optionName) {
            case "tasks":
                return this._getTaskDeletedAction();
            case "dependencies":
                return this._getDependencyDeletedAction();
            case "resources":
                return this._getResourceDeletedAction();
            case "resourceAssignments":
                return this._getResourceUnassignedAction()
        }
        return () => {}
    };
    _proto.raiseDeletedAction = function(optionName, key, data) {
        const action = this._getDeletedAction(optionName);
        if (action) {
            const args = {
                key: key,
                values: data
            };
            action(args)
        }
    };
    _proto.createTaskDeletedAction = function() {
        this._taskDeletedAction = this._createActionByOption(Actions.onTaskDeleted)
    };
    _proto.taskDeletedAction = function(args) {
        const action = this._getTaskDeletedAction();
        action(args)
    };
    _proto._getTaskDeletedAction = function() {
        if (!this._taskDeletedAction) {
            this.createTaskDeletedAction()
        }
        return this._taskDeletedAction
    };
    _proto.createDependencyDeletedAction = function() {
        this._dependencyDeletedAction = this._createActionByOption(Actions.onDependencyDeleted)
    };
    _proto.dependencyDeletedAction = function(args) {
        const action = this._getDependencyDeletedAction();
        action(args)
    };
    _proto._getDependencyDeletedAction = function() {
        if (!this._dependencyDeletedAction) {
            this.createDependencyDeletedAction()
        }
        return this._dependencyDeletedAction
    };
    _proto.createResourceDeletedAction = function() {
        this._resourceDeletedAction = this._createActionByOption(Actions.onResourceDeleted)
    };
    _proto.resourceDeletedAction = function(args) {
        const action = this._getResourceDeletedAction();
        action(args)
    };
    _proto._getResourceDeletedAction = function() {
        if (!this._resourceDeletedAction) {
            this.createResourceDeletedAction()
        }
        return this._resourceDeletedAction
    };
    _proto.createResourceUnassignedAction = function() {
        this._resourceUnassignedAction = this._createActionByOption(Actions.onResourceUnassigned)
    };
    _proto.resourceUnassignedAction = function(args) {
        const action = this._getResourceUnassignedAction();
        action(args)
    };
    _proto._getResourceUnassignedAction = function() {
        if (!this._resourceUnassignedAction) {
            this.createResourceUnassignedAction()
        }
        return this._resourceUnassignedAction
    };
    _proto._getUpdatingAction = function(optionName) {
        switch (optionName) {
            case "tasks":
                return this._getTaskUpdatingAction()
        }
        return () => {}
    };
    _proto.raiseUpdatingAction = function(optionName, coreArgs, action) {
        action = action || this._getUpdatingAction(optionName);
        if (action) {
            const isTaskUpdating = "tasks" === optionName;
            const args = {
                cancel: false,
                key: coreArgs.key,
                newValues: this._convertCoreToMappedData(optionName, coreArgs.newValues),
                values: isTaskUpdating ? this._getTaskData(coreArgs.key) : this._convertCoreToMappedData(optionName, coreArgs.values)
            };
            if (isTaskUpdating && this._customFieldsManager.cache.hasData(args.key)) {
                this._customFieldsManager.addCustomFieldsDataFromCache(args.key, args.newValues)
            }
            action(args);
            coreArgs.cancel = args.cancel;
            (0, _extend.extend)(coreArgs.newValues, this._convertMappedToCoreData(optionName, args.newValues));
            if (isTaskUpdating) {
                if (args.cancel) {
                    this._customFieldsManager.resetCustomFieldsDataCache(args.key)
                } else {
                    const forceUpdateOnKeyExpire = !Object.keys(coreArgs.newValues).length;
                    this._saveCustomFieldsDataToCache(args.key, args.newValues, forceUpdateOnKeyExpire)
                }
            }
        }
    };
    _proto.createTaskUpdatingAction = function() {
        this._taskUpdatingAction = this._createActionByOption(Actions.onTaskUpdating)
    };
    _proto.taskUpdatingAction = function(args) {
        const action = this._getTaskUpdatingAction();
        action(args)
    };
    _proto._getTaskUpdatingAction = function() {
        if (!this._taskUpdatingAction) {
            this.createTaskUpdatingAction()
        }
        return this._taskUpdatingAction
    };
    _proto._getUpdatedAction = function(optionName) {
        switch (optionName) {
            case "tasks":
                return this._getTaskUpdatedAction()
        }
        return () => {}
    };
    _proto.raiseUpdatedAction = function(optionName, data, key) {
        const action = this._getUpdatedAction(optionName);
        if (action) {
            const args = {
                values: data,
                key: key
            };
            action(args)
        }
    };
    _proto.createTaskUpdatedAction = function() {
        this._taskUpdatedAction = this._createActionByOption(Actions.onTaskUpdated)
    };
    _proto.taskUpdatedAction = function(args) {
        const action = this._getTaskUpdatedAction();
        action(args)
    };
    _proto._getTaskUpdatedAction = function() {
        if (!this._taskUpdatedAction) {
            this.createTaskUpdatedAction()
        }
        return this._taskUpdatedAction
    };
    _proto.createTaskEditDialogShowingAction = function() {
        this._taskEditDialogShowingAction = this._createActionByOption(Actions.onTaskEditDialogShowing)
    };
    _proto.taskEditDialogShowingAction = function(args) {
        const action = this._getTaskEditDialogShowingAction();
        action(args)
    };
    _proto._getTaskEditDialogShowingAction = function() {
        if (!this._taskEditDialogShowingAction) {
            this.createTaskEditDialogShowingAction()
        }
        return this._taskEditDialogShowingAction
    };
    _proto.raiseTaskEditDialogShowingAction = function(coreArgs) {
        const action = this._getTaskEditDialogShowingAction();
        if (action) {
            const args = {
                cancel: false,
                key: coreArgs.key,
                values: this._convertCoreToMappedData("tasks", coreArgs.values),
                readOnlyFields: this._convertCoreToMappedFields("tasks", coreArgs.readOnlyFields),
                hiddenFields: this._convertCoreToMappedFields("tasks", coreArgs.hiddenFields)
            };
            action(args);
            coreArgs.cancel = args.cancel;
            (0, _extend.extend)(coreArgs.values, this._convertMappedToCoreData("tasks", args.values));
            coreArgs.readOnlyFields = this._convertMappedToCoreFields("tasks", args.readOnlyFields);
            coreArgs.hiddenFields = this._convertMappedToCoreFields("tasks", args.hiddenFields)
        }
    };
    _proto.createResourceManagerDialogShowingAction = function() {
        this._resourceManagerDialogShowingAction = this._createActionByOption(Actions.onResourceManagerDialogShowing)
    };
    _proto.resourceManagerDialogShowingAction = function(args) {
        const action = this._getResourceManagerDialogShowingAction();
        action(args)
    };
    _proto._getResourceManagerDialogShowingAction = function() {
        if (!this._resourceManagerDialogShowingAction) {
            this.createResourceManagerDialogShowingAction()
        }
        return this._resourceManagerDialogShowingAction
    };
    _proto.raiseResourceManagerDialogShowingAction = function(coreArgs) {
        const action = this._getResourceManagerDialogShowingAction();
        if (action) {
            const mappedResources = coreArgs.values.resources.items.map(r => this._convertMappedToCoreData("resources", r));
            const args = {
                cancel: false,
                values: mappedResources
            };
            action(args);
            coreArgs.cancel = args.cancel
        }
    };
    _proto.createTaskMovingAction = function() {
        this._taskMovingAction = this._createActionByOption(Actions.onTaskMoving)
    };
    _proto.taskMovingAction = function(args) {
        const action = this.getTaskMovingAction();
        action(args)
    };
    _proto.getTaskMovingAction = function() {
        if (!this._taskMovingAction) {
            this.createTaskMovingAction()
        }
        return this._taskMovingAction
    };
    _proto.getScaleCellPreparedAction = function() {
        if (!this._scaleCellPreparedAction) {
            this.createScaleCellPreparedAction()
        }
        return this._scaleCellPreparedAction
    };
    _proto.createScaleCellPreparedAction = function() {
        this._scaleCellPreparedAction = this._createActionByOption(Actions.onScaleCellPrepared)
    };
    _proto.raiseScaleCellPreparedAction = function(data) {
        const action = this.getScaleCellPreparedAction();
        if (action) {
            const args = {
                scaleIndex: data.scaleIndex,
                scaleType: this._getScaleType(data.scaleType),
                scaleElement: (0, _element.getPublicElement)((0, _renderer.default)(data.scaleElement)),
                separatorElement: (0, _element.getPublicElement)((0, _renderer.default)(data.separatorElement)),
                startDate: new Date(data.start),
                endDate: new Date(data.end)
            };
            action(args)
        }
    };
    _proto._getScaleType = function(viewType) {
        switch (viewType) {
            case 0:
                return "minutes";
            case 1:
                return "hours";
            case 2:
                return "sixHours";
            case 3:
                return "days";
            case 4:
                return "weeks";
            case 5:
                return "months";
            case 6:
                return "quarters";
            case 7:
                return "years";
            case 8:
                return "fiveYears";
            default:
                return
        }
    };
    return GanttActionsManager
}();
exports.GanttActionsManager = GanttActionsManager;
