"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.searchStores = exports.getStore = exports.searchProducts = exports.getProducts = exports.getProduct = exports.getCategories = exports.getCategory = exports.getCustomer = exports.logout = exports.login = exports.registerCustomer = exports.getOrders = exports.getOrder = exports.createOrder = exports.deleteCart = exports.getPaymentMethods = exports.getShippingMethods = exports.setShippingMethod = exports.setShippingAddress = exports.setBillingAddress = exports.setCustomerInformation = exports.setPayment = exports.removeCouponEntry = exports.addCouponEntry = exports.removeCartItem = exports.updateCartItem = exports.addCartItem = exports.getCart = exports.createCart = exports.receiveStoreSearchData = exports.receiveStoreData = exports.receiveProductSearchData = exports.receiveProductData = exports.receiveOrderData = exports.receiveCustomerData = exports.receiveCategoryData = exports.receiveCartPaymentMethods = exports.receiveCartShippingMethods = exports.receiveCartData = void 0;

var _jsonStableStringify = _interopRequireDefault(require("json-stable-stringify"));

var _reduxActions = require("redux-actions");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(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; }

var receiveCartData = (0, _reduxActions.createAction)('Receive Cart Data', function (payload) {
  return payload;
});
exports.receiveCartData = receiveCartData;
var receiveCartShippingMethods = (0, _reduxActions.createAction)('Receive Cart Shipping Methods Data', function (payload) {
  return payload;
});
exports.receiveCartShippingMethods = receiveCartShippingMethods;
var receiveCartPaymentMethods = (0, _reduxActions.createAction)('Receive Cart Payment Methods Data', function (payload) {
  return payload;
});
exports.receiveCartPaymentMethods = receiveCartPaymentMethods;
var receiveCategoryData = (0, _reduxActions.createAction)('Receive Category Data', function (payload) {
  return payload;
});
exports.receiveCategoryData = receiveCategoryData;
var receiveCustomerData = (0, _reduxActions.createAction)('Receive Customer Data', function (payload) {
  return payload;
});
exports.receiveCustomerData = receiveCustomerData;
var receiveOrderData = (0, _reduxActions.createAction)('Receive Order Data', function (payload) {
  return payload;
});
exports.receiveOrderData = receiveOrderData;
var receiveProductData = (0, _reduxActions.createAction)('Receive Product Data', function (payload) {
  return payload;
});
exports.receiveProductData = receiveProductData;
var receiveProductSearchData = (0, _reduxActions.createAction)('Receive Product Search Data', function (payload) {
  return payload;
});
exports.receiveProductSearchData = receiveProductSearchData;
var receiveStoreData = (0, _reduxActions.createAction)('Receive Store Data', function (payload) {
  return payload;
});
exports.receiveStoreData = receiveStoreData;
var receiveStoreSearchData = (0, _reduxActions.createAction)('Receive Store Search Data', function (payload) {
  return payload;
});
exports.receiveStoreSearchData = receiveStoreSearchData;

var createCart = function createCart(oldCart, opts) {
  return function (dispatch, _, _ref) {
    var api = _ref.api;
    return api.createCart(oldCart, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.createCart = createCart;

var getCart = function getCart(cartId, opts) {
  return function (dispatch, _, _ref2) {
    var api = _ref2.api;
    return api.getCart(cartId, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.getCart = getCart;

var addCartItem = function addCartItem(cart, cartItem, opts) {
  return function (dispatch, _, _ref3) {
    var api = _ref3.api;
    return api.addCartItem(cart, cartItem, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.addCartItem = addCartItem;

var updateCartItem = function updateCartItem(cart, cartItem, opts) {
  return function (dispatch, _, _ref4) {
    var api = _ref4.api;
    return api.updateCartItem(cart, cartItem, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.updateCartItem = updateCartItem;

var removeCartItem = function removeCartItem(cart, cartItemId, opts) {
  return function (dispatch, _, _ref5) {
    var api = _ref5.api;
    return api.removeCartItem(cart, cartItemId, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.removeCartItem = removeCartItem;

var addCouponEntry = function addCouponEntry(cart, couponEntry, opts) {
  return function (dispatch, _, _ref6) {
    var api = _ref6.api;
    return api.addCouponEntry(cart, couponEntry, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.addCouponEntry = addCouponEntry;

var removeCouponEntry = function removeCouponEntry(cart, couponEntryId, opts) {
  return function (dispatch, _, _ref7) {
    var api = _ref7.api;
    return api.removeCouponEntry(cart, couponEntryId, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.removeCouponEntry = removeCouponEntry;

var setPayment = function setPayment(cart, payment, opts) {
  return function (dispatch, _, _ref8) {
    var api = _ref8.api;
    return api.setPayment(cart, payment, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.setPayment = setPayment;

var setCustomerInformation = function setCustomerInformation(cart, customerInformation, opts) {
  return function (dispatch, _, _ref9) {
    var api = _ref9.api;
    return api.setCustomerInformation(cart, customerInformation, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.setCustomerInformation = setCustomerInformation;

var setBillingAddress = function setBillingAddress(cart, billingAddress, opts) {
  return function (dispatch, _, _ref10) {
    var api = _ref10.api;
    return api.setBillingAddress(cart, billingAddress, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.setBillingAddress = setBillingAddress;

var setShippingAddress = function setShippingAddress(cart, shippingAddress, opts) {
  return function (dispatch, _, _ref11) {
    var api = _ref11.api;
    return api.setShippingAddress(cart, shippingAddress, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.setShippingAddress = setShippingAddress;

var setShippingMethod = function setShippingMethod(cart, shippingMethod, opts) {
  return function (dispatch, _, _ref12) {
    var api = _ref12.api;
    return api.setShippingMethod(cart, shippingMethod, opts).then(function (cart) {
      return dispatch(receiveCartData(cart));
    });
  };
};

exports.setShippingMethod = setShippingMethod;

var getShippingMethods = function getShippingMethods(cart, opts) {
  return function (dispatch, _, _ref13) {
    var api = _ref13.api;
    return api.getShippingMethods(cart, opts).then(function (shippingMethods) {
      return dispatch(receiveCartShippingMethods(shippingMethods));
    });
  };
};

exports.getShippingMethods = getShippingMethods;

var getPaymentMethods = function getPaymentMethods(cart, opts) {
  return function (dispatch, _, _ref14) {
    var api = _ref14.api;
    return api.getPaymentMethods(cart, opts).then(function (paymentMethods) {
      return dispatch(receiveCartPaymentMethods(paymentMethods));
    });
  };
};

exports.getPaymentMethods = getPaymentMethods;

var deleteCart = function deleteCart(id, opts) {
  return function (dispatch, _, _ref15) {
    var api = _ref15.api;
    return api.deleteCart(id, opts).then(function () {
      return dispatch(receiveCartData(null));
    });
  };
};

exports.deleteCart = deleteCart;

var createOrder = function createOrder(cart, opts) {
  return function (dispatch, _, _ref16) {
    var api = _ref16.api;
    return api.createOrder(cart, opts).then(function (order) {
      return dispatch(receiveOrderData(_defineProperty({}, order.id, order)));
    }).then(function () {
      // Some e-comm backend automatically delete the cart after an order is
      // placed. So trap any errors that might happen here, and remove the cart
      // from the redux store.
      // TODO: This should be in the delete cart action. But we need deleteCart to
      // return a more descriptive error with a code so we can conditionally clear
      // the redux store.
      return dispatch(deleteCart(cart.id))["catch"](function () {
        return dispatch(receiveCartData(null));
      });
    });
  };
};

exports.createOrder = createOrder;

var getOrder = function getOrder(id, opts) {
  return function (dispatch, _, _ref17) {
    var api = _ref17.api;
    return api.getOrder(id, opts).then(function (order) {
      return dispatch(receiveOrderData(_defineProperty({}, id, order)));
    });
  };
};

exports.getOrder = getOrder;

var getOrders = function getOrders(ids, opts) {
  return function (dispatch, _, _ref18) {
    var api = _ref18.api;
    return api.getOrders(ids, opts).then(function (orders) {
      // TODO: This looks like a common theme, consider refactoring.
      // This accumulator will make sure if we have requested a order that
      // isn't in the result set, that a null value is added to the redux store.
      // This will allow the UI to know that a value wasn't found on the server.
      var accumulator = function accumulator(acc, curr) {
        var order = orders.data.find(function (order) {
          return order.id === curr;
        });
        return _objectSpread({}, acc, _defineProperty({}, curr, order ? order : null));
      };

      var payload = ids.reduce(accumulator, {});
      return dispatch(receiveOrderData(payload));
    });
  };
};

exports.getOrders = getOrders;

var registerCustomer = function registerCustomer(data) {
  return function (dispatch, _, _ref19) {
    var api = _ref19.api;
    return api.registerCustomer(data).then(function (customer) {
      return dispatch(receiveCustomerData(customer));
    });
  };
};

exports.registerCustomer = registerCustomer;

var login = function login(username, password) {
  return function (dispatch, _, _ref20) {
    var api = _ref20.api;
    return api.login(username, password).then(function (customer) {
      return dispatch(receiveCustomerData(customer));
    });
  };
};

exports.login = login;

var logout = function logout() {
  return function (dispatch, _, _ref21) {
    var api = _ref21.api;
    return api.logout().then(function () {
      return dispatch(receiveCustomerData(null));
    });
  };
};

exports.logout = logout;

var getCustomer = function getCustomer(id, opts) {
  return function (dispatch, _, _ref22) {
    var api = _ref22.api;
    return api.getCustomer(id, opts).then(function (customer) {
      return dispatch(receiveCustomerData(customer));
    });
  };
};

exports.getCustomer = getCustomer;

var getCategory = function getCategory(id, opts) {
  return function (dispatch, _, _ref23) {
    var api = _ref23.api;
    return api.getCategory(id, opts).then(function (category) {
      return dispatch(receiveCategoryData(_defineProperty({}, id, category)));
    });
  };
};

exports.getCategory = getCategory;

var getCategories = function getCategories(ids, opts) {
  return function (dispatch, _, _ref24) {
    var api = _ref24.api;
    return api.getCategories(ids, opts).then(function (categories) {
      // This accumulator will make sure if we have requested a category that
      // isn't in the result set, that a null value is added to the redux store.
      // This will allow the UI to know that a value wasn't found on the server.
      var accumulator = function accumulator(acc, curr) {
        var category = categories.data.find(function (category) {
          return category.id === curr;
        });
        return _objectSpread({}, acc, _defineProperty({}, curr, category ? category : null));
      }; // TODO: We'll have to account for child categories at some point


      var payload = ids.reduce(accumulator, {});
      dispatch(receiveCategoryData(payload));
    });
  };
};

exports.getCategories = getCategories;

var getProduct = function getProduct(id, opts) {
  return function (dispatch, _, _ref25) {
    var api = _ref25.api;
    return api.getProduct(id, opts).then(function (product) {
      return dispatch(receiveProductData(_defineProperty({}, id, product)));
    });
  };
};

exports.getProduct = getProduct;

var getProducts = function getProducts(ids, opts) {
  return function (dispatch, _, _ref26) {
    var api = _ref26.api;
    return api.getProducts(ids, opts).then(function (products) {
      // This accumulator will make sure if we have requested a product that
      // isn't in the result set, that a null value is added to the redux store.
      // This will allow the UI to know that a value wasn't found on the server.
      var accumulator = function accumulator(acc, curr) {
        var product = products.data.find(function (product) {
          return product.id === curr;
        });
        return _objectSpread({}, acc, _defineProperty({}, curr, product ? product : null));
      };

      var payload = ids.reduce(accumulator, {});
      return dispatch(receiveProductData(payload));
    });
  };
};

exports.getProducts = getProducts;

var searchProducts = function searchProducts(searchParams) {
  return function (dispatch, _, _ref27) {
    var api = _ref27.api;
    var resultKey = (0, _jsonStableStringify["default"])(searchParams);

    var transformProductSearchResult = function transformProductSearchResult(productSearchResult) {
      return {
        id: productSearchResult.productId,
        name: productSearchResult.productName,
        price: productSearchResult.price,
        imageSets: [{
          images: [productSearchResult.defaultImage],
          variationProperties: [],
          sizeType: 'default'
        }],
        variationProperties: productSearchResult.variationProperties
      };
    };

    return api.searchProducts(searchParams).then(function (productSearch) {
      // Populate the product store, this will help with preloading.
      var accumulator = function accumulator(acc, curr) {
        return _objectSpread({}, acc, _defineProperty({}, curr.productId, transformProductSearchResult(curr)));
      };

      var products = (productSearch.results || []).reduce(accumulator, {});
      dispatch(receiveProductData(products)); // Add the search results to the store.

      return dispatch(receiveProductSearchData(_defineProperty({}, resultKey, productSearch)));
    });
  };
};

exports.searchProducts = searchProducts;

var getStore = function getStore(id, opts) {
  return function (dispatch, _, _ref28) {
    var api = _ref28.api;
    return api.getStore(id, opts).then(function (store) {
      return dispatch(receiveStoreData(_defineProperty({}, id, store)));
    });
  };
};

exports.getStore = getStore;

var searchStores = function searchStores(searchParams, opts) {
  return function (dispatch, _, _ref29) {
    var api = _ref29.api;
    var resultKey = (0, _jsonStableStringify["default"])(searchParams);
    return api.searchStores(searchParams, opts).then(function (StoreSearchResult) {
      // Add the search results to the store.
      return dispatch(receiveStoreSearchData(_defineProperty({}, resultKey, StoreSearchResult)));
    });
  };
};

exports.searchStores = searchStores;