import dayjs from 'dayjs';
import isSameOrBeforePlugin from 'dayjs/plugin/isSameOrBefore';

dayjs.extend(isSameOrBeforePlugin);

const getCurrentAndFuturePrice = (prices) => {
  const isFutureDate = (startDate) => dayjs(startDate).isAfter(new Date().getTime());
  const isPastDate = (startDate) => dayjs(startDate).isSameOrBefore(new Date().getTime());
  const result = [];

  for (let i = 0; i < prices.length; i += 1) {
    const price = prices[i];

    if (isFutureDate(price.startDate) && (!price.endDate || isFutureDate(price.endDate))) {
      result.push(price);
    }
    if (isPastDate(price.startDate) && (!price.endDate || isFutureDate(price.endDate))) {
      result.push(price);
      break;
    }
  }

  return result;
};

export default getCurrentAndFuturePrice;
