// TODO:
// move some parts of resolver to this file

import axios from 'axios';
//import { CartService } from './cart.service';

const CART_CACHE = 'nf_cart';

export class ShoppingService {
  //_cartApi: any;

  calculationType = '';
  prioritization = '';
  storeIdentifier = '';
  storeProvider = '';
  recipes = [];
  products = [];
  productExceptions = [];

  constructor() {
    //this._cartApi = new CartService();
  }

  getCart( customerToken:string, userId?:string ) {
    let promise:any;

    if( !userId ) {
      promise = new Promise( ( resolve:any ) => {
        const cart = localStorage.getItem( CART_CACHE );
        resolve(  cart ? JSON.parse( cart ): null );
      } );
    } else {
      //promise = this._cartApi.get( customerToken, userId );
    }

    return promise.then( ( cart:any ) => {
      if( !cart ) {
        cart = {
          calculation_type: this.calculationType,
          prioritization: this.prioritization,
          store: {
            identifier: this.storeIdentifier,
            provider: this.storeProvider
          },
          recipes: this.recipes,
          products: this.products,
          product_exceptions: this.productExceptions,
        };
      } else {
      }

      return cart;
    } );
  }
}
