text: import database from '@/firebase/database';
  import _ from '@/utils';
  
  export default {
    namespaced: true,
    state: {
      tables: {},
      tablesHistory: {},
      orders: {},
      ordersHistory: {},
      menuReference: {},
      allMenuSubcategories: [
        {id: 'special', name: 'especiais'},
        {id: 'baked', name: 'cozidos'},
        {id: 'fried', name: 'fritos'},
        {id: 'grilled', name: 'grelhados'},
        {id: 'extra', name: 'extras'},
        {id: 'extra-topping', name: 'misturas'},
        {id: 'extra-accomp', name: 'acompanhamentos'},
        {id: 'juice', name: 'sucos'},
        {id: 'soda', name: 'refrigerantes'},
        {id: 'beer', name: 'cervejas'},
        {id: 'liquor', name: 'cachacas'},
        {id: 'drink', name: 'drinks'},
        {id: 'brandy', name: 'conhaques'},
        {id: 'vodka', name: 'vodkas'},
        {id: 'whiskey', name: 'whiskeys'},
        {id: 'candy', name: 'doces'},
        {id: 'other', name: 'outros'},
      ],
      accompaniments: ['Fritas', 'Maionese', 'Salada', 'Pure', 'Sem acomp.'],
    },
    mutations: {},
    actions: {
      /* tables set finished/unfinished */
      async TABLES_SET_TABLE_AS_FINISHED({dispatch}, payload) {
        let {orderKey, order} = payload;
  
        await dispatch('TABLES_HISTORY_SET_TABLE', {orderKey, order});
        await dispatch('TABLES_DELETE_TABLE', orderKey);
      },
  
      async TABLES_SET_TABLE_AS_UNFINISHED({dispatch}, payload) {
        let {orderKey, order} = payload;
  
        dispatch('TABLES_SET_TABLE', {orderKey, order});
        dispatch('TABLES_HISTORY_DELETE_TABLE', orderKey);
      },
  
      /* tablesHistory */
      async TABLES_HISTORY_SET_TABLE({}, payload) {
        try {
          await database.restaurant.TABLES_HISTORY_SET_TABLE(payload.order, payload.orderKey);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async TABLES_HISTORY_DELETE_TABLE({}, payload) {
        try {
          await database.restaurant.TABLES_HISTORY_DELETE_TABLE(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async GET_TABLES_HISTORY() {
        try {
          await database.restaurant.GET_TABLES_HISTORY();
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      /* tables */
      async TABLES_ADD_ORDER_TO_TABLE({state, dispatch}, payload) {
        const sum = (a, b) => parseFloat(a) + parseFloat(b);
        let currentOrder = payload;
        let tableRef = currentOrder.tableRef;
        let existingTable = state.tables[tableRef];
  
        let amount = sum(existingTable.payment.amount, currentOrder.payment.amount);
        let payment = {...existingTable.payment, amount};
  
        let mergedOrder = [...existingTable.order, ...currentOrder.order];
        let resultTable = {...existingTable, order: mergedOrder, payment};
  
        dispatch('TABLES_SET_TABLE', {order: resultTable, orderKey: tableRef});
      },
  
      async TABLES_SET_TABLE({}, payload) {
        try {
          await database.restaurant.TABLES_SET_TABLE(payload.order, payload.orderKey);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async TABLES_DELETE_TABLE({}, payload) {
        try {
          await database.restaurant.TABLES_DELETE_TABLE(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async GET_TABLES() {
        try {
          await database.restaurant.GET_TABLES();
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      /* orders set done/undone */
      async ORDERS_SET_ORDER_DONE({dispatch}, payload) {
        let {orderKey, order} = payload;
        let {mode} = order;
  
        if (order.tableRef != '') await dispatch('TABLES_ADD_ORDER_TO_TABLE', order);
  
        if (mode == 'table') {
          if (order.tableRef == '') await dispatch('TABLES_SET_TABLE', {orderKey, order});
        } else {
          await dispatch('PRINT_ORDER', order, {root: true});
        }
  
        await dispatch('ORDERS_HISTORY_ADD_ORDER', {orderKey, order});
        await dispatch('ORDERS_DELETE_ORDER', orderKey);
      },
  
      async ORDERS_SET_ORDER_UNDONE({dispatch}, payload) {
        let {orderKey, order} = payload;
        order.isDelivered = false;
  
        dispatch('ORDERS_UPDATE_ORDER', {orderKey, order});
        dispatch('ORDERS_HISTORY_DELETE_ORDER', orderKey);
      },
  
      /* orders:crud */
      async ORDERS_ADD_ORDER({}, payload) {
        try {
          await database.restaurant.ORDERS_ADD_ORDER(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async ORDERS_UPDATE_ORDER({}, payload) {
        try {
          await database.restaurant.ORDERS_UPDATE_ORDER(payload.order, payload.orderKey);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async ORDERS_DELETE_ORDER({}, payload) {
        try {
          await database.restaurant.ORDERS_DELETE_ORDER(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async GET_ORDERS() {
        try {
          await database.restaurant.GET_ORDERS();
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      /*database: orders-history  */
  
      async ORDERS_HISTORY_ADD_ORDER({}, payload) {
        try {
          await database.restaurant.ORDERS_HISTORY_ADD_ORDER(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async ORDERS_HISTORY_DELETE_ORDER({}, payload) {
        try {
          await database.restaurant.ORDERS_HISTORY_DELETE_ORDER(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async GET_ORDERS_HISTORY() {
        try {
          await database.restaurant.GET_ORDERS_HISTORY();
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      /* database: menu-reference */
  
      async MENU_REFERENCE_ADD_ITEM({}, payload) {
        try {
          await database.restaurant.MENU_REFERENCE_ADD_ITEM(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async MENU_REFERENCE_UPDATE_ITEM({}, payload) {
        try {
          await database.restaurant.MENU_REFERENCE_UPDATE_ITEM(payload.item, payload.url);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async MENU_REFERENCE_DELETE_ITEM({}, payload) {
        try {
          await database.restaurant.MENU_REFERENCE_DELETE_ITEM(payload);
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
  
      async GET_MENU_REFERENCE() {
        try {
          await database.restaurant.GET_MENU_REFERENCE();
        } catch (error) {
          console.log(`error code: ${error.code}, error message: ${error.message}.`);
        }
      },
    },
    getters: {
      getMenuSubcategories: state => {
        return state.allMenuSubcategories.map(subcategory => subcategory.id);
      },
  
      getAvailableMenuItems: state => {
        return _.filter(state.menuReference, item => item.isAvailable);
      },
  
      getSubcategorizedMenu: (state, getters) => filter => {
        let menuObject = filter ? getters.getAvailableMenuItems : state.menuReference;
        let menuSubcategorized = {};
  
        getters.getMenuSubcategories.forEach(subcategory => (menuSubcategorized[subcategory] = {}));
  
        for (let itemKey in menuObject) {
          let currentItem = menuObject[itemKey];
          let subcategory = currentItem.subcategory;
  
          menuSubcategorized[subcategory][itemKey] = currentItem;
        }
  
        return menuSubcategorized;
      },
  
      isOrderInOrderHistory: state => orderKey => {
        let objectKeys = Object.keys(state.ordersHistory);
        let orderExistsInHistory = objectKeys.findIndex(key => (key = orderKey));
  
        return orderExistsInHistory > -1;
      },
    },
  };
  