import { EmbedContent, EmbedContentType, isEmbedContent, } from "@prismicio/types-internal/lib/content" import type { Embed } from "@prismicio/types-internal/lib/customtypes" import { DiffChange, DiffOperation, } from "@prismicio/types-internal/lib/customtypes/diff" import embedDataset from "../../dataset/embed.json" import type { NestableMock, Patch } from "../../Mock" import type { MockConfig } from "../../MockConfig" export type EmbedMockConfig = MockConfig function random(content: EmbedContent | undefined): EmbedContent { const embed = content || embedDataset[Math.floor(Math.random() * embedDataset.length)]?.oembed if (!embed) throw new Error("Something happened during Embed generation.") return { ...embed, all: embed, __TYPE__: EmbedContentType } } export const EmbedMock: NestableMock = { generate(_def: Embed, config?: EmbedMockConfig): EmbedContent { return random(config?.value) }, applyPatch(data: Patch): | { result: EmbedContent | undefined } | undefined { if (data.diff.op === DiffOperation.Removed) return { result: undefined } if (data.diff.value.type === "Embed") { const patched = this.patch( data.diff, isEmbedContent(data.content) ? data.content : undefined, data.config?.type === "Embed" ? data.config : undefined, ) return { result: patched } } return }, patch( diff: DiffChange, _content: EmbedContent, config?: EmbedMockConfig, ): EmbedContent | undefined { switch (diff.op) { case DiffOperation.Removed: return case DiffOperation.Updated: case DiffOperation.Added: return this.generate(diff.value, config) } }, }