const errorPoductId = '321312'

const message =  {
  SUCCESS_ADDED_MESSAGE: 'Added to favourites',
  SUCCESS_REMOVE_MESSAGE: 'Removed from favourites',
  ERROR_MESSAGE: 'Cannot add to favourites. You have reached the max. number allowed'
}

function initLocalFavouriteProductEvent() {
  if (!Wick?.FavouriteProduct?.TOGGLE_FAVOURITE_PRODUCT_EVENT) {
    return;
  }
  $(window).on(Wick.FavouriteProduct.TOGGLE_FAVOURITE_PRODUCT_EVENT, function ({ detail: { resolve, reject, payload }}) {
    setTimeout(() => {
      if (payload.productId === errorPoductId) {
        reject({
          message: message.ERROR_MESSAGE
        })
      } else {
        if (payload.isChecked) {
          resolve({
            message: message.SUCCESS_ADDED_MESSAGE
          })
        } else {
          resolve({
            message: message.SUCCESS_REMOVE_MESSAGE
          })
        }
      }
    }, 500);
  });
}

$(document).ready(function () {
  initLocalFavouriteProductEvent();
});

