"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWorkflowCheckPermission = void 0;
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useWorkflowCheckPermission = function () {
    var tenant = (0, MdmModuleContext_1.useMdmTenant)();
    var user = (0, MdmModuleContext_1.useMdmUser)();
    var workflowPath = (0, MdmModuleContext_1.useMdmWorkflowPath)();
    var canUserStartProcessFromQuery = (0, mdm_sdk_1.canUserStartProcessInstanceFromQuery)(user, tenant);
    var canUserViewWorkflowJobs = (0, mdm_sdk_1.canUserViewWorkflowJobs)(user, tenant);
    var canUserViewWorkflowEnvironmentConfigJar = (0, mdm_sdk_1.canUserViewWorkflowEnvironmentConfigJar)(user, tenant);
    var canUserViewWorkflowDefinition = (0, mdm_sdk_1.canUserViewWorkflowDefinition)(user, tenant);
    var canUserViewWorkflowConfigJar = (0, mdm_sdk_1.canUserViewWorkflowConfigJar)(user, tenant);
    var canUserViewProcessInstanceSearchQuery = (0, mdm_sdk_1.canUserViewProcessInstanceSearchQuery)(user, tenant);
    var canUserUpdateWorkflowDataTask = (0, mdm_sdk_1.canUserUpdateWorkflowDataTask)(user, tenant);
    var canUserUpdateProcessInstance = (0, mdm_sdk_1.canUserUpdateProcessInstance)(user, tenant);
    var canUserReadWorkflowDataTask = (0, mdm_sdk_1.canUserReadWorkflowDataTask)(user, tenant);
    var canUserManageWorkflowEnvironmentDefinition = (0, mdm_sdk_1.canUserManageWorkflowEnvironmentDefinition)(user, tenant);
    var canUserManageWorkflowDefinition = (0, mdm_sdk_1.canUserManageWorkflowDefinition)(user, tenant);
    var canUserExecuteWorkflowJobsValidateTasks = (0, mdm_sdk_1.canUserExecuteWorkflowJobsValidateTasks)(user, tenant);
    var canUserExecuteWorkflowJobsUpdateTasks = (0, mdm_sdk_1.canUserExecuteWorkflowJobsUpdateTasks)(user, tenant);
    var canUserExecuteWorkflowJobsTerminateProcessInstance = (0, mdm_sdk_1.canUserExecuteWorkflowJobsTerminateProcessInstance)(user, tenant);
    var canUserExecuteWorkflowJobsSyncBusinessProcessData = (0, mdm_sdk_1.canUserExecuteWorkflowJobsSyncBusinessProcessData)(user, tenant);
    var canUserExecuteWorkflowJobs = (0, mdm_sdk_1.canUserExecuteWorkflowJobs)(user, tenant);
    var canUserCreateWorkflowEnvironmentConfigJar = (0, mdm_sdk_1.canUserCreateWorkflowEnvironmentConfigJar)(user, tenant);
    var canUserRegisterConfigEnvironments = (0, mdm_sdk_1.canUserRegisterConfigEnvironments)(user, tenant);
    var canUserCreateWorkflowConfigJar = (0, mdm_sdk_1.canUserCreateWorkflowConfigJar)(user, tenant);
    var checkWorkflowPermission = function (permission) {
        if (!workflowPath)
            return false;
        switch (permission) {
            case 'VIEW_PROCESS_DEFINITIONS': {
                return canUserViewWorkflowDefinition;
            }
            case 'DEPLOY_GENERIC_PROCESS_DEFINITION': {
                return canUserManageWorkflowEnvironmentDefinition;
            }
            case 'VIEW_JOBS': {
                return canUserViewWorkflowJobs;
            }
            case 'START_JOB': {
                return (canUserExecuteWorkflowJobs ||
                    (canUserExecuteWorkflowJobsSyncBusinessProcessData &&
                        canUserExecuteWorkflowJobsTerminateProcessInstance &&
                        canUserExecuteWorkflowJobsUpdateTasks &&
                        canUserExecuteWorkflowJobsValidateTasks));
            }
            case 'VIEW_CUSTOM_JARS': {
                return canUserViewWorkflowConfigJar || canUserViewWorkflowEnvironmentConfigJar;
            }
            case 'UPDATE_PROCESS_INSTANCE': {
                return canUserUpdateProcessInstance;
            }
            case 'VIEW_TASKS': {
                return canUserReadWorkflowDataTask;
            }
            case 'REGISTER_TENANT': {
                return canUserRegisterConfigEnvironments;
            }
            case 'START_PROCESS_INSTANCE': {
                return canUserStartProcessFromQuery;
            }
            case 'VIEW_PROCESS_INSTANCE_COMMENTS': {
                return canUserViewProcessInstanceSearchQuery;
            }
            case 'VIEW_PROCESS_INSTANCES': {
                return canUserViewProcessInstanceSearchQuery;
            }
            case 'UPDATE_TASK': {
                return canUserUpdateWorkflowDataTask;
            }
            case 'DEPLOY_TENANT_PROCESS_DEFINITION': {
                return canUserManageWorkflowDefinition;
            }
            case 'DEPLOY_CUSTOM_JAR': {
                return canUserCreateWorkflowConfigJar && canUserCreateWorkflowEnvironmentConfigJar;
            }
            case 'START_PROCESS_INSTANCE_SEARCH_QUERY': {
                return canUserStartProcessFromQuery;
            }
            case 'VIEW_PROCESS_INSTANCE_COUNT': {
                return canUserViewProcessInstanceSearchQuery;
            }
        }
        return false;
    };
    return checkWorkflowPermission;
};
exports.useWorkflowCheckPermission = useWorkflowCheckPermission;
