import { useMemo } from 'react';
import { useStoryCollection } from "../../../hooks/election/index.js";
import useIsMobile from "../../../hooks/breakpoint/useIsMobile/index.js";
import useContainerBreakpoint from "../../../hooks/breakpoint/useContainerBreakpoint/index.js";
import sortBy from "../../../utils/sorting/sortBy.js";
import useContainerBreakpointIsAtLeast from "../../../hooks/breakpoint/useContainerBreakpointIsAtLeast/index.js";
import useContainerBreakpointIsAtMost from "../../../hooks/breakpoint/useContainerBreakpointIsAtMost/index.js";
export default function useStoryRecircOptions(tag, mode) {
  const isMobile = useIsMobile();
  const containerBp = useContainerBreakpoint();
  const isAtLeastLg = useContainerBreakpointIsAtLeast('lg');
  const isAtLeastMd = useContainerBreakpointIsAtLeast('md');
  const isAtMostMd = useContainerBreakpointIsAtMost('md');
  const collection = useStoryCollection(tag);
  const count = useMemo(() => {
    if (isAtLeastLg) {
      return 4;
    }
    if (isMobile || containerBp === 'md') {
      return 5;
    }
    return 2;
  }, [isMobile, containerBp, isAtLeastLg]);
  const sortedStories = useMemo(() => sortBy(collection.stories, [{
    comparison: s => new Date(`${s.published.replace(' ', 'T')}Z`),
    direction: 'desc'
  }, 'url']), [count, collection]);
  const filteredStories = useMemo(() => {
    switch (mode) {
      case 'even':
        return sortedStories.filter((s, idx) => idx % 2 === 0).slice(0, count);
      case 'odd':
        return sortedStories.filter((s, idx) => idx % 2 === 1).slice(0, count);
      default:
        // Since the default mode is "chronological", it's handled here
        return sortedStories.slice(0, count);
    }
  }, [sortedStories]);
  const layout = useMemo(() => isMobile || isAtLeastMd ? 'horizontal' : 'vertical', [isMobile, isAtLeastMd]);
  const overflow = useMemo(() => isMobile && isAtMostMd || containerBp === 'md', [isMobile, containerBp, isAtMostMd]);
  return {
    stories: filteredStories,
    layout,
    overflow
  };
}