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

export const FactTrafficSlider = ({ fullRange }) => {
  const {
    filters: { factTraffic },
    setFactTraffic,
  } = useWorkingPointsFilters();

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

  useEffect(() => {
    const min = fullRange.factTraffic[0];
    const max = fullRange.factTraffic[1];

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

  return (
    <Slider
      title={"Фактический трафик"}
      value={factTraffic}
      onAfterChange={handleAfterChange}
      min={fullRange.factTraffic[0]}
      max={fullRange.factTraffic[1]}
      range
    />
  );
};
