export default {
  time: (time, {
    timeZone,
    truncateZeroMinutes = false
  } = {}) => {
    const {
      format
    } = new Intl.DateTimeFormat('en-US', {
      hour: 'numeric',
      minute: 'numeric',
      timeZone
    });
    let [timestamp, dayPeriod] = format(time).split(' ');
    dayPeriod = dayPeriod.split('').map(e => `${e.toLowerCase()}.`).join('');
    if (truncateZeroMinutes) {
      timestamp = timestamp.replace(':00', '');
    }
    return `${timestamp} ${dayPeriod}`;
  },
  weekday: (time, {
    timeZone
  } = {}) => {
    const {
      format
    } = new Intl.DateTimeFormat('en-US', {
      weekday: 'long',
      timeZone
    });
    return format(time);
  },
  timezone: (time, {
    timeZone
  } = {}) => {
    const {
      format
    } = new Intl.DateTimeFormat('en-US', {
      timeZoneName: 'short',
      timeZone
    });
    return format(time).split(',')[1].trim();
  },
  day: (time, {
    timeZone,
    shortDate = true
  } = {}) => {
    let format;
    if (shortDate) {
      format = new Intl.DateTimeFormat('en-US', {
        year: 'numeric',
        month: 'numeric',
        day: 'numeric',
        timeZone
      }).format;
    } else {
      format = new Intl.DateTimeFormat('en-US', {
        year: 'numeric',
        month: 'long',
        day: 'numeric',
        timeZone
      }).format;
    }
    return format(time);
  },
  apdate: (time, {
    timeZone
  } = {}) => {
    const {
      format
    } = new Intl.DateTimeFormat('en-US', {
      timeZone
    });

    // Note that this is mapping the real month numbers onto AP abbreviations,
    // _not_ JavaScript's zero-indexed month indices. That's because we're
    // using the formatted month number, _not_ the `date.getMonth()` function
    // to index into this object.
    const realMonthToApMonth = {
      1: 'Jan.',
      2: 'Feb.',
      3: 'March',
      4: 'April',
      5: 'May',
      6: 'June',
      7: 'July',
      8: 'Aug.',
      9: 'Sept.',
      10: 'Oct.',
      11: 'Nov.',
      12: 'Dec.'
    };
    const [month, day, year] = format(time).split('/');
    const apMonth = realMonthToApMonth[month];
    return `${apMonth} ${day}, ${year}`;
  }
};