import type { PageSectionComponent } from '../types';
import type { CountdownSectionProps } from './types';

import { ItemTypes } from '~/lib/types';
import { CountdownSectionView } from './CountdownSectionView';
import { getReleaseTimestamp } from './utils';

const CountdownSection: PageSectionComponent<CountdownSectionProps> = ({
  title,
  layoutData: { item },
}) => {
  if (item.type !== ItemTypes.PRERELEASE)
    throw new Error(`Countdown is not supported for item type: ${item.type}`);

  const { releaseDate, releaseTimezone } = item;

  const releaseTimestamp = getReleaseTimestamp(releaseDate, releaseTimezone);
  const remainingTime = releaseTimestamp - Date.now();

  // do not render if no time remaining
  if (remainingTime < 0) return null;

  return (
    <CountdownSectionView title={title} releaseTimestamp={releaseTimestamp} />
  );
};

// we need to export as default for lazy loading
export default CountdownSection;
