import { Injectable } from '@angular/core';
import CartItem from '../models/cart-item';
import { Storage } from '@ionic/storage';

@Injectable({
    providedIn: 'root'
})
export class CartItemService {
    cart_items: Array<CartItem> = [];

    constructor(
        private storage: Storage
    ) { }

    async updateCartItems(cart_items: Array<CartItem>) {
        this.cart_items = cart_items;
        this.storage.set('cart_items', JSON.stringify(this.cart_items));
        return await this.cart_items;
    }

    async getAllCartItems() {
        return await this.storage.get('cart_items').then((data: string) => {
            if (data) {
                return JSON.parse(data);
            } else {
                return [];
            }
        });
    }

	async clearCartItems() {
		return await this.storage.set('cart_items', '[]');
	}

}
