import { useCallback } from 'react';
import getColor from "../../../utils/colors/getColor.js";
import getElectionPaletteColor from "../../../utils/colors/getElectionPaletteColor.js";

/**
 *
 * @param {Object} colors  // Colors palette object as returned by `Hooks/useMapColors`
 * @param {String} format  // The diplay format of the map. One of "dots", "geo" or "carto"
 * @returns
 */
const useGetMapLabelProps = function useGetMapLabelProps(colors, format) {
  return useCallback(({
    code
  }) => {
    const color = colors.fipsToPalette.get(code);
    const getPaletteColor = (palette, token) => {
      const paletteName = palette === 'fallback' ? 'other-party' : palette;
      return getColor(getElectionPaletteColor(paletteName, token));
    };
    if (!color && format === 'geo') {
      return {
        fill: 'white'
      };
    }
    if (!color) {
      return {
        fill: getColor('warmgray-30'),
        stroke: 'white',
        fontWeight: 'normal'
      };
    }
    if (color.statusIsUnclear) {
      return {
        fill: getColor('warmgray-100'),
        stroke: 'white',
        fontWeight: 'bold'
      };
    }
    if (color.status.outcome === 'winner') {
      return {
        fill: 'white',
        stroke: getPaletteColor(color.palette, 'fgColored'),
        fontWeight: 'bold'
      };
    }
    if (color.status.outcome === 'runoff') {
      return {
        fill: getColor('runoff-contrast'),
        stroke: getColor('runoff'),
        fontWeight: 'bold'
      };
    }
    return {
      fill: getColor('warmgray-100'),
      stroke: getPaletteColor(color.palette, 'leadingBg'),
      fontWeight: 'bold'
    };
  }, [colors, format]);
};
export default useGetMapLabelProps;