import { GeoPointContent, GeoPointContentType, isGeoPointContent, } from "@prismicio/types-internal/lib/content" import type { GeoPoint } from "@prismicio/types-internal/lib/customtypes" import { DiffChange, DiffOperation, } from "@prismicio/types-internal/lib/customtypes/diff" import places from "../../dataset/geopoint.json" import type { NestableMock, Patch } from "../../Mock" import type { MockConfig } from "../../MockConfig" export type GeoPointMockConfig = MockConfig< GeoPoint["type"], { latitude: number longitude: number } > function random(): { latitude: number; longitude: number } { const place = places[Math.floor(Math.random() * places.length)] if (!place) throw new Error("Something happened during GeoPoint generation.") return { latitude: place.points.latitude, longitude: place.points.longitude, } } export const GeoPointMock: NestableMock< GeoPoint, GeoPointContent, GeoPointMockConfig > = { generate(_def: GeoPoint, config?: GeoPointMockConfig): GeoPointContent { const value = config?.value || random() return { __TYPE__: GeoPointContentType, position: { lat: value.latitude, lng: value.longitude, }, } }, applyPatch(data: Patch): | { result: GeoPointContent | undefined } | undefined { if (data.diff.op === DiffOperation.Removed) return { result: undefined } if (data.diff.value.type === "GeoPoint") { const patched = this.patch( data.diff, isGeoPointContent(data.content) ? data.content : undefined, data.config?.type === "GeoPoint" ? data.config : undefined, ) return { result: patched } } return }, patch( diff: DiffChange, _content: GeoPointContent, config?: GeoPointMockConfig, ): GeoPointContent | undefined { switch (diff.op) { case DiffOperation.Removed: return case DiffOperation.Updated: case DiffOperation.Added: return this.generate(diff.value, config) } }, }