import { compareAsc } from 'date-fns';
import { toSeconds } from 'duration-fns';

function isDeliveryEstimationAvailable(quote) {
  return quote.deliveryEstimation;
}
function hasDuration(quote) {
  return quote.deliveryEstimation.duration;
}
function hasDeliveryDate(quote) {
  return quote.deliveryEstimation.deliveryDate;
}
function isDurationMinMaxNull(quote) {
  const {
    duration
  } = quote.deliveryEstimation;
  return duration.min == null && duration.max == null;
}
function isDeliveryDateMinMaxNull(quote) {
  const {
    deliveryDate
  } = quote.deliveryEstimation;
  return deliveryDate.min == null && deliveryDate.max == null;
}
function hasNoValidDeliveryEstimation(quote) {
  if (!isDeliveryEstimationAvailable(quote) || !hasDuration(quote)) {
    return true;
  }
  return false;
}
function hasNoValidDeliveryDate(quote) {
  if (!isDeliveryEstimationAvailable(quote) || !hasDeliveryDate(quote)) {
    return true;
  }
  return false;
}
function sortByDeliverySpeedEstimate(quotes) {
  // the use of slice() here is to provide a shallow copy of quotes, otherwise the original array is modified, which could lead to unexpected behaviour
  return quotes.slice().sort((a, b) => {
    if (hasNoValidDeliveryEstimation(a)) {
      return 1;
    }
    if (hasNoValidDeliveryEstimation(b)) {
      return -1;
    }
    if (isDurationMinMaxNull(a)) {
      return 1;
    }
    if (isDurationMinMaxNull(b)) {
      return -1;
    }
    const aDuration = a.deliveryEstimation.duration;
    const bDuration = b.deliveryEstimation.duration;
    const aMax = toSeconds(aDuration.max);
    const aMin = toSeconds(aDuration.min);
    const bMax = toSeconds(bDuration.max);
    const bMin = toSeconds(bDuration.min);
    if (aMax === bMax) {
      if (aMin < bMin) {
        return 1;
      }
      if (aMin > bMin) {
        return -1;
      }
      return 0;
    } else {
      if (aMax > bMax) {
        return 1;
      }
      if (aMax < bMax) {
        return -1;
      }
      return 0;
    }
  });
}
function sortByDeliveryDate(quotes) {
  // the use of slice() here is to provide a shallow copy of quotes, otherwise the original array is modified, which could lead to unexpected behaviour
  return quotes.slice().sort((a, b) => {
    if (hasNoValidDeliveryDate(a)) {
      return 1;
    }
    if (hasNoValidDeliveryDate(b)) {
      return -1;
    }
    if (isDeliveryDateMinMaxNull(a)) {
      return 1;
    }
    if (isDeliveryDateMinMaxNull(b)) {
      return -1;
    }
    const aDeliveryDate = a.deliveryEstimation.deliveryDate;
    const bDeliveryDate = b.deliveryEstimation.deliveryDate;
    const aMin = aDeliveryDate.min ? aDeliveryDate.min : aDeliveryDate.max;
    const bMin = bDeliveryDate.min ? bDeliveryDate.min : bDeliveryDate.max;
    const result = compareAsc(new Date(aMin), new Date(bMin));
    if (result === 0) {
      return compareAsc(new Date(aDeliveryDate.max), new Date(bDeliveryDate.max));
    } else {
      return result;
    }
  });
}

export { sortByDeliverySpeedEstimate as a, sortByDeliveryDate as s };
//# sourceMappingURL=sortQuotesByDeliverySpeedEstimate-f8c8a1b8.js.map
