import PromiseMap from 'p-map';
import { apiShopeeSeller } from '@/api/shopee/seller/init';

const splitListOrder = (listPayload = []) => {
  const perEach = 5;

  const listReturn = [];
  for (let i = 0; i < listPayload.length; i += perEach) {
    listReturn.push(listPayload.slice(i, i + perEach));
  }

  return listReturn;
};

/**
 *
 * @param {number} limit
 * @param {number} page
 * @param {string} sentinel
 * @param {number} tabId
 * @returns
 */
export const getListOrderIndex = async (limit = 40, page = 1, sentinel = '', tabId = 0) => {
  const returnIndex = {
    listIndex: [],
    totalOrder: 0,
    previousSentinel: '',
    nextsentinel: '',
    isError: false,
  };

  const payload = {
    order_list_tab: tabId,
    entity_type: 1,
    pagination: {
      from_page_number: 1,
      page_number: page,
      page_size: limit,
      page_sentinel: '',
    },
    filter: {
      fulfillment_type: 0,
      is_drop_off: 0,
      fulfillment_source: 0,
      action_filter: 0,
    },
  };

  if (page > 1) {
    payload.pagination.from_page_number = page - 1;
  }

  if (sentinel) {
    payload.pagination.page_sentinel = sentinel;
  }

  try {
    const result = await apiShopeeSeller.post('/api/v3/order/search_order_list_index', payload);

    returnIndex.listIndex = result.data.index_list;
    returnIndex.totalOrder = result.data.pagination.total;
    returnIndex.nextsentinel = result.data.pagination.previous_page_sentinel;
    returnIndex.previousSentinel = result.data.pagination.next_page_sentinel;
  } catch (error) {
    returnIndex.isError = true;
  }

  return returnIndex;
};

/**
 * @param {object[]} listPayload
 * @param {number} tabId
 * @returns {Promise<ShopeeSellerOrderCard[]>}
 */
const getListOrderCard = async (listPayload = [], tabId = 0) => {
  if (!(listPayload && listPayload.length && tabId)) {
    return [];
  }

  const payload = {
    order_list_tab: tabId,
    need_count_down_desc: false,
    order_param_list: listPayload,
  };

  try {
    const result = await apiShopeeSeller.post('/api/v3/order/get_order_list_card_list', payload);

    return result.data.card_list.map((card) => card.order_card);
  } catch (error) {
    return [];
  }
};

/**
 * @param {object[]} listPayload
 * @param {number} tabId
 * @returns
 */
const getListOrderButton = async (listPayload = [], tabId = 0) => {
  if (!(listPayload && listPayload.length && tabId)) {
    return [];
  }

  const payload = {
    order_list_tab: tabId,
    order_with_ofgs: listPayload,
  };

  try {
    const result = await apiShopeeSeller.post('/api/v3/order/get_order_list_buttons', payload);
    return result.data.order_list;
  } catch (error) {
    return [];
  }
};

/**
 *
 * @param {number} limit
 * @param {number} page
 * @param {string} sentinel
 * @returns
 */
export const getListOrderCompleted = async (limit = 40, page = 1, sentinel = '') => {
  const tabId = 500;
  /** @type {ShopeeSellerOrderCard[]} */
  const listDataReturn = [];

  const resultIndex = await getListOrderIndex(limit, page, sentinel, tabId);

  if (!resultIndex.isError && resultIndex.listIndex && resultIndex.listIndex.length) {
    await PromiseMap(splitListOrder(resultIndex.listIndex), async (listPayload) => {
      const listOrderFetch = await getListOrderCard(listPayload, tabId);
      listDataReturn.push(...listOrderFetch);
    });
  }

  return {
    listOrder: listDataReturn,
    ...resultIndex,
  };
};

/**
 *
 * @param {number} limit
 * @param {number} page
 * @param {string} sentinel
 * @returns
 */
export const getListButtonCompleted = async (limit = 40, page = 1, sentinel = '') => {
  const tabId = 500;
  const listDataReturn = [];

  const resultIndex = await getListOrderIndex(limit, page, sentinel, tabId);

  if (!resultIndex.isError && resultIndex.listIndex && resultIndex.listIndex.length) {
    await PromiseMap(splitListOrder(resultIndex.listIndex), async (listPayload) => {
      const listOrderFetch = await getListOrderButton(listPayload, tabId);
      listDataReturn.push(...listOrderFetch);
    });
  }

  return {
    listButton: listDataReturn,
    ...resultIndex,
  };
};
