   //==========================================
   class Cart {
   
    //--------------------------------------
    constructor( key ) {

        this.key = key;
        this.contents = [];
        let _key = this.key;

        // Check localStorage and initialise the contents of this.contents
        let _contents = localStorage.getItem(_key);
        if(_contents) {

            this.contents = JSON.parse(_contents);
                   
        } else {

            console.log("No Data in Cart");
           
            //dummy test data
            //this.contents = [
            //            {id:1, item_number:'Cork', qty:1, mc_gross: 0.01},
            //            {id:2, item_number:'Dial Up', qty:1, mc_gross: 0.01},
            //            {id:3, item_number:'Door Bell', qty:1, mc_gross: 0.04}
            //        ];
            //        this.sync();
            
        }

        //console.log(this.contents); 

    }

    //--------------------------------------
    async sync() {
        await localStorage.setItem( this.key, JSON.stringify(this.contents) );
    }
   
    //--------------------------------------        
    // Find an item in the cart by it's id
    find(id) {

        let match = this.contents.filter(item=>{
            if(item.id == id)
                return true;
        });
        if(match && match[0])
            return match[0];
    }

    //--------------------------------------        
    add(id) {

        // Add a new item to the cart
        // Check that it is not in the cart already
        if(this.find(id)) {

            this.increase(id, 1);   //found
        } else {  

            let arr = gblProducts.filter(product=>{    //not found
                if(product.id == id){
                    return true;
                }
            });
            if(arr && arr[0]){
                let obj = {
                    id: arr[0].id,              //push id, item_number, qty, mc_gross
                    item_number: arr[0].item_number,
                    qty: 1,
                    mc_gross: Number(arr[0].mc_gross).toFixed(2),
                    shipping: Number(arr[0].shipping).toFixed(2),
                    shippingtotal: Number(arr[0].shipping).toFixed(2),
                    total: Number(arr[0].mc_gross).toFixed(2)         
                };
                this.contents.push(obj);
                //update localStorage
                this.sync();
            }else{
                //product id does not exist in products data
                console.error('Invalid Product');
            }
        }
    }

    //--------------------------------------
    // Increase the quantity of an item in the cart
    increase(id, qty=1) {

        this.contents = this.contents.map(item=>{
            if(item.id === id)
                item.qty = item.qty + qty;
                item.shippingtotal = Number(item.qty * item.shipping).toFixed(2);
                item.total = Number(item.qty * item.mc_gross).toFixed(2);
            return item;
        });
        // update localStorage
        this.sync()
    }

    //--------------------------------------
    // Decrease the quantity of an item in the cart
    decrease(id, qty=1) {

        this.contents = this.contents.map(item=>{
            if(item.id === id)
                item.qty = item.qty - qty;
                item.shippingtotal = Number(item.qty * item.shipping).toFixed(2);
                item.total = Number(item.qty * item.mc_gross).toFixed(2);
            return item;
        });
        this.contents.forEach(async item=>{
            if(item.id === id && item.qty === 0)
                await this.remove(id);
        });
        // update localStorage
        this.sync()
    }

    //--------------------------------------
    // Remove an item from the cart based on its id
    remove(id) {

        this.contents = this.contents.filter(item=>{
            if(item.id !== id)
                return true;
        });
        // update localStorage
        this.sync()
    }

    //--------------------------------------
    // Empty whole cart
    empty() {

        this.contents = [];
        // update localStorage
        this.sync()
    }

    //--------------------------------------        
    sort(field='title') {
        // sort by field - title, price
        // return a sorted shallow copy of the cart array
        let sorted = this.contents.sort( (a, b)=>{
            if ( a[field] > b[field] ) {
                return 1;
            } else if ( a[field] < a[field] ) {
                return -1;
            } else {
                return 0;
            }
        });
        return sorted;
        //No impact on localStorage
    }
    
    //--------------------------------------        
    logContents(prefix) {
        console.log(prefix, this.contents)
    }

};

