Liferay.Loader.define("app-builder-workflow-web@1.0.40/js/pages/apps/edit/configReducer.es", ['module', 'exports', 'require', 'app-builder-web/js/utils/lang.es', './utils.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = exports.getInitialConfig = exports.UPDATE_TABLE_VIEW = exports.UPDATE_STEP_INDEX = exports.UPDATE_STEP_FORM_VIEW_READONLY = exports.UPDATE_STEP_FORM_VIEW = exports.UPDATE_STEP_ACTION = exports.UPDATE_STEP = exports.UPDATE_LIST_ITEMS = exports.UPDATE_FORM_VIEW = exports.UPDATE_DATA_OBJECT = exports.UPDATE_CONFIG = exports.REMOVE_STEP_FORM_VIEW = exports.REMOVE_STEP_ACTION = exports.REMOVE_STEP = exports.REMOVE_STEP_EMPTY_FORM_VIEWS = exports.ADD_STEP_FORM_VIEW = exports.ADD_STEP_ACTION = exports.ADD_STEP = void 0;

    var _lang = require("app-builder-web/js/utils/lang.es");

    var _utils = require("./utils.es");

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

    function _nonIterableSpread() {
      throw new TypeError("Invalid attempt to spread non-iterable instance");
    }

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    var ADD_STEP = 'ADD_STEP';
    exports.ADD_STEP = ADD_STEP;
    var ADD_STEP_ACTION = 'ADD_STEP_ACTION';
    exports.ADD_STEP_ACTION = ADD_STEP_ACTION;
    var ADD_STEP_FORM_VIEW = 'ADD_STEP_FORM_VIEW';
    exports.ADD_STEP_FORM_VIEW = ADD_STEP_FORM_VIEW;
    var REMOVE_STEP_EMPTY_FORM_VIEWS = 'REMOVE_STEP_EMPTY_FORM_VIEWS';
    exports.REMOVE_STEP_EMPTY_FORM_VIEWS = REMOVE_STEP_EMPTY_FORM_VIEWS;
    var REMOVE_STEP = 'REMOVE_STEP';
    exports.REMOVE_STEP = REMOVE_STEP;
    var REMOVE_STEP_ACTION = 'REMOVE_STEP_ACTION';
    exports.REMOVE_STEP_ACTION = REMOVE_STEP_ACTION;
    var REMOVE_STEP_FORM_VIEW = 'REMOVE_STEP_FORM_VIEW';
    exports.REMOVE_STEP_FORM_VIEW = REMOVE_STEP_FORM_VIEW;
    var UPDATE_CONFIG = 'UPDATE_CONFIG';
    exports.UPDATE_CONFIG = UPDATE_CONFIG;
    var UPDATE_DATA_OBJECT = 'UPDATE_DATA_OBJECT';
    exports.UPDATE_DATA_OBJECT = UPDATE_DATA_OBJECT;
    var UPDATE_FORM_VIEW = 'UPDATE_FORM_VIEW';
    exports.UPDATE_FORM_VIEW = UPDATE_FORM_VIEW;
    var UPDATE_LIST_ITEMS = 'UPDATE_LIST_ITEMS';
    exports.UPDATE_LIST_ITEMS = UPDATE_LIST_ITEMS;
    var UPDATE_STEP = 'UPDATE_STEP';
    exports.UPDATE_STEP = UPDATE_STEP;
    var UPDATE_STEP_ACTION = 'UPDATE_STEP_ACTION';
    exports.UPDATE_STEP_ACTION = UPDATE_STEP_ACTION;
    var UPDATE_STEP_FORM_VIEW = 'UPDATE_STEP_FORM_VIEW';
    exports.UPDATE_STEP_FORM_VIEW = UPDATE_STEP_FORM_VIEW;
    var UPDATE_STEP_FORM_VIEW_READONLY = 'UPDATE_STEP_FORM_VIEW_READONLY';
    exports.UPDATE_STEP_FORM_VIEW_READONLY = UPDATE_STEP_FORM_VIEW_READONLY;
    var UPDATE_STEP_INDEX = 'UPDATE_STEP_INDEX';
    exports.UPDATE_STEP_INDEX = UPDATE_STEP_INDEX;
    var UPDATE_TABLE_VIEW = 'UPDATE_TABLE_VIEW';
    exports.UPDATE_TABLE_VIEW = UPDATE_TABLE_VIEW;

    var getInitialConfig = function getInitialConfig() {
      var defaultLanguageId = themeDisplay.getLanguageId();
      var initialSteps = [{
        appWorkflowTransitions: [{
          name: Liferay.Language.get('submit'),
          primary: true,
          transitionTo: Liferay.Language.get('final-step')
        }],
        initial: true,
        name: Liferay.Language.get('initial-step')
      }, {
        initial: false,
        name: Liferay.Language.get('final-step')
      }];
      return {
        currentStep: initialSteps[0],
        dataObject: {
          availableLanguageIds: [defaultLanguageId],
          defaultLanguageId: defaultLanguageId
        },
        draftConfig: {},
        formView: {},
        listItems: {
          assigneeRoles: [],
          dataObjects: [],
          fetching: false,
          formViews: [],
          tableViews: []
        },
        stepIndex: 0,
        steps: initialSteps,
        tableView: {}
      };
    };

    exports.getInitialConfig = getInitialConfig;

    var _default = function _default(state, action) {
      switch (action.type) {
        case ADD_STEP:
          {
            var _nextStep$name;

            var appWorkflowDataLayoutLinks = [];
            var stepIndex = action.stepIndex + 1;

            var workflowSteps = _toConsumableArray(state.steps);

            var finalStep = workflowSteps.pop();
            var nextStep = workflowSteps[stepIndex];
            var previousStep = workflowSteps[action.stepIndex];

            if (state.formView.id) {
              appWorkflowDataLayoutLinks.push(_objectSpread(_objectSpread({}, state.formView), {}, {
                dataLayoutId: state.formView.id,
                readOnly: true
              }));
            }

            var currentStep = {
              appWorkflowDataLayoutLinks: appWorkflowDataLayoutLinks,
              appWorkflowRoleAssignments: [],
              appWorkflowTransitions: [{
                name: Liferay.Language.get('submit'),
                primary: true,
                transitionTo: (_nextStep$name = nextStep === null || nextStep === void 0 ? void 0 : nextStep.name) !== null && _nextStep$name !== void 0 ? _nextStep$name : finalStep.name
              }],
              errors: {
                formViews: {
                  duplicatedFields: [],
                  errorIndexes: []
                }
              },
              name: (0, _lang.sub)(Liferay.Language.get('step-x'), [state.steps.length - 1])
            };

            if (stepIndex > 1) {
              previousStep.appWorkflowDataLayoutLinks = previousStep.appWorkflowDataLayoutLinks.filter(function (_ref) {
                var dataLayoutId = _ref.dataLayoutId;
                return dataLayoutId !== undefined;
              });
              currentStep.appWorkflowDataLayoutLinks = previousStep.appWorkflowDataLayoutLinks.map(function (dataLayout) {
                return _objectSpread(_objectSpread({}, dataLayout), {}, {
                  readOnly: true
                });
              });
              currentStep.errors.formViews = _objectSpread({}, previousStep.errors.formViews);
            }

            previousStep.appWorkflowTransitions.forEach(function (action) {
              if (action.primary) {
                action.transitionTo = currentStep.name;
              }
            });

            if (nextStep) {
              nextStep.appWorkflowTransitions.forEach(function (action) {
                if (!action.primary) {
                  action.transitionTo = currentStep.name;
                }
              });
            }

            workflowSteps.splice(stepIndex, 0, currentStep);
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: currentStep,
              stepIndex: stepIndex,
              steps: [].concat(_toConsumableArray(workflowSteps), [finalStep])
            });
          }

        case ADD_STEP_ACTION:
          {
            state.steps[state.stepIndex].appWorkflowTransitions.push({
              name: Liferay.Language.get('secondary-action'),
              primary: false,
              transitionTo: state.steps[state.stepIndex - 1].name
            });
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: state.steps[state.stepIndex]
            });
          }

        case ADD_STEP_FORM_VIEW:
          {
            state.steps[state.stepIndex].appWorkflowDataLayoutLinks.push({
              dataLayoutId: undefined,
              fields: [],
              readOnly: false
            });
            return _objectSpread({}, state);
          }

        case REMOVE_STEP:
          {
            var _nextStep$appWorkflow;

            var _currentStep;

            var _workflowSteps = _toConsumableArray(state.steps);

            var _previousStep = _workflowSteps[action.stepIndex - 1];
            var _nextStep = _workflowSteps[action.stepIndex + 1];
            _previousStep.appWorkflowTransitions[0].transitionTo = _nextStep.name;

            if ((_nextStep === null || _nextStep === void 0 ? void 0 : (_nextStep$appWorkflow = _nextStep.appWorkflowTransitions) === null || _nextStep$appWorkflow === void 0 ? void 0 : _nextStep$appWorkflow.length) > 1) {
              if (action.stepIndex > 1) {
                _nextStep.appWorkflowTransitions.forEach(function (transition) {
                  if (!transition.primary) {
                    transition.transitionTo = _previousStep.name;
                  }
                });
              } else {
                _nextStep.appWorkflowTransitions.forEach(function (transition, index) {
                  if (!transition.primary) {
                    _nextStep.appWorkflowTransitions.splice(index, 1);
                  }
                });
              }
            }

            if (state.stepIndex === action.stepIndex) {
              _currentStep = _previousStep;
            }

            _workflowSteps.splice(action.stepIndex, 1);

            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: _currentStep,
              stepIndex: state.stepIndex - 1,
              steps: _toConsumableArray(_workflowSteps)
            });
          }

        case REMOVE_STEP_ACTION:
          {
            state.steps[state.stepIndex].appWorkflowTransitions.pop();
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: state.steps[state.stepIndex]
            });
          }

        case REMOVE_STEP_EMPTY_FORM_VIEWS:
          {
            var _state$steps$action$s;

            state.steps[action.stepIndex].appWorkflowDataLayoutLinks = (_state$steps$action$s = state.steps[action.stepIndex].appWorkflowDataLayoutLinks) === null || _state$steps$action$s === void 0 ? void 0 : _state$steps$action$s.filter(function (_ref2) {
              var dataLayoutId = _ref2.dataLayoutId;
              return dataLayoutId !== undefined;
            });
            return _objectSpread({}, state);
          }

        case REMOVE_STEP_FORM_VIEW:
          {
            var _currentStep2 = state.steps[state.stepIndex];

            _currentStep2.appWorkflowDataLayoutLinks.splice(action.index, 1);

            _currentStep2.errors.formViews = (0, _utils.validateSelectedFormViews)(_currentStep2.appWorkflowDataLayoutLinks);
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: _currentStep2
            });
          }

        case UPDATE_CONFIG:
          {
            return _objectSpread(_objectSpread(_objectSpread({}, state), action.config), {}, {
              draftConfig: JSON.parse(JSON.stringify(action.config))
            });
          }

        case UPDATE_DATA_OBJECT:
          {
            state.steps.forEach(function (step) {
              var _step$errors;

              if (step.appWorkflowDataLayoutLinks) {
                step.appWorkflowDataLayoutLinks = [];
              }

              if (step === null || step === void 0 ? void 0 : (_step$errors = step.errors) === null || _step$errors === void 0 ? void 0 : _step$errors.formViews) {
                step.errors.formViews = {
                  duplicatedFields: [],
                  errorIndexes: []
                };
              }
            });
            return _objectSpread(_objectSpread({}, state), {}, {
              dataObject: action.dataObject,
              formView: {},
              tableView: {}
            });
          }

        case UPDATE_FORM_VIEW:
          {
            var _workflowSteps2 = _toConsumableArray(state.steps);

            var initialStep = _workflowSteps2.shift();

            var _finalStep = _workflowSteps2.pop();

            var formView = _objectSpread(_objectSpread({}, action.formView), {}, {
              fields: (0, _utils.getFormViewFields)(action.formView)
            });

            _workflowSteps2.forEach(function (step) {
              if (step.appWorkflowDataLayoutLinks.length === 0) {
                step.appWorkflowDataLayoutLinks.push(_objectSpread(_objectSpread({}, formView), {}, {
                  dataLayoutId: formView.id,
                  readOnly: true
                }));
              }
            });

            return _objectSpread(_objectSpread({}, state), {}, {
              formView: formView,
              steps: [initialStep].concat(_toConsumableArray(_workflowSteps2), [_finalStep])
            });
          }

        case UPDATE_LIST_ITEMS:
          {
            return _objectSpread(_objectSpread({}, state), {}, {
              listItems: _objectSpread(_objectSpread({}, state.listItems), action.listItems)
            });
          }

        case UPDATE_STEP:
          {
            var _action = _objectSpread({}, action),
                _currentStep3 = _action.step,
                _stepIndex = _action.stepIndex;

            if (_stepIndex > 0) {
              var _state$steps, _state$steps2, _previousStep2$appWor, _nextStep2$appWorkflo;

              var _previousStep2 = (_state$steps = state.steps) === null || _state$steps === void 0 ? void 0 : _state$steps[_stepIndex - 1];

              var _nextStep2 = (_state$steps2 = state.steps) === null || _state$steps2 === void 0 ? void 0 : _state$steps2[_stepIndex + 1];

              if (_previousStep2 === null || _previousStep2 === void 0 ? void 0 : (_previousStep2$appWor = _previousStep2.appWorkflowTransitions) === null || _previousStep2$appWor === void 0 ? void 0 : _previousStep2$appWor[0]) {
                _previousStep2.appWorkflowTransitions[0].transitionTo = _currentStep3.name;
              }

              if (_nextStep2 === null || _nextStep2 === void 0 ? void 0 : (_nextStep2$appWorkflo = _nextStep2.appWorkflowTransitions) === null || _nextStep2$appWorkflo === void 0 ? void 0 : _nextStep2$appWorkflo[1]) {
                _nextStep2.appWorkflowTransitions[1].transitionTo = _currentStep3.name;
              }
            }

            state.steps[_stepIndex] = _currentStep3;
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: _currentStep3
            });
          }

        case UPDATE_STEP_ACTION:
          {
            var name = action.name,
                primary = action.primary;
            var appWorkflowTransitions = state.steps[state.stepIndex].appWorkflowTransitions;
            var transitionIndex = appWorkflowTransitions.findIndex(function (transition) {
              return transition.primary === primary;
            });
            appWorkflowTransitions[transitionIndex].name = name;
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: state.steps[state.stepIndex]
            });
          }

        case UPDATE_STEP_FORM_VIEW:
          {
            var _currentStep4 = state.steps[state.stepIndex];
            var formViews = _currentStep4.appWorkflowDataLayoutLinks;
            formViews[action.index] = _objectSpread(_objectSpread({}, formViews[action.index]), {}, {
              dataLayoutId: action.formView.id,
              fields: (0, _utils.getFormViewFields)(action.formView),
              name: action.formView.name
            });
            _currentStep4.errors.formViews = (0, _utils.validateSelectedFormViews)(formViews);
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: _currentStep4
            });
          }

        case UPDATE_STEP_FORM_VIEW_READONLY:
          {
            var _currentStep5 = state.steps[state.stepIndex];
            _currentStep5.appWorkflowDataLayoutLinks[action.index] = _objectSpread(_objectSpread({}, _currentStep5.appWorkflowDataLayoutLinks[action.index]), {}, {
              readOnly: action.readOnly
            });
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: _currentStep5
            });
          }

        case UPDATE_STEP_INDEX:
          {
            return _objectSpread(_objectSpread({}, state), {}, {
              currentStep: state.steps[action.stepIndex],
              stepIndex: action.stepIndex
            });
          }

        case UPDATE_TABLE_VIEW:
          {
            return _objectSpread(_objectSpread({}, state), {}, {
              tableView: action.tableView
            });
          }

        default:
          {
            return state;
          }
      }
    };

    exports.default = _default;
    //# sourceMappingURL=configReducer.es.js.map
  }
});
//# sourceMappingURL=configReducer.es.js.map