"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/hooks/useMapData.ts
var useMapData_exports = {};
__export(useMapData_exports, {
  createUseMapData: () => createUseMapData
});
module.exports = __toCommonJS(useMapData_exports);
var import_react = require("react");
function transformGeoJSONFeatures(geoJSON) {
  return geoJSON.features.map((feature) => ({
    id: feature.properties?.GEOCODIGO || feature.properties?.NOME || "",
    name: feature.properties?.NRE ? `NRE ${feature.properties.NRE}` : feature.properties?.NOME || "",
    value: feature.properties?.value ?? 0,
    accessCount: feature.properties?.totalAccess ?? 0,
    geoJson: feature
  }));
}
function transformRegionData(region) {
  if (!region.geoJson) {
    return null;
  }
  return {
    id: region.schoolGroupId,
    name: region.schoolGroupName,
    code: region.schoolGroupCode,
    value: region.value,
    accessCount: region.totalAccess,
    geoJson: region.geoJson
  };
}
var createUseMapData = (fetchMapDataApi) => {
  return (filters) => {
    const filtersRef = (0, import_react.useRef)(filters);
    filtersRef.current = filters;
    const filtersKey = JSON.stringify(filters);
    const fetchIdRef = (0, import_react.useRef)(0);
    const [data, setData] = (0, import_react.useState)([]);
    const [bounds, setBounds] = (0, import_react.useState)(null);
    const [geoJSON, setGeoJSON] = (0, import_react.useState)(null);
    const [loading, setLoading] = (0, import_react.useState)(true);
    const [error, setError] = (0, import_react.useState)(null);
    const fetchData = (0, import_react.useCallback)(async () => {
      const fetchId = ++fetchIdRef.current;
      setLoading(true);
      setError(null);
      try {
        const mapData = await fetchMapDataApi(filtersRef.current);
        if (fetchIdRef.current !== fetchId) return;
        const transformedRegions = mapData.data.geoJSON ? transformGeoJSONFeatures(mapData.data.geoJSON) : mapData.data.regions.map(transformRegionData).filter((r) => r !== null);
        setData(transformedRegions);
        setBounds(mapData.data.bounds);
        setGeoJSON(mapData.data.geoJSON ?? null);
      } catch (err) {
        if (fetchIdRef.current !== fetchId) return;
        const errorMessage = err instanceof Error ? err.message : "Erro ao carregar dados do mapa";
        setError(errorMessage);
        setData([]);
        setBounds(null);
        setGeoJSON(null);
      } finally {
        if (fetchIdRef.current === fetchId) {
          setLoading(false);
        }
      }
    }, [filtersKey]);
    (0, import_react.useEffect)(() => {
      fetchData();
    }, [fetchData]);
    return {
      data,
      bounds,
      geoJSON,
      loading,
      error,
      refetch: fetchData
    };
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createUseMapData
});
//# sourceMappingURL=index.js.map