export function resolveBillingPeriod(
  periodStart?: string,
  periodEnd?: string,
): { start: Date; end: Date } {
  if (periodStart && periodEnd) {
    return {
      start: new Date(periodStart),
      end: new Date(periodEnd),
    };
  }

  const now = new Date();
  const start = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
  const end = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1));
  return { start, end };
}

export function previousBillingPeriod(reference = new Date()): { start: Date; end: Date } {
  const start = new Date(
    Date.UTC(reference.getUTCFullYear(), reference.getUTCMonth() - 1, 1),
  );
  const end = new Date(Date.UTC(reference.getUTCFullYear(), reference.getUTCMonth(), 1));
  return { start, end };
}
