import { extent } from 'd3-array';
import { scaleUtc } from 'd3-scale';
import { interpolateRgbBasis } from 'd3-interpolate';
import getColor from "../../../utils/colors/getColor.js";
const getGradientCartoProps = function getGradientCartoProps(opts) {
  const {
    closingTimes
  } = opts;
  const times = [...new Set([...closingTimes.byState.values()])].sort();
  const colorInterpolator = interpolateRgbBasis([getColor('gop-alt04-40'), getColor('gop-alt04-30'), getColor('gop-alt04-20'), getColor('gop-alt04-10'), 'white']);
  const domain = extent(times);
  const timeScale = scaleUtc().domain(domain.map(t => new Date(t))).range([0, 1]);
  const ticks = timeScale.ticks();
  return {
    legendProps: {
      limits: domain,
      colorStops: ticks.map(t => colorInterpolator(timeScale(t)))
    },
    cartoProps: {
      boundaryProps: properties => {
        const time = closingTimes.byState.get(properties.code);
        const color = colorInterpolator(timeScale(new Date(time)));
        return {
          fill: color,
          stroke: getColor('gop-alt04-40')
        };
      },
      labelProps: properties => {
        const time = closingTimes.byState.get(properties.code);
        const level = timeScale(new Date(time));
        return {
          fill: level < 0.1 ? 'white' : getColor('warmgray-90')
        };
      }
    }
  };
};
export default getGradientCartoProps;