text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  var _actions = require('../actions');
  
  var _ = require('../../');
  
  function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  
  var queue = [];
  
  var executeOrQueue = function executeOrQueue(paused, func, isDimension) {
    if (!paused) {
      func();
      return;
    }
  
    isDimension ? queue.unshift(func) : queue.push(func);
  };
  
  var middleware = function middleware(client, dataLayer) {
    return function (store) {
      return function (next) {
        return function (action) {
          var returnValue = next(action);
  
          var _store$getState = store.getState(),
              analytics = _store$getState.analytics;
  
          var _ref = analytics || {},
              paused = _ref.paused;
  
          if (!(action && client)) return returnValue;
  
          if (action.type === _actions.SET_GA_CLIENT_OPTIONS) {
            if (action.options && Object.keys(action.options).length) {
              executeOrQueue(paused, function () {
                return client.setOptions(action.options);
              }, true);
            }
          } else if (action.type === _actions.SET_GA_CUSTOM_DIMENSIONS) {
            if (action.dimensions && Object.keys(action.dimensions).length) {
              executeOrQueue(paused, function () {
                return client.setOptions(action.dimensions);
              }, true);
            }
          } else if (action.type === _actions.VALIDATE_AND_SET_DIMENSIONS) {
            if (action.dimensions && Object.keys(action.dimensions).length) {
              executeOrQueue(paused, function () {
                return client.setOptions(action.dimensions);
              }, true);
            }
          } else if (action.type === _actions.LOG_PAGEVIEW) {
            if (action.page) {
              executeOrQueue(paused, function () {
                return client.logPageView(action.page, action.dimensions);
              });
            } else {
              executeOrQueue(paused, function () {
                return client.legacyPageView();
              });
            }
          } else if (action.type === _actions.ADD_IMPRESSION) {
            executeOrQueue(paused, function () {
              return client.addImpression(action.data);
            });
          } else if (action.type === _actions.LOG_TIMING) {
            executeOrQueue(paused, function () {
              return client.logTiming(action.data);
            });
          } else if (action.type === _actions.RESUME) {
            if (paused === 0) {
              [].concat(_toConsumableArray(queue)).forEach(function (func) {
                return func();
              });
              queue = [];
            }
          } else {
            if (action.meta && action.meta.dataLayer && dataLayer) {
              dataLayer.push(action.meta.dataLayer);
            }
            if (action.meta && action.meta.commerce) {
              var commerceAction = action.meta.commerce.action;
              if (commerceAction === _.constants.ECOMMERCE_ACTION_TYPE_ADD) {
                executeOrQueue(paused, function () {
                  return client.setEnhancedEcommerceAddProduct(action.meta.commerce);
                });
              } else {
                executeOrQueue(paused, function () {
                  return client.setEnhancedEcommerceProductDetail(action.meta.commerce);
                });
              }
            }
            if (action.meta && action.meta.analytics) {
              executeOrQueue(paused, function () {
                return client.logEvent(action.meta.analytics);
              });
            }
          }
  
          return returnValue;
        };
      };
    };
  };
  
  exports.default = middleware;