import { IItem, IItemContainer } from "@rpg-engine/shared";

export const countItemFromInventory = (itemKey: string, inventory: IItemContainer) => {
  let itemsFromInventory: (IItem | undefined | null)[] = [];

  if (inventory) {
    Object.keys(inventory.slots).forEach(i => {
      const index = parseInt(i);

      if (inventory.slots[index]?.key === itemKey) {
        itemsFromInventory.push(inventory.slots[index]);
      }
    });
  }

  const totalQty = itemsFromInventory.reduce(
    (acc, item) => acc + (item?.stackQty || 1),
    0
  );

  return totalQty;
};