"use client";

import { useMutation } from "@tanstack/react-query";
import type { FeatureCollection } from "geojson";

import { apiFetch } from "@/lib/api";

export interface IsochronesResponse {
  cad_num: string;
  lat: number;
  lon: number;
  mode: string;
  times_min: number[];
  geojson: FeatureCollection;
  source: string;
  note: string;
}

export function useIsochrones() {
  return useMutation({
    mutationFn: ({
      cadNum,
      mode,
      timesMin,
    }: {
      cadNum: string;
      mode: string;
      timesMin: number[];
    }) =>
      apiFetch<IsochronesResponse>(
        `/api/v1/parcels/${encodeURIComponent(cadNum)}/isochrones?mode=${mode}&${timesMin.map((t) => `times_min=${t}`).join("&")}`,
      ),
  });
}
