'use strict';

class ShoppingCart {
  constructor(opts) {
    opts = opts || {};

    this.name = opts.name;
    this.items = opts.items || [];
    this.___class = ShoppingCart.name;
  }

  addItem(item) {
    item.objectId = null;

    this.items.push(item);
  }

  deleteItem(product) {
    const idx = this.items.findIndex(item => item.product === product);

    if (idx === -1) {
      throw new Error(`No ${product} in cart`);
    }

    this.items.splice(idx, 1);

    return this;
  }

  setQuantity(product, quantity) {
    const productItem = this.items.find(item => item.product === product);

    if (!productItem) {
      throw new Error(`No [${product}] in cart`);
    }

    productItem.quantity = quantity;

    return this;
  }

  getItems() {
    return this.items;
  }

  destroy() {
    return Backendless.Cache.remove(this.name, this);
  }

  save() {
    return Backendless.Cache.put(this.name, this);
  }

  static get(name, mustExist) {
    Backendless.Cache.setObjectFactory(ShoppingCart.name, ShoppingCart);

    return Backendless.Cache.get(name).then(cart => {
      if (!cart && mustExist) {
        throw new Error(`Shopping cart [${name}] does not exist`);
      }

      return cart;
    });
  }
}

module.exports = ShoppingCart;
@@ -832,38 +832,32 @@
 remove(this.name
-, this
 );%0A  %7D%0A%0A  save()

fix (examples) error in ShoppingCart.destroy method