import { useMdmTenant, useMdmUser, useMdmWorkflowPath } from '../../../contexts/MdmModuleContext';
import { canUserStartProcessInstanceFromQuery as checkCanUserStartProcessInstanceFromQuery, canUserViewWorkflowJobs as checkCanUserViewWorkflowJobs, canUserViewWorkflowEnvironmentConfigJar as checkCanUserViewWorkflowEnvironmentConfigJar, canUserViewWorkflowDefinition as checkCanUserViewWorkflowDefinition, canUserViewWorkflowConfigJar as checkCanUserViewWorkflowConfigJar, canUserViewProcessInstanceSearchQuery as checkCanUserViewProcessInstanceSearchQuery, canUserUpdateWorkflowDataTask as checkCanUserUpdateWorkflowDataTask, canUserUpdateProcessInstance as checkCanUserUpdateProcessInstance, canUserReadWorkflowDataTask as checkCanUserReadWorkflowDataTask, canUserManageWorkflowEnvironmentDefinition as checkCanUserManageWorkflowEnvironmentDefinition, canUserManageWorkflowDefinition as checkCanUserManageWorkflowDefinition, canUserExecuteWorkflowJobsValidateTasks as checkCanUserExecuteWorkflowJobsValidateTasks, canUserExecuteWorkflowJobsUpdateTasks as checkCanUserExecuteWorkflowJobsUpdateTasks, canUserExecuteWorkflowJobsTerminateProcessInstance as checkCanUserExecuteWorkflowJobsTerminateProcessInstance, canUserExecuteWorkflowJobsSyncBusinessProcessData as checkCanUserExecuteWorkflowJobsSyncBusinessProcessData, canUserExecuteWorkflowJobs as checkCanUserExecuteWorkflowJobs, canUserCreateWorkflowEnvironmentConfigJar as checkCanUserCreateWorkflowEnvironmentConfigJar, canUserRegisterConfigEnvironments as checkCanUserRegisterConfigEnvironments, canUserCreateWorkflowConfigJar as checkCanUserCreateWorkflowConfigJar } from '@reltio/mdm-sdk';
export var useWorkflowCheckPermission = function () {
    var tenant = useMdmTenant();
    var user = useMdmUser();
    var workflowPath = useMdmWorkflowPath();
    var canUserStartProcessFromQuery = checkCanUserStartProcessInstanceFromQuery(user, tenant);
    var canUserViewWorkflowJobs = checkCanUserViewWorkflowJobs(user, tenant);
    var canUserViewWorkflowEnvironmentConfigJar = checkCanUserViewWorkflowEnvironmentConfigJar(user, tenant);
    var canUserViewWorkflowDefinition = checkCanUserViewWorkflowDefinition(user, tenant);
    var canUserViewWorkflowConfigJar = checkCanUserViewWorkflowConfigJar(user, tenant);
    var canUserViewProcessInstanceSearchQuery = checkCanUserViewProcessInstanceSearchQuery(user, tenant);
    var canUserUpdateWorkflowDataTask = checkCanUserUpdateWorkflowDataTask(user, tenant);
    var canUserUpdateProcessInstance = checkCanUserUpdateProcessInstance(user, tenant);
    var canUserReadWorkflowDataTask = checkCanUserReadWorkflowDataTask(user, tenant);
    var canUserManageWorkflowEnvironmentDefinition = checkCanUserManageWorkflowEnvironmentDefinition(user, tenant);
    var canUserManageWorkflowDefinition = checkCanUserManageWorkflowDefinition(user, tenant);
    var canUserExecuteWorkflowJobsValidateTasks = checkCanUserExecuteWorkflowJobsValidateTasks(user, tenant);
    var canUserExecuteWorkflowJobsUpdateTasks = checkCanUserExecuteWorkflowJobsUpdateTasks(user, tenant);
    var canUserExecuteWorkflowJobsTerminateProcessInstance = checkCanUserExecuteWorkflowJobsTerminateProcessInstance(user, tenant);
    var canUserExecuteWorkflowJobsSyncBusinessProcessData = checkCanUserExecuteWorkflowJobsSyncBusinessProcessData(user, tenant);
    var canUserExecuteWorkflowJobs = checkCanUserExecuteWorkflowJobs(user, tenant);
    var canUserCreateWorkflowEnvironmentConfigJar = checkCanUserCreateWorkflowEnvironmentConfigJar(user, tenant);
    var canUserRegisterConfigEnvironments = checkCanUserRegisterConfigEnvironments(user, tenant);
    var canUserCreateWorkflowConfigJar = checkCanUserCreateWorkflowConfigJar(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;
};
