import { Button } from "antd";
import { useEffect } from "react";
import { useClickedPointConfig } from "../../../stores/useClickedPointConfig";
import { usePendingPointsFilters } from "../../../stores/usePendingPointsFilters";
import { usePointSelection } from "../../../stores/usePointSelection";
import { doesMatchFilter } from "../../../utils.js";
import { FeatureProperties } from "./FeatureProperties";

export const PendingPointPopup = ({ feature, point }) => {
  const { include, selection, exclude } = usePointSelection();
  const { setClickedPointConfig } = useClickedPointConfig();
  const { filters, ranges } = usePendingPointsFilters();

  const featureId = feature.properties.id;

  const isSelected =
    (doesMatchFilter(filters, ranges, feature) && !selection.excluded.has(featureId)) ||
    selection.included.has(featureId);

  useEffect(
    () => setClickedPointConfig(featureId, isSelected),
    [featureId, isSelected]
  );

  const handleSelect = () => {
    if (isSelected) {
      exclude(featureId);
    } else {
      include(featureId);
    }
  };

  const canEdit = true

  return (
    <>
      <FeatureProperties feature={feature} point={point} />
      {canEdit && (
        <Button
          type="primary"
          className="mt-2 mx-auto"
          block
          onClick={handleSelect}
        >
          {isSelected ? "Исключить из выборки" : "Добавить в выборку"}
        </Button>
      )}
    </>
  );
};
