Liferay.Loader.define("segments-experiment-web@2.0.19/js/state/reducer.es", ['module', 'exports', 'require'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.reducer = reducer;

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

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

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    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;
      }
    }

    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;
    }

    /**
     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
     *
     * The contents of this file are subject to the terms of the Liferay Enterprise
     * Subscription License ("License"). You may not use this file except in
     * compliance with the License. You can obtain a copy of the License by
     * contacting Liferay, Inc. See the License for the specific language governing
     * permissions and limitations under the License, including but not limited to
     * distribution rights of the Software.
     */
    function reducer(state, action) {
      switch (action.type) {
        case 'ADD_EXPERIMENT':
          return _objectSpread({}, state, {
            experiment: action.payload
          });

        case 'ADD_VARIANT':
          return _objectSpread({}, state, {
            variants: [].concat(_toConsumableArray(state.variants), [action.payload])
          });

        case 'ARCHIVE_EXPERIMENT':
          return _objectSpread({}, state, {
            errors: {},
            experiment: null,
            experimentHistory: [_objectSpread({}, state.experiment, {
              status: action.payload.status
            })].concat(_toConsumableArray(state.experimentHistory)),
            variants: [],
            viewExperimentURL: undefined
          });

        case 'CREATE_EXPERIMENT_FINISH':
          return _objectSpread({}, state, {
            createExperimentModal: {
              active: false
            }
          });

        case 'CREATE_EXPERIMENT_START':
          return _createExperimentStart(state, action.payload);

        case 'DELETE_ARCHIVED_EXPERIMENT':
          return _objectSpread({}, state, {
            experimentHistory: state.experimentHistory.filter(function (experiment) {
              return experiment.segmentsExperimentId !== action.payload.experimentId;
            })
          });

        case 'EDIT_EXPERIMENT':
          return _editExperiment(state, action.payload);

        case 'EDIT_EXPERIMENT_FINISH':
          return _objectSpread({}, state, {
            editExperimentModal: {
              active: false
            }
          });

        case 'EDIT_EXPERIMENT_START':
          return _editExperienceStart(state, action.payload);

        case 'REVIEW_AND_RUN_EXPERIMENT':
          return _reviewAndRunExperiment(state);

        case 'REVIEW_AND_RUN_EXPERIMENT_FINISH':
          return _objectSpread({}, state, {
            reviewExperimentModal: {
              active: false
            }
          });

        case 'REVIEW_CLICK_TARGET_ELEMENT':
          return _reviewClickTargetElement(state);

        case 'REVIEW_VARIANTS':
          return _reviewVariants(state);

        case 'RUN_EXPERIMENT':
          return _runExperiment(state, action.payload);

        case 'UPDATE_SEGMENTS_EXPERIMENT_TARGET':
          return _editExperiment(state, action.payload);

        case 'UPDATE_EXPERIMENT_STATUS':
          return _updateExperimentStatus(state, action.payload);

        case 'UPDATE_VARIANT':
          return _objectSpread({}, state, {
            variants: state.variants.map(function (variant) {
              if (action.payload.variantId === variant.segmentsExperimentRelId) {
                return _objectSpread({}, variant, {}, action.payload.changes);
              }

              return variant;
            })
          });

        case 'UPDATE_VARIANTS':
          return _objectSpread({}, state, {
            variants: action.payload
          });

        default:
          return state;
      }
    }

    function _createExperimentStart(state) {
      var experimentModalState = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var selectedExperienceId = state.selectedExperienceId;
      var description = experimentModalState.description,
          error = experimentModalState.error,
          name = experimentModalState.name,
          segmentsExperienceId = experimentModalState.segmentsExperienceId;
      return _objectSpread({}, state, {
        createExperimentModal: {
          active: true,
          description: description,
          error: error,
          name: name,
          segmentsExperienceId: segmentsExperienceId ? segmentsExperienceId : selectedExperienceId
        }
      });
    }

    function _editExperiment(state, updatedValues) {
      return _objectSpread({}, state, {
        experiment: _objectSpread({}, state.experiment, {}, updatedValues)
      });
    }

    function _editExperienceStart(state) {
      var experiementModalState = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var experiment = state.experiment;
      var description = experiementModalState.description,
          editable = experiementModalState.editable,
          error = experiementModalState.error,
          goal = experiementModalState.goal,
          name = experiementModalState.name,
          segmentsEntryName = experiementModalState.segmentsEntryName,
          segmentsExperienceId = experiementModalState.segmentsExperienceId,
          segmentsExperimentId = experiementModalState.segmentsExperimentId,
          status = experiementModalState.status;
      return _objectSpread({}, state, {
        editExperimentModal: {
          active: true,
          description: description ? description : experiment.description,
          editable: editable ? editable : experiment.editable,
          error: error ? error : experiment.error,
          goal: goal ? goal : experiment.goal,
          name: name ? name : experiment.name,
          segmentsEntryName: segmentsEntryName ? segmentsEntryName : experiment.segmentsEntryName,
          segmentsExperienceId: segmentsExperienceId ? segmentsExperienceId : experiment.segmentsExperienceId,
          segmentsExperimentId: segmentsExperimentId ? segmentsExperimentId : experiment.segmentsExperimentId,
          status: status ? status : experiment.status
        }
      });
    }

    function _reviewAndRunExperiment(state) {
      var newState = _objectSpread({}, state);

      newState.errors = _objectSpread({}, newState.errors, {
        clickTargetError: state.experiment.goal.value === 'click' && !state.experiment.goal.target,
        variantsError: newState.variants.length <= 1
      });
      var errors = Object.entries(newState.errors);

      for (var i = 0; i < errors.length; i++) {
        var _errors$i = _slicedToArray(errors[i], 2),
            hasError = _errors$i[1];

        if (hasError) {
          return _objectSpread({}, newState, {
            reviewExperimentModal: {
              active: false
            }
          });
        }
      }

      return _objectSpread({}, newState, {
        reviewExperimentModal: {
          active: true
        }
      });
    }

    function _reviewClickTargetElement(state) {
      var newState = _objectSpread({}, state);

      newState.errors = _objectSpread({}, newState.errors, {
        clickTargetError: state.experiment.goal.value === 'click' && !state.experiment.goal.target
      });
      return newState;
    }

    function _reviewVariants(state) {
      var newState = _objectSpread({}, state);

      newState.errors = _objectSpread({}, newState.errors, {
        variantsError: state.variants.length <= 1
      });
      return newState;
    }

    function _runExperiment(state, _ref) {
      var experiment = _ref.experiment,
          splitVariantsMap = _ref.splitVariantsMap;
      var variants = state.variants.map(function (variant) {
        return _objectSpread({}, variant, {
          split: splitVariantsMap[variant.segmentsExperimentRelId]
        });
      });
      return _objectSpread({}, state, {
        error: {},
        experiment: _objectSpread({}, state.experiment, {}, experiment),
        variants: variants
      });
    }

    function _updateExperimentStatus(state, updatedValues) {
      return _objectSpread({}, state, {
        errors: {},
        experiment: _objectSpread({}, state.experiment, {}, updatedValues),
        variants: []
      });
    }
    //# sourceMappingURL=reducer.es.js.map
  }
});
//# sourceMappingURL=reducer.es.js.map