import { ORIGIN_OPTIONS } from '../constants';
import {
  DEFAULT_FEED_TYPE_VALUE,
  DEFAULT_MEDIA_TYPE_VALUE,
  SPONSORED_ALL,
} from '@/modules/editorial/editorialSearchFilter/constants';

export const getClosestKnownTimeZone = () => {
  const timeZones = [
    {
      name: 'UTC',
      value: 0,
    },
    {
      name: 'EST',
      value: -5,
    },
    {
      name: 'PST',
      value: -8,
    },
  ];

  const time = -(new Date().getTimezoneOffset() / 60);

  const closestTimeZone = timeZones.reduce((a, b) => (Math.abs(b.value - time) < Math.abs(a.value - time) ? b : a));

  return closestTimeZone?.name ?? 'UTC';
};

export const getOriginValue = (filterInternalParams) => {
  const getValue = (type, defaultValue) =>
    ORIGIN_OPTIONS[type] || ORIGIN_OPTIONS[type] === null ? ORIGIN_OPTIONS[type] : ORIGIN_OPTIONS[defaultValue];

  if (filterInternalParams.feedType) {
    return getValue(filterInternalParams.feedType, DEFAULT_FEED_TYPE_VALUE);
  }

  if (filterInternalParams.mediaType) {
    return getValue(filterInternalParams.mediaType, DEFAULT_MEDIA_TYPE_VALUE);
  }

  if (filterInternalParams.sponsored) {
    return getValue(filterInternalParams.sponsored, SPONSORED_ALL);
  }

  return ORIGIN_OPTIONS[DEFAULT_MEDIA_TYPE_VALUE];
};

export default {};
