import getColor from "../../../utils/colors/getColor.js";
const shadow100 = 'drop-shadow(0 2px 5px rgb(0 0 0 / .1))';
const justClosedStateStyles = {
  boundaryProps: {
    fill: getColor('gop-alt04-20'),
    stroke: getColor('gop-alt04-40'),
    style: {
      filter: shadow100
    }
  },
  labelProps: {
    fill: getColor('warmgray-100')
  }
};
const closedStateStyles = {
  boundaryProps: {
    fill: getColor('gop-alt04-10'),
    stroke: getColor('gop-alt04-40'),
    style: {
      filter: shadow100
    }
  },
  labelProps: {
    fill: getColor('warmgray-60')
  }
};
const partiallyClosedStateStyles = {
  boundaryProps: {
    fill: 'white',
    stroke: getColor('warmgray-90'),
    strokeDasharray: '4',
    strokeWidth: '1',
    style: {
      filter: shadow100
    }
  },
  labelProps: {
    fill: getColor('warmgray-100')
  }
};
const closingSoonStateStyles = {
  boundaryProps: {
    fill: 'none',
    stroke: getColor('warmgray-100'),
    strokeDasharray: '4',
    strokeWidth: '1'
  },
  labelProps: {
    fill: getColor('warmgray-90')
  }
};
const futureStyles = {
  boundaryProps: {
    fill: 'none',
    stroke: 'none'
  },
  labelProps: {
    fill: getColor('warmgray-90')
  }
};
const getSoonCartoProps = function getSoonCartoProps(opts) {
  const {
    pollClosingTimes,
    currentTime
  } = opts;
  const statesMap = new Map();
  let previousEvent;
  let nextEvent;
  pollClosingTimes.forEach(time => {
    const delta = new Date(time.date) - currentTime;
    if (currentTime < new Date(time.date)) {
      if (!nextEvent || delta < new Date(nextEvent.date) - currentTime) {
        nextEvent = time;
      }
    } else {
      if (!previousEvent || delta > new Date(previousEvent.date) - currentTime) {
        previousEvent = time;
      }
      time.fullyClosed.forEach(state => {
        statesMap.set(state, closedStateStyles);
      });
      time.partiallyClosed.forEach(state => {
        statesMap.set(state, partiallyClosedStateStyles);
      });
    }
  });
  if (previousEvent) {
    const timeDiffPreviousEvent = currentTime - new Date(previousEvent.date);
    const diffMins = Math.round(timeDiffPreviousEvent / (1000 * 60));
    if (diffMins < 60) {
      previousEvent.fullyClosed.forEach(state => {
        statesMap.set(state, justClosedStateStyles);
      });
    }
  }
  if (nextEvent) {
    nextEvent.fullyClosed.forEach(state => {
      statesMap.set(state, closingSoonStateStyles);
    });
  }
  const getProps = properties => {
    if (!properties) return futureStyles;
    return statesMap.get(properties.code) || futureStyles;
  };
  return {
    boundaryProps: properties => getProps(properties).boundaryProps,
    labelProps: properties => getProps(properties).labelProps
  };
};
export default getSoonCartoProps;