/* eslint-disable @typescript-eslint/no-explicit-any */ import type {SanityDocument} from '@sanity/client' import {type Mutation, type NodePatchList} from '@sanity/mutate' export type Path = K extends string ? T[K] extends Record ? `${K}.${Path}` | K : K : never export type PathValue = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? PathValue : never : P extends keyof T ? T[P] : never export type DocumentsMutate = ( documentId: string, mutations: Mutation[], options?: {commit?: boolean | {debounce: number}}, ) => void export type DocumentsGet = >( documentId: string, ) => OptimisticDocument export type OptimisticDocumentPatches = Record> = | ((context: { draftId: string publishedId: string /** * @deprecated - use `getSnapshot` instead */ snapshot: SanityDocument | undefined getSnapshot: () => Promise | null> }) => Promise | NodePatchList) | NodePatchList export type OptimisticDocument = Record> = { /** * The document ID */ id: string /** * Commits any locally applied mutations to the remote document */ commit: () => void /** * @deprecated - use `getSnapshot` instead */ get: { (): SanityDocument | undefined

>(path: P): PathValue | undefined } /** * Returns a promise that resolves to the current document snapshot */ getSnapshot: () => Promise | null> /** * Applies the given patches to the document */ patch: ( patches: OptimisticDocumentPatches, options?: {commit?: boolean | {debounce: number}}, ) => void } export type OptimisticReducerAction = { document: T id: string originalId: string type: 'appear' | 'mutate' | 'disappear' } export type OptimisticReducer = (state: T, action: OptimisticReducerAction) => T