"use client";

/**
 * PticaMap — SSR-safe wrapper around the rich Leaflet cockpit map. Leaflet
 * touches `window`, so PticaMapInner is loaded via dynamic(ssr:false) with a
 * dark loading placeholder (never imported into a server component).
 *
 * This wrapper owns the client-only data the rich map needs beyond the /analyze
 * payload: connection points (utility structures + distances) and the user's
 * custom POIs. Both are cached TanStack queries keyed by cad; they hydrate the
 * map's «Точки подключения ресурсов» list and the custom-POI layer.
 */

import dynamic from "next/dynamic";

import styles from "@/app/site-finder/analysis/[cad]/ptica/ptica.module.css";
import type { ParcelAnalysis } from "@/types/site-finder";
import { useConnectionPoints } from "@/hooks/useConnectionPoints";
import { useCustomPois } from "@/hooks/useCustomPois";
import type { DrawerKey } from "@/components/site-finder/ptica/drawers/drawer-keys";

const PticaMapInner = dynamic(
  () => import("@/components/site-finder/ptica/PticaMapInner"),
  {
    ssr: false,
    loading: () => <div className={styles.mapLoading}>Загрузка карты…</div>,
  },
);

interface Props {
  analysis: ParcelAnalysis;
  onOpenDrawer?: (key: DrawerKey) => void;
}

export function PticaMap({ analysis, onOpenDrawer }: Props) {
  const cad = analysis.cad_num;
  const { data: connectionPoints } = useConnectionPoints(cad);
  const { data: customPois } = useCustomPois(cad);

  return (
    <div className={styles.mapCard}>
      <PticaMapInner
        analysis={analysis}
        cad={cad}
        connectionPoints={connectionPoints}
        customPois={customPois}
        onOpenDrawer={onOpenDrawer}
      />
    </div>
  );
}
