// ----------------------------------------------------------------------------- // Author: Simon-Pierre Boucher // Contact: contact@spboucher.ai // Project: Toit-Ka // kamaps/adapter.ts : adaptateur Toit-Ka -> MapProperty (framework Ka Maps). // L'univers (louer/acheter) devient listingType rent/sale — chaque famille a // son style de marqueur (voir theme.ts). // ----------------------------------------------------------------------------- import type { BoundsQuery, BoundsQueryResult, KaDataAdapter, MapProperty, } from "@groupe-ka/ka-maps"; import { bboxToString } from "@groupe-ka/ka-maps"; import type { ListingFilters } from "../api"; interface ToitKaFeatureProps { uid: string; tx: "louer" | "acheter"; title: string | null; address: string | null; price: number | null; price_label: string | null; type: string | null; bedrooms: number | null; bathrooms: number | null; source: string; city: string | null; sector: string | null; image: string | null; } interface ToitKaFC { type: "FeatureCollection"; features: { geometry: { coordinates: [number, number] }; properties: ToitKaFeatureProps; }[]; totalGeocoded?: number; totalMatching?: number; } function toMapProperty(coords: [number, number], p: ToitKaFeatureProps): MapProperty { const louer = p.tx === "louer"; return { id: p.uid, appSource: louer ? "lou-ka" : "immo-ka", longitude: coords[0], latitude: coords[1], kind: "listing", listingType: louer ? "rent" : "sale", price: p.price ?? undefined, propertyType: p.type ?? undefined, bedrooms: p.bedrooms ?? undefined, bathrooms: p.bathrooms ?? undefined, address: p.address ?? p.title ?? undefined, city: p.city ?? undefined, region: p.sector ?? undefined, thumbnailUrl: p.image ?? undefined, originalUrl: `/annonce/${encodeURIComponent(p.uid)}`, extra: { tx: p.tx, title: p.title, priceLabel: p.price_label, source: p.source, }, }; } /** Adaptateur viewport -> données pour la carte Toit-Ka. */ export const toitKaMapAdapter: KaDataAdapter = { id: "toit-ka-listings", appSource: "immo-ka", async fetchInBounds(query: BoundsQuery): Promise { const params = new URLSearchParams(); const filters = (query.filters ?? {}) as Partial; for (const [k, v] of Object.entries(filters)) { if (v && k !== "sort") params.set(k, String(v)); } params.set("bbox", bboxToString(query.bbox)); params.set("limit", "3000"); const res = await fetch(`/api/listings.geojson?${params}`, { signal: query.signal, }); if (!res.ok) throw new Error(`Carte : API ${res.status}`); const data = (await res.json()) as ToitKaFC; return { properties: data.features.map((f) => toMapProperty(f.geometry.coordinates, f.properties), ), totalCount: data.totalMatching, }; }, };