/**
 * React Native Full App (https://store.enappd.com/product/react-native-full-app)
 *
 * Copyright © 2019-present Enappd. All rights reserved.
 *
 * This source code is licensed as per the terms found in the
 * LICENSE.md file in the root directory of this source tree.
 */
import Data from './data';

class Database {
  products: Array<any>;
  cartItems: Array<any>;
  shortOffers: Array<any>;
  constructor() {
    this.products = [];
    this.cartItems = [];
    const { shortOffers } = Data;
    this.shortOffers = shortOffers;
  }

  addToCart = (sectionIndex: number, productIndex: number) => {
    this.shortOffers[sectionIndex].products[productIndex].units += 1;
    const proIndex = this.products.findIndex(
      (el) => el.id === this.shortOffers[sectionIndex].products[productIndex].id
    );
    if (proIndex !== -1) {
    } else {
      const newProduct = this.shortOffers[sectionIndex].products[productIndex];
      this.products.push(newProduct);
    }
  };

  removeFromCart = (sectionIndex: number, productIndex: number) => {
    this.shortOffers[sectionIndex].products[productIndex].units -= 1;
    const proIndex = this.products.findIndex(
      (el) => el.id === this.shortOffers[sectionIndex].products[productIndex].id
    );
    if (this.products[proIndex].units > 0) {
      // this.products[proIndex].units -= 1
    } else {
      this.products.splice(proIndex, 1);
    }
  };
  minusQuantity = (productId: number) => {
    this.products[productId].units--;
    if (this.products[productId].units > 0) {
    } else {
      this.products.splice(productId, 1);
    }
  };
  plusQuantity = (productId: number) => {
    this.products[productId].units++;
  };
}

export default Database;
