const STATUS = {
  SHIPPED: 'shipped',
  PENDING: 'pending',
  DELAYED: 'delayed',
};

const orders = [
  {
    id: 'o1',
    date: '2022-07-14T15:23:44.557Z',
    products: [
      {
        id: 'p1',
        title: 'React - kompletny przewodnik',
        price: 18.99,
        quantity: 2,
      },
      {
        id: 'p8',
        title: 'Okulary',
        price: 28.29,
        quantity: 1,
      },
    ],
    status: STATUS.SHIPPED,
  },
  {
    id: 'o2',
    date: '2022-06-11T11:52:23.557Z',
    products: [
      {
        id: 'p2',
        title: 'Czerwony dywan',
        price: 539.99,
        quantity: 1,
      },
    ],
    status: STATUS.SHIPPED,
  },
  {
    id: 'o3',
    date: '2022-07-16T13:29:01.007Z',
    products: [
      {
        id: 'p5',
        title: 'Najwyższe góry - wydanie z 1999 r.',
        price: 68.99,
        quantity: 1,
      },
      {
        id: 'p12',
        title: 'Piesze wędrówki dla zaawansowanych - Europa Południowa',
        price: 30.69,
        quantity: 1,
      },
    ],
    status: STATUS.DELAYED,
  },
  {
    id: 'o4',
    date: '2022-05-28T18:47:12.007Z',
    products: [
      {
        id: 'p6',
        title: 'Nowoczesne programowanie (książka)',
        price: 58.99,
        quantity: 3,
      },
      {
        id: 'p3',
        title: 'Budowanie zespołów - ciągle od nowa',
        price: 78.89,
        quantity: 1,
      },
    ],
    status: STATUS.SHIPPED,
  },
  {
    id: 'o5',
    date: '2022-08-01T09:15:22.040Z',
    products: [
      {
        id: 'p18',
        title: 'Ergonomiczna klawiatura',
        price: 121.39,
        quantity: 1,
      },
    ],
    status: STATUS.PENDING,
  },
  {
    id: 'o6',
    date: '2022-08-02T10:10:19.555Z',
    products: [
      {
        id: 'p1',
        title: 'React - kompletny przewodnik',
        price: 15.99,
        quantity: 1,
      },
      {
        id: 'p18',
        title: 'Ergonomiczna klawiatura',
        price: 121.39,
        quantity: 1,
      },
    ],
    status: STATUS.PENDING,
  },
];

export function getOrderById(id) {
  return { ...orders.find((order) => order.id === id) };
}

export function getOrderSummaryData(orderId) {
  const order = getOrderById(orderId);

  let totalExp = 0;
  let quantity = 0;

  for (const product of order.products) {
    totalExp += product.price * product.quantity;
    quantity += product.quantity;
  }

  return { total: totalExp, quantity };
}

export function getOrdersSummaryData(orderId) {
  let totalExp = 0;
  let quantity = 0;

  for (const order of orders) {
    if (orderId && order.id !== orderId) {
      continue;
    }
    for (const product of order.products) {
      totalExp += product.price * product.quantity;
      quantity += product.quantity;
    }
  }

  return { total: totalExp, quantity };
}

export function formatDate(date) {
  return new Date(date).toLocaleDateString('en-US', {
    day: '2-digit',
    month: 'short',
    year: 'numeric',
  });
}

export default orders;
