import useResizeObserver from "../../../../hooks/useResizeObserver/index.js";
import { useContestStatus, useContestMetadata } from "../../../../hooks/election/index.js";
import { useMemo } from 'react';
import { optionsByFormat, optionsBySize } from './formatOptions';
import sizes from './sizes';
const useFormatOptions = function useFormatOptions({
  format,
  expandable,
  expandableCutoff,
  disableFooter,
  id
}) {
  const meta = useContestMetadata(id);
  const showPartyTags = meta && !meta.party;
  const contestStatus = useContestStatus(id);
  const contestHasOutcome = contestStatus.outcome !== 'none';
  const hasRowPaddingRight = contestHasOutcome;
  const [ref, [width]] = useResizeObserver();

  // Get our default options from our given format
  const defaultOptions = optionsByFormat[format];

  // Find the size based on the container width
  const matchingSize = sizes.find(step => width < step.max);
  const sizeOptions = optionsBySize[format][matchingSize.key];

  // Show bar either based on options or on contest outcome
  const showBar = contestHasOutcome ? false : defaultOptions.showBar;

  // Override the format options if needed
  const formatOptions = useMemo(() => ({
    ...defaultOptions,
    ...sizeOptions,
    size: matchingSize.key,
    expandable,
    expandableCutoff,
    format,
    showPartyTags,
    hasRowPaddingRight,
    showBar,
    disableFooter
  }), [defaultOptions, sizeOptions, matchingSize.key, expandable, expandableCutoff, format, showPartyTags, hasRowPaddingRight, showBar, disableFooter]);
  return [ref, formatOptions];
};
export default useFormatOptions;