{"size": 1837, "ext": "js", "max_stars_repo_path": "assets/js/general.js", "max_stars_repo_name": "denilson-santos/brumas-delivery", "max_stars_repo_stars_event_min_datetime": "2019-12-09T17:14:55.000Z", "max_stars_repo_stars_event_max_datetime": "2019-12-09T17:14:55.000Z", "max_issues_repo_path": "assets/js/general.js", "max_issues_repo_name": "denilson-santos/brumas-delivery", "max_issues_repo_issues_event_min_datetime": "2021-03-22T23:57:38.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-13T17:39:41.000Z", "max_forks_repo_path": "assets/js/general.js", "max_forks_repo_name": "denilson-santos/brumas-delivery", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 23.8571428571, "max_line_length": 102, "alphanum_fraction": 0.6684812194}
// Create our number formatter.
formatter = new Intl.NumberFormat('pt-BR', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2 
});

var cart = loadCart();

$('.phone-mask').mask('(00) 0000-0000');
$('.cell-phone-mask').mask('(00) 0 0000-0000');

$('.cart-quantity').text(cart.quantity);
$('.cart-price').text(formatter.format(cart.total/100));

$('.show-tooltip').tooltip();

function addCart(plateId, restaurantId, comments, complementsChecked, itemsChecked, plateTotalPrice) {
  const { items, quantity } = cart;

  items.push({
    id: items.length ? items[items.length - 1].id + 1 : 1,
    plate_id: plateId,
    restaurant_id: restaurantId,
    comments: comments,
    plate_total_price: plateTotalPrice,
    ...(complementsChecked.length && { plate_complements: complementsChecked }),
    ...(itemsChecked.length && { plate_items: itemsChecked })
  });

  const total = items.reduce((totalItems, item) => {
    return totalItems + item.plate_total_price;
  }, 0);

  cart.quantity = items.length;
  cart.total = total;

  localStorage.setItem('cart', JSON.stringify(cart));
}

function removeCart(id) {
  cart.items = cart.items.filter((item) => item.id !== id);
  updateCart(cart.items);
}

function cleanCart() {
  localStorage.removeItem('cart');
}

function updateCart(cartItems) {  
  cart.items = cartItems;

  const total = cart.items.reduce((totalItems, item) => {
    return totalItems + item.plate_total_price;
  }, 0);

  cart.quantity = cart.items.length;
  cart.total = total;

  localStorage.setItem('cart', JSON.stringify(cart));
}

function loadCart() {
  let storeCart = JSON.parse(localStorage.getItem('cart'));

  if (!storeCart || !Object.keys(storeCart).length)
    storeCart = {
      items: [],
      total: 0,
      quantity: 0,
    };

  return storeCart;
}

function getCart() {
  return cart;
}