import { INITIAL, useFilters } from "../../../stores/useFilters";
import { useEffect } from "react";
import { SliderComponent as Slider } from "../../../components/SliderComponent";

export const DeltaTrafficSlider = ({ fullRange }) => {
  const {
    filters: { deltaTraffic },
    setDeltaTraffic,
  } = useFilters();

  const handleAfterChange = (range) => setDeltaTraffic(range);

  useEffect(() => {
    if (!fullRange) return;

    const min = fullRange.delta_current[0];
    const max = fullRange.delta_current[1];

    if (
      deltaTraffic[0] === INITIAL.deltaTraffic[0] &&
      deltaTraffic[1] === INITIAL.deltaTraffic[1]
    ) {
      setDeltaTraffic([min, max]);
    }
  }, [fullRange, deltaTraffic]);

  return (
    <Slider
      title={"Расхождение факта с прогнозом, %"}
      value={deltaTraffic}
      onAfterChange={handleAfterChange}
      min={fullRange?.delta_current[0]}
      max={fullRange?.delta_current[1]}
      range
      showZeroMark={true}
    />
  );
};
