// ----------------------------------------------------------------------------- // Author: Simon-Pierre Boucher // Contact: contact@spboucher.ai // Project: Groupe Ka / Ka Maps (House-Ka integration) // kamaps/adapter.ts : House-Ka → MapProperty adapter. // ----------------------------------------------------------------------------- import type { BoundsQuery, BoundsQueryResult, KaDataAdapter, MapProperty, } from "@groupe-ka/ka-maps"; import { bboxToString } from "@groupe-ka/ka-maps"; import type { ListingFilters } from "../api"; interface HouseKaFeatureProps { uid: string; title: string | null; address: string | null; price: number | null; price_label: string | null; property_type: string | null; bedrooms: number | null; bathrooms: number | null; source: string; city: string | null; sector: string | null; image: string | null; } interface HouseKaFC { type: "FeatureCollection"; features: { geometry: { coordinates: [number, number] }; properties: HouseKaFeatureProps; }[]; totalGeocoded?: number; totalMatching?: number; } function toMapProperty( coords: [number, number], p: HouseKaFeatureProps, ): MapProperty { return { id: p.uid, appSource: "house-ka", longitude: coords[0], latitude: coords[1], kind: "listing", listingType: "sale", price: p.price ?? undefined, propertyType: p.property_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: `/property/${encodeURIComponent(p.uid)}`, highlight: false, extra: { title: p.title, priceLabel: p.price_label, source: p.source, }, }; } /** Viewport→data adapter for the House-Ka map. */ export const houseKaMapAdapter: KaDataAdapter = { id: "house-ka-listings", appSource: "house-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(`Map: API ${res.status}`); const data = (await res.json()) as HouseKaFC; return { properties: data.features.map((f) => toMapProperty(f.geometry.coordinates, f.properties), ), totalCount: data.totalMatching, }; }, };