import { FieldContent, isColorContent, } from "@prismicio/types-internal/lib/content" import { FieldContentType } from "@prismicio/types-internal/lib/content" import type { Color } from "@prismicio/types-internal/lib/customtypes" import { DiffChange, DiffOperation, } from "@prismicio/types-internal/lib/customtypes/diff" import type { NestableMock, Patch } from "../../Mock" import type { MockConfig } from "../../MockConfig" function random() { const hexLetters = "0123456789abcdef" let color = "#" for (let i = 0; i < 6; i++) { color += hexLetters[Math.floor(Math.random() * 16)] } return color } export type ColorMockConfig = MockConfig export const ColorMock: NestableMock = { generate(_def: Color, config?: ColorMockConfig): FieldContent { const value = config?.value || random() return { __TYPE__: FieldContentType, value, type: "Color", } }, applyPatch(data: Patch): | { result: FieldContent | undefined } | undefined { if (data.diff.op === DiffOperation.Removed) return { result: undefined } if (data.diff.value.type === "Color") { const patched = this.patch( data.diff, isColorContent(data.content) ? data.content : undefined, data.config?.type === "Color" ? data.config : undefined, ) return { result: patched } } return }, patch( diff: DiffChange, _content: FieldContent, config?: ColorMockConfig, ): FieldContent | undefined { switch (diff.op) { case DiffOperation.Removed: return case DiffOperation.Updated: case DiffOperation.Added: return this.generate(diff.value, config) } }, }