import type {ClientPerspective, ContentSourceMapDocuments} from '@sanity/client'
import {
  MutationEvent as MutationEvent_2,
  ReconnectEvent,
  SanityClient,
  SanityDocument,
  WelcomeEvent,
} from '@sanity/client'
import type {StudioPathLike} from '@sanity/client/csm'
import type {Node as Node_2} from '@sanity/comlink'
import type {InsertMenuOptions} from '@sanity/insert-menu'
import {Mutation, NodePatchList} from '@sanity/mutate'
import {
  createSharedListener,
  DocumentMutatorMachineInput,
  Mutation as Mutation_2,
  MutationGroup,
  RawPatch,
  SanityDocumentBase,
  Transaction,
} from '@sanity/mutate/_unstable_machine'
import type {ArrayOptions, PreviewValue} from '@sanity/types'
import {Observable} from 'rxjs'
import {
  ActorRef,
  ActorRefFrom,
  ActorRefFromLogic,
  AnyActorLogic,
  AnyActorRef,
  AnyEventObject,
  ConditionalRequired,
  EventObject,
  GetConcreteByKey,
  InputFrom,
  IsNotNever,
  MachineSnapshot,
  MetaObject,
  NonReducibleUnknown,
  ObservableActorLogic,
  PromiseActorLogic,
  RequiredActorOptions,
  Snapshot,
  StateMachine,
  StateValue,
  Values,
} from 'xstate'

export declare let actor: MutatorActor | EmptyActor

export declare const createDatasetMutator: (comlink: VisualEditingNode) => StateMachine<
  {
    client: SanityClient
    /** A shared listener can be provided, if not it'll be created using `client.listen()` */
    sharedListener?: ReturnType<typeof createSharedListener>
    documents: Record<string, ActorRefFrom<ReturnType<typeof createDocumentMutator>>>
  },
  | {
      type: 'sync'
      id: string
      document: SanityDocumentBase
    }
  | {
      type: 'mutation'
      id: string
      effects: {
        apply: RawPatch
      }
      previousRev: string
      resultRev: string
    }
  | {
      type: 'rebased.local'
      id: string
      document: SanityDocumentBase
    }
  | {
      type: 'rebased.remote'
      id: string
      document: SanityDocumentBase
    }
  | {
      type: 'pristine'
      id: string
    }
  | {
      type: 'observe'
      documentId: string
    }
  | {
      type: 'unobserve'
      documentId: string
    }
  | {
      type: 'add document actor'
      documentId: string
    }
  | {
      type: 'stop document actor'
      documentId: string
    },
  {
    [x: string]:
      | ActorRefFromLogic<
          StateMachine<
            {
              client: SanityClient
              sharedListener?: ReturnType<typeof createSharedListener>
              id: string
              cache?: Map<string, SanityDocument<DocumentType> | null>
              remote: SanityDocument<DocumentType> | null | undefined
              local: SanityDocument<DocumentType> | null | undefined
              mutationEvents: MutationEvent_2[]
              stagedChanges: MutationGroup[]
              stashedChanges: MutationGroup[]
              error: unknown
              fetchRemoteSnapshotAttempts: number
              submitTransactionsAttempts: number
            },
            | WelcomeEvent
            | ReconnectEvent
            | MutationEvent_2
            | {
                type: 'error'
              }
            | {
                type: 'retry'
              }
            | {
                type: 'connect'
              }
            | {
                type: 'reconnect'
              }
            | {
                type: 'welcome'
              }
            | {
                type: 'mutate'
                mutations: Mutation_2[]
              }
            | {
                type: 'submit'
              }
            | {
                type: 'xstate.done.actor.getDocument'
                output: SanityDocument<DocumentType>
              }
            | {
                type: 'xstate.done.actor.submitTransactions'
                output: undefined
              },
            {
              [x: string]:
                | ActorRefFromLogic<
                    PromiseActorLogic<
                      void | SanityDocument<Record<string, any>> | undefined,
                      {
                        client: SanityClient
                        id: string
                      },
                      EventObject
                    >
                  >
                | ActorRefFromLogic<
                    PromiseActorLogic<
                      void,
                      {
                        client: SanityClient
                        transactions: Transaction[]
                      },
                      EventObject
                    >
                  >
                | ActorRefFromLogic<
                    ObservableActorLogic<
                      WelcomeEvent | ReconnectEvent | MutationEvent_2,
                      {
                        listener: ReturnType<typeof createSharedListener>
                        id: string
                      },
                      EventObject
                    >
                  >
                | undefined
              getDocument?:
                | ActorRefFromLogic<
                    PromiseActorLogic<
                      void | SanityDocument<Record<string, any>> | undefined,
                      {
                        client: SanityClient
                        id: string
                      },
                      EventObject
                    >
                  >
                | undefined
              submitTransactions?:
                | ActorRefFromLogic<
                    PromiseActorLogic<
                      void,
                      {
                        client: SanityClient
                        transactions: Transaction[]
                      },
                      EventObject
                    >
                  >
                | undefined
            },
            Values<{
              'fetch remote snapshot': {
                src: 'fetch remote snapshot'
                logic: PromiseActorLogic<
                  void | SanityDocument<Record<string, any>> | undefined,
                  {
                    client: SanityClient
                    id: string
                  },
                  EventObject
                >
                id: 'getDocument'
              }
              'submit mutations as transactions': {
                src: 'submit mutations as transactions'
                logic: PromiseActorLogic<
                  void,
                  {
                    client: SanityClient
                    transactions: Transaction[]
                  },
                  EventObject
                >
                id: 'submitTransactions'
              }
              'server-sent events': {
                src: 'server-sent events'
                logic: ObservableActorLogic<
                  ReconnectEvent | WelcomeEvent | MutationEvent_2,
                  {
                    listener: ReturnType<typeof createSharedListener>
                    id: string
                  },
                  EventObject
                >
                id: string | undefined
              }
            }>,
            Values<{
              'assign error to context': {
                type: 'assign error to context'
                params: NonReducibleUnknown
              }
              'clear error from context': {
                type: 'clear error from context'
                params: NonReducibleUnknown
              }
              'connect to server-sent events': {
                type: 'connect to server-sent events'
                params: NonReducibleUnknown
              }
              'listen to server-sent events': {
                type: 'listen to server-sent events'
                params: NonReducibleUnknown
              }
              'stop listening to server-sent events': {
                type: 'stop listening to server-sent events'
                params: NonReducibleUnknown
              }
              'buffer remote mutation events': {
                type: 'buffer remote mutation events'
                params: NonReducibleUnknown
              }
              'restore stashed changes': {
                type: 'restore stashed changes'
                params: NonReducibleUnknown
              }
              'rebase fetched remote snapshot': {
                type: 'rebase fetched remote snapshot'
                params: NonReducibleUnknown
              }
              'apply mendoza patch': {
                type: 'apply mendoza patch'
                params: NonReducibleUnknown
              }
              'increment fetch attempts': {
                type: 'increment fetch attempts'
                params: NonReducibleUnknown
              }
              'reset fetch attempts': {
                type: 'reset fetch attempts'
                params: NonReducibleUnknown
              }
              'increment submit attempts': {
                type: 'increment submit attempts'
                params: NonReducibleUnknown
              }
              'reset submit attempts': {
                type: 'reset submit attempts'
                params: NonReducibleUnknown
              }
              'stage mutation': {
                type: 'stage mutation'
                params: NonReducibleUnknown
              }
              'stash mutation': {
                type: 'stash mutation'
                params: NonReducibleUnknown
              }
              'rebase local snapshot': {
                type: 'rebase local snapshot'
                params: NonReducibleUnknown
              }
              'send pristine event to parent': {
                type: 'send pristine event to parent'
                params: NonReducibleUnknown
              }
              'send sync event to parent': {
                type: 'send sync event to parent'
                params: NonReducibleUnknown
              }
              'send mutation event to parent': {
                type: 'send mutation event to parent'
                params: NonReducibleUnknown
              }
            }>,
            never,
            'fetchRemoteSnapshotTimeout' | 'submitTransactionsTimeout',
            | 'disconnected'
            | 'connecting'
            | 'reconnecting'
            | 'connectFailure'
            | {
                connected:
                  | 'loading'
                  | 'loadFailure'
                  | {
                      loaded: 'pristine' | 'dirty' | 'submitting' | 'submitFailure'
                    }
              },
            'error' | 'busy' | 'ready',
            DocumentMutatorMachineInput,
            NonReducibleUnknown,
            EventObject,
            MetaObject,
            {
              readonly id: 'document-mutator'
              readonly context: ({
                input,
              }: {
                spawn: {
                  <
                    TSrc extends
                      | 'fetch remote snapshot'
                      | 'submit mutations as transactions'
                      | 'server-sent events',
                  >(
                    logic: TSrc,
                    ...[options]:
                      | ({
                          src: 'fetch remote snapshot'
                          logic: PromiseActorLogic<
                            void | SanityDocument<Record<string, any>> | undefined,
                            {
                              client: SanityClient
                              id: string
                            },
                            EventObject
                          >
                          id: 'getDocument'
                        } extends infer T
                          ? T extends {
                              src: 'fetch remote snapshot'
                              logic: PromiseActorLogic<
                                void | SanityDocument<Record<string, any>> | undefined,
                                {
                                  client: SanityClient
                                  id: string
                                },
                                EventObject
                              >
                              id: 'getDocument'
                            }
                            ? T extends {
                                src: TSrc
                              }
                              ? ConditionalRequired<
                                  [
                                    options?:
                                      | ({
                                          id?: T['id'] | undefined
                                          systemId?: string
                                          input?: InputFrom<T['logic']> | undefined
                                          syncSnapshot?: boolean
                                        } & {[K in RequiredActorOptions<T>]: unknown})
                                      | undefined,
                                  ],
                                  IsNotNever<RequiredActorOptions<T>>
                                >
                              : never
                            : never
                          : never)
                      | ({
                          src: 'submit mutations as transactions'
                          logic: PromiseActorLogic<
                            void,
                            {
                              client: SanityClient
                              transactions: Transaction[]
                            },
                            EventObject
                          >
                          id: 'submitTransactions'
                        } extends infer T_1
                          ? T_1 extends {
                              src: 'submit mutations as transactions'
                              logic: PromiseActorLogic<
                                void,
                                {
                                  client: SanityClient
                                  transactions: Transaction[]
                                },
                                EventObject
                              >
                              id: 'submitTransactions'
                            }
                            ? T_1 extends {
                                src: TSrc
                              }
                              ? ConditionalRequired<
                                  [
                                    options?:
                                      | ({
                                          id?: T_1['id'] | undefined
                                          systemId?: string
                                          input?: InputFrom<T_1['logic']> | undefined
                                          syncSnapshot?: boolean
                                        } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                                      | undefined,
                                  ],
                                  IsNotNever<RequiredActorOptions<T_1>>
                                >
                              : never
                            : never
                          : never)
                      | ({
                          src: 'server-sent events'
                          logic: ObservableActorLogic<
                            ReconnectEvent | WelcomeEvent | MutationEvent_2,
                            {
                              listener: ReturnType<typeof createSharedListener>
                              id: string
                            },
                            EventObject
                          >
                          id: string | undefined
                        } extends infer T_2
                          ? T_2 extends {
                              src: 'server-sent events'
                              logic: ObservableActorLogic<
                                ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                {
                                  listener: ReturnType<typeof createSharedListener>
                                  id: string
                                },
                                EventObject
                              >
                              id: string | undefined
                            }
                            ? T_2 extends {
                                src: TSrc
                              }
                              ? ConditionalRequired<
                                  [
                                    options?:
                                      | ({
                                          id?: T_2['id'] | undefined
                                          systemId?: string
                                          input?: InputFrom<T_2['logic']> | undefined
                                          syncSnapshot?: boolean
                                        } & {[K_2 in RequiredActorOptions<T_2>]: unknown})
                                      | undefined,
                                  ],
                                  IsNotNever<RequiredActorOptions<T_2>>
                                >
                              : never
                            : never
                          : never)
                  ): ActorRefFromLogic<
                    GetConcreteByKey<
                      Values<{
                        'fetch remote snapshot': {
                          src: 'fetch remote snapshot'
                          logic: PromiseActorLogic<
                            void | SanityDocument<Record<string, any>> | undefined,
                            {
                              client: SanityClient
                              id: string
                            },
                            EventObject
                          >
                          id: 'getDocument'
                        }
                        'submit mutations as transactions': {
                          src: 'submit mutations as transactions'
                          logic: PromiseActorLogic<
                            void,
                            {
                              client: SanityClient
                              transactions: Transaction[]
                            },
                            EventObject
                          >
                          id: 'submitTransactions'
                        }
                        'server-sent events': {
                          src: 'server-sent events'
                          logic: ObservableActorLogic<
                            ReconnectEvent | WelcomeEvent | MutationEvent_2,
                            {
                              listener: ReturnType<typeof createSharedListener>
                              id: string
                            },
                            EventObject
                          >
                          id: string | undefined
                        }
                      }>,
                      'src',
                      TSrc
                    >['logic']
                  >
                  <TLogic extends AnyActorLogic>(
                    src: TLogic,
                    options?:
                      | {
                          id?: never
                          systemId?: string
                          input?: InputFrom<TLogic> | undefined
                          syncSnapshot?: boolean
                        }
                      | undefined,
                  ): ActorRefFromLogic<TLogic>
                }
                input: DocumentMutatorMachineInput
                self: ActorRef<
                  MachineSnapshot<
                    {
                      client: SanityClient
                      sharedListener?: ReturnType<typeof createSharedListener>
                      id: string
                      cache?: Map<string, SanityDocument<DocumentType> | null>
                      remote: SanityDocument<DocumentType> | null | undefined
                      local: SanityDocument<DocumentType> | null | undefined
                      mutationEvents: MutationEvent_2[]
                      stagedChanges: MutationGroup[]
                      stashedChanges: MutationGroup[]
                      error: unknown
                      fetchRemoteSnapshotAttempts: number
                      submitTransactionsAttempts: number
                    },
                    | ReconnectEvent
                    | WelcomeEvent
                    | MutationEvent_2
                    | {
                        type: 'error'
                      }
                    | {
                        type: 'retry'
                      }
                    | {
                        type: 'connect'
                      }
                    | {
                        type: 'reconnect'
                      }
                    | {
                        type: 'welcome'
                      }
                    | {
                        type: 'mutate'
                        mutations: Mutation_2[]
                      }
                    | {
                        type: 'submit'
                      }
                    | {
                        type: 'xstate.done.actor.getDocument'
                        output: SanityDocument<DocumentType>
                      }
                    | {
                        type: 'xstate.done.actor.submitTransactions'
                        output: undefined
                      },
                    Record<string, AnyActorRef | undefined>,
                    StateValue,
                    string,
                    unknown,
                    any,
                    any
                  >,
                  | ReconnectEvent
                  | WelcomeEvent
                  | MutationEvent_2
                  | {
                      type: 'error'
                    }
                  | {
                      type: 'retry'
                    }
                  | {
                      type: 'connect'
                    }
                  | {
                      type: 'reconnect'
                    }
                  | {
                      type: 'welcome'
                    }
                  | {
                      type: 'mutate'
                      mutations: Mutation_2[]
                    }
                  | {
                      type: 'submit'
                    }
                  | {
                      type: 'xstate.done.actor.getDocument'
                      output: SanityDocument<DocumentType>
                    }
                  | {
                      type: 'xstate.done.actor.submitTransactions'
                      output: undefined
                    },
                  AnyEventObject
                >
              }) => {
                client: SanityClient
                sharedListener:
                  | Observable<ReconnectEvent | WelcomeEvent | MutationEvent_2>
                  | undefined
                id: string
                remote: undefined
                local: undefined
                mutationEvents: never[]
                stagedChanges: never[]
                stashedChanges: never[]
                error: undefined
                fetchRemoteSnapshotAttempts: number
                submitTransactionsAttempts: number
                cache: Map<string, SanityDocument<DocumentType> | null> | undefined
              }
              readonly entry: readonly ['connect to server-sent events']
              readonly on: {
                readonly mutate: {
                  readonly actions: readonly ['rebase local snapshot', 'stage mutation']
                }
              }
              readonly initial: 'disconnected'
              readonly states: {
                readonly disconnected: {
                  readonly on: {
                    readonly connect: {
                      readonly target: 'connecting'
                      readonly actions: readonly ['listen to server-sent events']
                    }
                  }
                }
                readonly connecting: {
                  readonly on: {
                    readonly welcome: 'connected'
                    readonly reconnect: 'reconnecting'
                    readonly error: 'connectFailure'
                  }
                  readonly tags: readonly ['busy']
                }
                readonly connectFailure: {
                  readonly on: {
                    readonly connect: {
                      readonly target: 'connecting'
                      readonly actions: readonly ['listen to server-sent events']
                    }
                  }
                  readonly entry: readonly [
                    'stop listening to server-sent events',
                    'assign error to context',
                  ]
                  readonly exit: readonly ['clear error from context']
                  readonly tags: readonly ['error']
                }
                readonly reconnecting: {
                  readonly on: {
                    readonly welcome: {
                      readonly target: 'connected'
                    }
                    readonly error: {
                      readonly target: 'connectFailure'
                    }
                  }
                  readonly tags: readonly ['busy', 'error']
                }
                readonly connected: {
                  readonly on: {
                    readonly mutation: {
                      readonly actions: readonly ['buffer remote mutation events']
                    }
                    readonly reconnect: 'reconnecting'
                  }
                  readonly entry: readonly ['clear error from context']
                  readonly initial: 'loading'
                  readonly states: {
                    readonly loading: {
                      readonly invoke: {
                        readonly src: 'fetch remote snapshot'
                        readonly id: 'getDocument'
                        readonly input: ({
                          context,
                        }: {
                          context: {
                            client: SanityClient
                            sharedListener?: ReturnType<typeof createSharedListener>
                            id: string
                            cache?: Map<string, SanityDocument<DocumentType> | null>
                            remote: SanityDocument<DocumentType> | null | undefined
                            local: SanityDocument<DocumentType> | null | undefined
                            mutationEvents: MutationEvent_2[]
                            stagedChanges: MutationGroup[]
                            stashedChanges: MutationGroup[]
                            error: unknown
                            fetchRemoteSnapshotAttempts: number
                            submitTransactionsAttempts: number
                          }
                          event:
                            | ReconnectEvent
                            | WelcomeEvent
                            | MutationEvent_2
                            | {
                                type: 'error'
                              }
                            | {
                                type: 'retry'
                              }
                            | {
                                type: 'connect'
                              }
                            | {
                                type: 'reconnect'
                              }
                            | {
                                type: 'welcome'
                              }
                            | {
                                type: 'mutate'
                                mutations: Mutation_2[]
                              }
                            | {
                                type: 'submit'
                              }
                            | {
                                type: 'xstate.done.actor.getDocument'
                                output: SanityDocument<DocumentType>
                              }
                            | {
                                type: 'xstate.done.actor.submitTransactions'
                                output: undefined
                              }
                          self: ActorRef<
                            MachineSnapshot<
                              {
                                client: SanityClient
                                sharedListener?: ReturnType<typeof createSharedListener>
                                id: string
                                cache?: Map<string, SanityDocument<DocumentType> | null>
                                remote: SanityDocument<DocumentType> | null | undefined
                                local: SanityDocument<DocumentType> | null | undefined
                                mutationEvents: MutationEvent_2[]
                                stagedChanges: MutationGroup[]
                                stashedChanges: MutationGroup[]
                                error: unknown
                                fetchRemoteSnapshotAttempts: number
                                submitTransactionsAttempts: number
                              },
                              | ReconnectEvent
                              | WelcomeEvent
                              | MutationEvent_2
                              | {
                                  type: 'error'
                                }
                              | {
                                  type: 'retry'
                                }
                              | {
                                  type: 'connect'
                                }
                              | {
                                  type: 'reconnect'
                                }
                              | {
                                  type: 'welcome'
                                }
                              | {
                                  type: 'mutate'
                                  mutations: Mutation_2[]
                                }
                              | {
                                  type: 'submit'
                                }
                              | {
                                  type: 'xstate.done.actor.getDocument'
                                  output: SanityDocument<DocumentType>
                                }
                              | {
                                  type: 'xstate.done.actor.submitTransactions'
                                  output: undefined
                                },
                              Record<string, AnyActorRef>,
                              StateValue,
                              string,
                              unknown,
                              any,
                              any
                            >,
                            | ReconnectEvent
                            | WelcomeEvent
                            | MutationEvent_2
                            | {
                                type: 'error'
                              }
                            | {
                                type: 'retry'
                              }
                            | {
                                type: 'connect'
                              }
                            | {
                                type: 'reconnect'
                              }
                            | {
                                type: 'welcome'
                              }
                            | {
                                type: 'mutate'
                                mutations: Mutation_2[]
                              }
                            | {
                                type: 'submit'
                              }
                            | {
                                type: 'xstate.done.actor.getDocument'
                                output: SanityDocument<DocumentType>
                              }
                            | {
                                type: 'xstate.done.actor.submitTransactions'
                                output: undefined
                              },
                            AnyEventObject
                          >
                        }) => {
                          client: SanityClient
                          id: string
                        }
                        readonly onError: {
                          readonly target: 'loadFailure'
                        }
                        readonly onDone: {
                          readonly target: 'loaded'
                          readonly actions: readonly [
                            'rebase fetched remote snapshot',
                            'reset fetch attempts',
                          ]
                        }
                      }
                      readonly tags: readonly ['busy']
                    }
                    readonly loaded: {
                      readonly entry: readonly ['send sync event to parent']
                      readonly on: {
                        readonly mutation: {
                          readonly actions: readonly [
                            'apply mendoza patch',
                            'send mutation event to parent',
                          ]
                        }
                      }
                      readonly initial: 'pristine'
                      readonly states: {
                        readonly pristine: {
                          readonly on: {
                            readonly mutate: {
                              readonly actions: readonly ['rebase local snapshot', 'stage mutation']
                              readonly target: 'dirty'
                            }
                          }
                          readonly tags: readonly ['ready']
                        }
                        readonly dirty: {
                          readonly on: {
                            readonly submit: 'submitting'
                          }
                          readonly tags: readonly ['ready']
                        }
                        readonly submitting: {
                          readonly on: {
                            readonly mutate: {
                              readonly actions: readonly ['rebase local snapshot', 'stash mutation']
                            }
                          }
                          readonly invoke: {
                            readonly src: 'submit mutations as transactions'
                            readonly id: 'submitTransactions'
                            readonly input: ({
                              context,
                            }: {
                              context: {
                                client: SanityClient
                                sharedListener?: ReturnType<typeof createSharedListener>
                                id: string
                                cache?: Map<string, SanityDocument<DocumentType> | null>
                                remote: SanityDocument<DocumentType> | null | undefined
                                local: SanityDocument<DocumentType> | null | undefined
                                mutationEvents: MutationEvent_2[]
                                stagedChanges: MutationGroup[]
                                stashedChanges: MutationGroup[]
                                error: unknown
                                fetchRemoteSnapshotAttempts: number
                                submitTransactionsAttempts: number
                              }
                              event:
                                | ReconnectEvent
                                | WelcomeEvent
                                | MutationEvent_2
                                | {
                                    type: 'error'
                                  }
                                | {
                                    type: 'retry'
                                  }
                                | {
                                    type: 'connect'
                                  }
                                | {
                                    type: 'reconnect'
                                  }
                                | {
                                    type: 'welcome'
                                  }
                                | {
                                    type: 'mutate'
                                    mutations: Mutation_2[]
                                  }
                                | {
                                    type: 'submit'
                                  }
                                | {
                                    type: 'xstate.done.actor.getDocument'
                                    output: SanityDocument<DocumentType>
                                  }
                                | {
                                    type: 'xstate.done.actor.submitTransactions'
                                    output: undefined
                                  }
                              self: ActorRef<
                                MachineSnapshot<
                                  {
                                    client: SanityClient
                                    sharedListener?: ReturnType<typeof createSharedListener>
                                    id: string
                                    cache?: Map<string, SanityDocument<DocumentType> | null>
                                    remote: SanityDocument<DocumentType> | null | undefined
                                    local: SanityDocument<DocumentType> | null | undefined
                                    mutationEvents: MutationEvent_2[]
                                    stagedChanges: MutationGroup[]
                                    stashedChanges: MutationGroup[]
                                    error: unknown
                                    fetchRemoteSnapshotAttempts: number
                                    submitTransactionsAttempts: number
                                  },
                                  | ReconnectEvent
                                  | WelcomeEvent
                                  | MutationEvent_2
                                  | {
                                      type: 'error'
                                    }
                                  | {
                                      type: 'retry'
                                    }
                                  | {
                                      type: 'connect'
                                    }
                                  | {
                                      type: 'reconnect'
                                    }
                                  | {
                                      type: 'welcome'
                                    }
                                  | {
                                      type: 'mutate'
                                      mutations: Mutation_2[]
                                    }
                                  | {
                                      type: 'submit'
                                    }
                                  | {
                                      type: 'xstate.done.actor.getDocument'
                                      output: SanityDocument<DocumentType>
                                    }
                                  | {
                                      type: 'xstate.done.actor.submitTransactions'
                                      output: undefined
                                    },
                                  Record<string, AnyActorRef>,
                                  StateValue,
                                  string,
                                  unknown,
                                  any,
                                  any
                                >,
                                | ReconnectEvent
                                | WelcomeEvent
                                | MutationEvent_2
                                | {
                                    type: 'error'
                                  }
                                | {
                                    type: 'retry'
                                  }
                                | {
                                    type: 'connect'
                                  }
                                | {
                                    type: 'reconnect'
                                  }
                                | {
                                    type: 'welcome'
                                  }
                                | {
                                    type: 'mutate'
                                    mutations: Mutation_2[]
                                  }
                                | {
                                    type: 'submit'
                                  }
                                | {
                                    type: 'xstate.done.actor.getDocument'
                                    output: SanityDocument<DocumentType>
                                  }
                                | {
                                    type: 'xstate.done.actor.submitTransactions'
                                    output: undefined
                                  },
                                AnyEventObject
                              >
                            }) => {
                              client: SanityClient
                              transactions: Transaction[]
                            }
                            readonly onError: {
                              readonly target: 'submitFailure'
                            }
                            readonly onDone: {
                              readonly target: 'pristine'
                              readonly actions: readonly [
                                'restore stashed changes',
                                'reset submit attempts',
                                'send pristine event to parent',
                              ]
                            }
                          }
                          readonly tags: readonly ['busy', 'ready']
                        }
                        readonly submitFailure: {
                          readonly exit: readonly ['clear error from context']
                          readonly after: {
                            readonly submitTransactionsTimeout: {
                              readonly actions: readonly ['increment submit attempts']
                              readonly target: 'submitting'
                            }
                          }
                          readonly on: {
                            readonly retry: 'submitting'
                          }
                          readonly tags: readonly ['error', 'ready']
                        }
                      }
                    }
                    readonly loadFailure: {
                      readonly exit: readonly ['clear error from context']
                      readonly after: {
                        readonly fetchRemoteSnapshotTimeout: {
                          readonly actions: readonly ['increment fetch attempts']
                          readonly target: 'loading'
                        }
                      }
                      readonly on: {
                        readonly retry: 'loading'
                      }
                      readonly tags: readonly ['error']
                    }
                  }
                }
              }
            }
          >
        >
      | undefined
  },
  {
    src: 'documentMutatorMachine'
    logic: StateMachine<
      {
        client: SanityClient
        sharedListener?: ReturnType<typeof createSharedListener>
        id: string
        cache?: Map<string, SanityDocument<DocumentType> | null>
        remote: SanityDocument<DocumentType> | null | undefined
        local: SanityDocument<DocumentType> | null | undefined
        mutationEvents: MutationEvent_2[]
        stagedChanges: MutationGroup[]
        stashedChanges: MutationGroup[]
        error: unknown
        fetchRemoteSnapshotAttempts: number
        submitTransactionsAttempts: number
      },
      | WelcomeEvent
      | ReconnectEvent
      | MutationEvent_2
      | {
          type: 'error'
        }
      | {
          type: 'retry'
        }
      | {
          type: 'connect'
        }
      | {
          type: 'reconnect'
        }
      | {
          type: 'welcome'
        }
      | {
          type: 'mutate'
          mutations: Mutation_2[]
        }
      | {
          type: 'submit'
        }
      | {
          type: 'xstate.done.actor.getDocument'
          output: SanityDocument<DocumentType>
        }
      | {
          type: 'xstate.done.actor.submitTransactions'
          output: undefined
        },
      {
        [x: string]:
          | ActorRefFromLogic<
              PromiseActorLogic<
                void | SanityDocument<Record<string, any>> | undefined,
                {
                  client: SanityClient
                  id: string
                },
                EventObject
              >
            >
          | ActorRefFromLogic<
              PromiseActorLogic<
                void,
                {
                  client: SanityClient
                  transactions: Transaction[]
                },
                EventObject
              >
            >
          | ActorRefFromLogic<
              ObservableActorLogic<
                WelcomeEvent | ReconnectEvent | MutationEvent_2,
                {
                  listener: ReturnType<typeof createSharedListener>
                  id: string
                },
                EventObject
              >
            >
          | undefined
        getDocument?:
          | ActorRefFromLogic<
              PromiseActorLogic<
                void | SanityDocument<Record<string, any>> | undefined,
                {
                  client: SanityClient
                  id: string
                },
                EventObject
              >
            >
          | undefined
        submitTransactions?:
          | ActorRefFromLogic<
              PromiseActorLogic<
                void,
                {
                  client: SanityClient
                  transactions: Transaction[]
                },
                EventObject
              >
            >
          | undefined
      },
      Values<{
        'fetch remote snapshot': {
          src: 'fetch remote snapshot'
          logic: PromiseActorLogic<
            void | SanityDocument<Record<string, any>> | undefined,
            {
              client: SanityClient
              id: string
            },
            EventObject
          >
          id: 'getDocument'
        }
        'submit mutations as transactions': {
          src: 'submit mutations as transactions'
          logic: PromiseActorLogic<
            void,
            {
              client: SanityClient
              transactions: Transaction[]
            },
            EventObject
          >
          id: 'submitTransactions'
        }
        'server-sent events': {
          src: 'server-sent events'
          logic: ObservableActorLogic<
            ReconnectEvent | WelcomeEvent | MutationEvent_2,
            {
              listener: ReturnType<typeof createSharedListener>
              id: string
            },
            EventObject
          >
          id: string | undefined
        }
      }>,
      Values<{
        'assign error to context': {
          type: 'assign error to context'
          params: NonReducibleUnknown
        }
        'clear error from context': {
          type: 'clear error from context'
          params: NonReducibleUnknown
        }
        'connect to server-sent events': {
          type: 'connect to server-sent events'
          params: NonReducibleUnknown
        }
        'listen to server-sent events': {
          type: 'listen to server-sent events'
          params: NonReducibleUnknown
        }
        'stop listening to server-sent events': {
          type: 'stop listening to server-sent events'
          params: NonReducibleUnknown
        }
        'buffer remote mutation events': {
          type: 'buffer remote mutation events'
          params: NonReducibleUnknown
        }
        'restore stashed changes': {
          type: 'restore stashed changes'
          params: NonReducibleUnknown
        }
        'rebase fetched remote snapshot': {
          type: 'rebase fetched remote snapshot'
          params: NonReducibleUnknown
        }
        'apply mendoza patch': {
          type: 'apply mendoza patch'
          params: NonReducibleUnknown
        }
        'increment fetch attempts': {
          type: 'increment fetch attempts'
          params: NonReducibleUnknown
        }
        'reset fetch attempts': {
          type: 'reset fetch attempts'
          params: NonReducibleUnknown
        }
        'increment submit attempts': {
          type: 'increment submit attempts'
          params: NonReducibleUnknown
        }
        'reset submit attempts': {
          type: 'reset submit attempts'
          params: NonReducibleUnknown
        }
        'stage mutation': {
          type: 'stage mutation'
          params: NonReducibleUnknown
        }
        'stash mutation': {
          type: 'stash mutation'
          params: NonReducibleUnknown
        }
        'rebase local snapshot': {
          type: 'rebase local snapshot'
          params: NonReducibleUnknown
        }
        'send pristine event to parent': {
          type: 'send pristine event to parent'
          params: NonReducibleUnknown
        }
        'send sync event to parent': {
          type: 'send sync event to parent'
          params: NonReducibleUnknown
        }
        'send mutation event to parent': {
          type: 'send mutation event to parent'
          params: NonReducibleUnknown
        }
      }>,
      never,
      'fetchRemoteSnapshotTimeout' | 'submitTransactionsTimeout',
      | 'disconnected'
      | 'connecting'
      | 'reconnecting'
      | 'connectFailure'
      | {
          connected:
            | 'loading'
            | 'loadFailure'
            | {
                loaded: 'pristine' | 'dirty' | 'submitting' | 'submitFailure'
              }
        },
      'error' | 'busy' | 'ready',
      DocumentMutatorMachineInput,
      NonReducibleUnknown,
      EventObject,
      MetaObject,
      {
        readonly id: 'document-mutator'
        readonly context: ({
          input,
        }: {
          spawn: {
            <
              TSrc extends
                | 'fetch remote snapshot'
                | 'submit mutations as transactions'
                | 'server-sent events',
            >(
              logic: TSrc,
              ...[options]:
                | ({
                    src: 'fetch remote snapshot'
                    logic: PromiseActorLogic<
                      void | SanityDocument<Record<string, any>> | undefined,
                      {
                        client: SanityClient
                        id: string
                      },
                      EventObject
                    >
                    id: 'getDocument'
                  } extends infer T
                    ? T extends {
                        src: 'fetch remote snapshot'
                        logic: PromiseActorLogic<
                          void | SanityDocument<Record<string, any>> | undefined,
                          {
                            client: SanityClient
                            id: string
                          },
                          EventObject
                        >
                        id: 'getDocument'
                      }
                      ? T extends {
                          src: TSrc
                        }
                        ? ConditionalRequired<
                            [
                              options?:
                                | ({
                                    id?: T['id'] | undefined
                                    systemId?: string
                                    input?: InputFrom<T['logic']> | undefined
                                    syncSnapshot?: boolean
                                  } & {[K in RequiredActorOptions<T>]: unknown})
                                | undefined,
                            ],
                            IsNotNever<RequiredActorOptions<T>>
                          >
                        : never
                      : never
                    : never)
                | ({
                    src: 'submit mutations as transactions'
                    logic: PromiseActorLogic<
                      void,
                      {
                        client: SanityClient
                        transactions: Transaction[]
                      },
                      EventObject
                    >
                    id: 'submitTransactions'
                  } extends infer T_1
                    ? T_1 extends {
                        src: 'submit mutations as transactions'
                        logic: PromiseActorLogic<
                          void,
                          {
                            client: SanityClient
                            transactions: Transaction[]
                          },
                          EventObject
                        >
                        id: 'submitTransactions'
                      }
                      ? T_1 extends {
                          src: TSrc
                        }
                        ? ConditionalRequired<
                            [
                              options?:
                                | ({
                                    id?: T_1['id'] | undefined
                                    systemId?: string
                                    input?: InputFrom<T_1['logic']> | undefined
                                    syncSnapshot?: boolean
                                  } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                                | undefined,
                            ],
                            IsNotNever<RequiredActorOptions<T_1>>
                          >
                        : never
                      : never
                    : never)
                | ({
                    src: 'server-sent events'
                    logic: ObservableActorLogic<
                      ReconnectEvent | WelcomeEvent | MutationEvent_2,
                      {
                        listener: ReturnType<typeof createSharedListener>
                        id: string
                      },
                      EventObject
                    >
                    id: string | undefined
                  } extends infer T_2
                    ? T_2 extends {
                        src: 'server-sent events'
                        logic: ObservableActorLogic<
                          ReconnectEvent | WelcomeEvent | MutationEvent_2,
                          {
                            listener: ReturnType<typeof createSharedListener>
                            id: string
                          },
                          EventObject
                        >
                        id: string | undefined
                      }
                      ? T_2 extends {
                          src: TSrc
                        }
                        ? ConditionalRequired<
                            [
                              options?:
                                | ({
                                    id?: T_2['id'] | undefined
                                    systemId?: string
                                    input?: InputFrom<T_2['logic']> | undefined
                                    syncSnapshot?: boolean
                                  } & {[K_2 in RequiredActorOptions<T_2>]: unknown})
                                | undefined,
                            ],
                            IsNotNever<RequiredActorOptions<T_2>>
                          >
                        : never
                      : never
                    : never)
            ): ActorRefFromLogic<
              GetConcreteByKey<
                Values<{
                  'fetch remote snapshot': {
                    src: 'fetch remote snapshot'
                    logic: PromiseActorLogic<
                      void | SanityDocument<Record<string, any>> | undefined,
                      {
                        client: SanityClient
                        id: string
                      },
                      EventObject
                    >
                    id: 'getDocument'
                  }
                  'submit mutations as transactions': {
                    src: 'submit mutations as transactions'
                    logic: PromiseActorLogic<
                      void,
                      {
                        client: SanityClient
                        transactions: Transaction[]
                      },
                      EventObject
                    >
                    id: 'submitTransactions'
                  }
                  'server-sent events': {
                    src: 'server-sent events'
                    logic: ObservableActorLogic<
                      ReconnectEvent | WelcomeEvent | MutationEvent_2,
                      {
                        listener: ReturnType<typeof createSharedListener>
                        id: string
                      },
                      EventObject
                    >
                    id: string | undefined
                  }
                }>,
                'src',
                TSrc
              >['logic']
            >
            <TLogic extends AnyActorLogic>(
              src: TLogic,
              options?:
                | {
                    id?: never
                    systemId?: string
                    input?: InputFrom<TLogic> | undefined
                    syncSnapshot?: boolean
                  }
                | undefined,
            ): ActorRefFromLogic<TLogic>
          }
          input: DocumentMutatorMachineInput
          self: ActorRef<
            MachineSnapshot<
              {
                client: SanityClient
                sharedListener?: ReturnType<typeof createSharedListener>
                id: string
                cache?: Map<string, SanityDocument<DocumentType> | null>
                remote: SanityDocument<DocumentType> | null | undefined
                local: SanityDocument<DocumentType> | null | undefined
                mutationEvents: MutationEvent_2[]
                stagedChanges: MutationGroup[]
                stashedChanges: MutationGroup[]
                error: unknown
                fetchRemoteSnapshotAttempts: number
                submitTransactionsAttempts: number
              },
              | ReconnectEvent
              | WelcomeEvent
              | MutationEvent_2
              | {
                  type: 'error'
                }
              | {
                  type: 'retry'
                }
              | {
                  type: 'connect'
                }
              | {
                  type: 'reconnect'
                }
              | {
                  type: 'welcome'
                }
              | {
                  type: 'mutate'
                  mutations: Mutation_2[]
                }
              | {
                  type: 'submit'
                }
              | {
                  type: 'xstate.done.actor.getDocument'
                  output: SanityDocument<DocumentType>
                }
              | {
                  type: 'xstate.done.actor.submitTransactions'
                  output: undefined
                },
              Record<string, AnyActorRef | undefined>,
              StateValue,
              string,
              unknown,
              any,
              any
            >,
            | ReconnectEvent
            | WelcomeEvent
            | MutationEvent_2
            | {
                type: 'error'
              }
            | {
                type: 'retry'
              }
            | {
                type: 'connect'
              }
            | {
                type: 'reconnect'
              }
            | {
                type: 'welcome'
              }
            | {
                type: 'mutate'
                mutations: Mutation_2[]
              }
            | {
                type: 'submit'
              }
            | {
                type: 'xstate.done.actor.getDocument'
                output: SanityDocument<DocumentType>
              }
            | {
                type: 'xstate.done.actor.submitTransactions'
                output: undefined
              },
            AnyEventObject
          >
        }) => {
          client: SanityClient
          sharedListener: Observable<ReconnectEvent | WelcomeEvent | MutationEvent_2> | undefined
          id: string
          remote: undefined
          local: undefined
          mutationEvents: never[]
          stagedChanges: never[]
          stashedChanges: never[]
          error: undefined
          fetchRemoteSnapshotAttempts: number
          submitTransactionsAttempts: number
          cache: Map<string, SanityDocument<DocumentType> | null> | undefined
        }
        readonly entry: readonly ['connect to server-sent events']
        readonly on: {
          readonly mutate: {
            readonly actions: readonly ['rebase local snapshot', 'stage mutation']
          }
        }
        readonly initial: 'disconnected'
        readonly states: {
          readonly disconnected: {
            readonly on: {
              readonly connect: {
                readonly target: 'connecting'
                readonly actions: readonly ['listen to server-sent events']
              }
            }
          }
          readonly connecting: {
            readonly on: {
              readonly welcome: 'connected'
              readonly reconnect: 'reconnecting'
              readonly error: 'connectFailure'
            }
            readonly tags: readonly ['busy']
          }
          readonly connectFailure: {
            readonly on: {
              readonly connect: {
                readonly target: 'connecting'
                readonly actions: readonly ['listen to server-sent events']
              }
            }
            readonly entry: readonly [
              'stop listening to server-sent events',
              'assign error to context',
            ]
            readonly exit: readonly ['clear error from context']
            readonly tags: readonly ['error']
          }
          readonly reconnecting: {
            readonly on: {
              readonly welcome: {
                readonly target: 'connected'
              }
              readonly error: {
                readonly target: 'connectFailure'
              }
            }
            readonly tags: readonly ['busy', 'error']
          }
          readonly connected: {
            readonly on: {
              readonly mutation: {
                readonly actions: readonly ['buffer remote mutation events']
              }
              readonly reconnect: 'reconnecting'
            }
            readonly entry: readonly ['clear error from context']
            readonly initial: 'loading'
            readonly states: {
              readonly loading: {
                readonly invoke: {
                  readonly src: 'fetch remote snapshot'
                  readonly id: 'getDocument'
                  readonly input: ({
                    context,
                  }: {
                    context: {
                      client: SanityClient
                      sharedListener?: ReturnType<typeof createSharedListener>
                      id: string
                      cache?: Map<string, SanityDocument<DocumentType> | null>
                      remote: SanityDocument<DocumentType> | null | undefined
                      local: SanityDocument<DocumentType> | null | undefined
                      mutationEvents: MutationEvent_2[]
                      stagedChanges: MutationGroup[]
                      stashedChanges: MutationGroup[]
                      error: unknown
                      fetchRemoteSnapshotAttempts: number
                      submitTransactionsAttempts: number
                    }
                    event:
                      | ReconnectEvent
                      | WelcomeEvent
                      | MutationEvent_2
                      | {
                          type: 'error'
                        }
                      | {
                          type: 'retry'
                        }
                      | {
                          type: 'connect'
                        }
                      | {
                          type: 'reconnect'
                        }
                      | {
                          type: 'welcome'
                        }
                      | {
                          type: 'mutate'
                          mutations: Mutation_2[]
                        }
                      | {
                          type: 'submit'
                        }
                      | {
                          type: 'xstate.done.actor.getDocument'
                          output: SanityDocument<DocumentType>
                        }
                      | {
                          type: 'xstate.done.actor.submitTransactions'
                          output: undefined
                        }
                    self: ActorRef<
                      MachineSnapshot<
                        {
                          client: SanityClient
                          sharedListener?: ReturnType<typeof createSharedListener>
                          id: string
                          cache?: Map<string, SanityDocument<DocumentType> | null>
                          remote: SanityDocument<DocumentType> | null | undefined
                          local: SanityDocument<DocumentType> | null | undefined
                          mutationEvents: MutationEvent_2[]
                          stagedChanges: MutationGroup[]
                          stashedChanges: MutationGroup[]
                          error: unknown
                          fetchRemoteSnapshotAttempts: number
                          submitTransactionsAttempts: number
                        },
                        | ReconnectEvent
                        | WelcomeEvent
                        | MutationEvent_2
                        | {
                            type: 'error'
                          }
                        | {
                            type: 'retry'
                          }
                        | {
                            type: 'connect'
                          }
                        | {
                            type: 'reconnect'
                          }
                        | {
                            type: 'welcome'
                          }
                        | {
                            type: 'mutate'
                            mutations: Mutation_2[]
                          }
                        | {
                            type: 'submit'
                          }
                        | {
                            type: 'xstate.done.actor.getDocument'
                            output: SanityDocument<DocumentType>
                          }
                        | {
                            type: 'xstate.done.actor.submitTransactions'
                            output: undefined
                          },
                        Record<string, AnyActorRef>,
                        StateValue,
                        string,
                        unknown,
                        any,
                        any
                      >,
                      | ReconnectEvent
                      | WelcomeEvent
                      | MutationEvent_2
                      | {
                          type: 'error'
                        }
                      | {
                          type: 'retry'
                        }
                      | {
                          type: 'connect'
                        }
                      | {
                          type: 'reconnect'
                        }
                      | {
                          type: 'welcome'
                        }
                      | {
                          type: 'mutate'
                          mutations: Mutation_2[]
                        }
                      | {
                          type: 'submit'
                        }
                      | {
                          type: 'xstate.done.actor.getDocument'
                          output: SanityDocument<DocumentType>
                        }
                      | {
                          type: 'xstate.done.actor.submitTransactions'
                          output: undefined
                        },
                      AnyEventObject
                    >
                  }) => {
                    client: SanityClient
                    id: string
                  }
                  readonly onError: {
                    readonly target: 'loadFailure'
                  }
                  readonly onDone: {
                    readonly target: 'loaded'
                    readonly actions: readonly [
                      'rebase fetched remote snapshot',
                      'reset fetch attempts',
                    ]
                  }
                }
                readonly tags: readonly ['busy']
              }
              readonly loaded: {
                readonly entry: readonly ['send sync event to parent']
                readonly on: {
                  readonly mutation: {
                    readonly actions: readonly [
                      'apply mendoza patch',
                      'send mutation event to parent',
                    ]
                  }
                }
                readonly initial: 'pristine'
                readonly states: {
                  readonly pristine: {
                    readonly on: {
                      readonly mutate: {
                        readonly actions: readonly ['rebase local snapshot', 'stage mutation']
                        readonly target: 'dirty'
                      }
                    }
                    readonly tags: readonly ['ready']
                  }
                  readonly dirty: {
                    readonly on: {
                      readonly submit: 'submitting'
                    }
                    readonly tags: readonly ['ready']
                  }
                  readonly submitting: {
                    readonly on: {
                      readonly mutate: {
                        readonly actions: readonly ['rebase local snapshot', 'stash mutation']
                      }
                    }
                    readonly invoke: {
                      readonly src: 'submit mutations as transactions'
                      readonly id: 'submitTransactions'
                      readonly input: ({
                        context,
                      }: {
                        context: {
                          client: SanityClient
                          sharedListener?: ReturnType<typeof createSharedListener>
                          id: string
                          cache?: Map<string, SanityDocument<DocumentType> | null>
                          remote: SanityDocument<DocumentType> | null | undefined
                          local: SanityDocument<DocumentType> | null | undefined
                          mutationEvents: MutationEvent_2[]
                          stagedChanges: MutationGroup[]
                          stashedChanges: MutationGroup[]
                          error: unknown
                          fetchRemoteSnapshotAttempts: number
                          submitTransactionsAttempts: number
                        }
                        event:
                          | ReconnectEvent
                          | WelcomeEvent
                          | MutationEvent_2
                          | {
                              type: 'error'
                            }
                          | {
                              type: 'retry'
                            }
                          | {
                              type: 'connect'
                            }
                          | {
                              type: 'reconnect'
                            }
                          | {
                              type: 'welcome'
                            }
                          | {
                              type: 'mutate'
                              mutations: Mutation_2[]
                            }
                          | {
                              type: 'submit'
                            }
                          | {
                              type: 'xstate.done.actor.getDocument'
                              output: SanityDocument<DocumentType>
                            }
                          | {
                              type: 'xstate.done.actor.submitTransactions'
                              output: undefined
                            }
                        self: ActorRef<
                          MachineSnapshot<
                            {
                              client: SanityClient
                              sharedListener?: ReturnType<typeof createSharedListener>
                              id: string
                              cache?: Map<string, SanityDocument<DocumentType> | null>
                              remote: SanityDocument<DocumentType> | null | undefined
                              local: SanityDocument<DocumentType> | null | undefined
                              mutationEvents: MutationEvent_2[]
                              stagedChanges: MutationGroup[]
                              stashedChanges: MutationGroup[]
                              error: unknown
                              fetchRemoteSnapshotAttempts: number
                              submitTransactionsAttempts: number
                            },
                            | ReconnectEvent
                            | WelcomeEvent
                            | MutationEvent_2
                            | {
                                type: 'error'
                              }
                            | {
                                type: 'retry'
                              }
                            | {
                                type: 'connect'
                              }
                            | {
                                type: 'reconnect'
                              }
                            | {
                                type: 'welcome'
                              }
                            | {
                                type: 'mutate'
                                mutations: Mutation_2[]
                              }
                            | {
                                type: 'submit'
                              }
                            | {
                                type: 'xstate.done.actor.getDocument'
                                output: SanityDocument<DocumentType>
                              }
                            | {
                                type: 'xstate.done.actor.submitTransactions'
                                output: undefined
                              },
                            Record<string, AnyActorRef>,
                            StateValue,
                            string,
                            unknown,
                            any,
                            any
                          >,
                          | ReconnectEvent
                          | WelcomeEvent
                          | MutationEvent_2
                          | {
                              type: 'error'
                            }
                          | {
                              type: 'retry'
                            }
                          | {
                              type: 'connect'
                            }
                          | {
                              type: 'reconnect'
                            }
                          | {
                              type: 'welcome'
                            }
                          | {
                              type: 'mutate'
                              mutations: Mutation_2[]
                            }
                          | {
                              type: 'submit'
                            }
                          | {
                              type: 'xstate.done.actor.getDocument'
                              output: SanityDocument<DocumentType>
                            }
                          | {
                              type: 'xstate.done.actor.submitTransactions'
                              output: undefined
                            },
                          AnyEventObject
                        >
                      }) => {
                        client: SanityClient
                        transactions: Transaction[]
                      }
                      readonly onError: {
                        readonly target: 'submitFailure'
                      }
                      readonly onDone: {
                        readonly target: 'pristine'
                        readonly actions: readonly [
                          'restore stashed changes',
                          'reset submit attempts',
                          'send pristine event to parent',
                        ]
                      }
                    }
                    readonly tags: readonly ['busy', 'ready']
                  }
                  readonly submitFailure: {
                    readonly exit: readonly ['clear error from context']
                    readonly after: {
                      readonly submitTransactionsTimeout: {
                        readonly actions: readonly ['increment submit attempts']
                        readonly target: 'submitting'
                      }
                    }
                    readonly on: {
                      readonly retry: 'submitting'
                    }
                    readonly tags: readonly ['error', 'ready']
                  }
                }
              }
              readonly loadFailure: {
                readonly exit: readonly ['clear error from context']
                readonly after: {
                  readonly fetchRemoteSnapshotTimeout: {
                    readonly actions: readonly ['increment fetch attempts']
                    readonly target: 'loading'
                  }
                }
                readonly on: {
                  readonly retry: 'loading'
                }
                readonly tags: readonly ['error']
              }
            }
          }
        }
      }
    >
    id: string | undefined
  },
  Values<{
    'add document actor': {
      type: 'add document actor'
      params: NonReducibleUnknown
    }
    'emit sync event': {
      type: 'emit sync event'
      params: NonReducibleUnknown
    }
    'emit mutation event': {
      type: 'emit mutation event'
      params: NonReducibleUnknown
    }
    'emit rebased event': {
      type: 'emit rebased event'
      params: NonReducibleUnknown
    }
    'emit pristine event': {
      type: 'emit pristine event'
      params: NonReducibleUnknown
    }
    'stop remote snapshot': {
      type: 'stop remote snapshot'
      params: NonReducibleUnknown
    }
    'remove remote snapshot from context': {
      type: 'remove remote snapshot from context'
      params: NonReducibleUnknown
    }
  }>,
  never,
  never,
  'pristine',
  string,
  DatasetMutatorMachineInput,
  NonReducibleUnknown,
  | {
      type: 'sync'
      id: string
      document: SanityDocumentBase
    }
  | {
      type: 'mutation'
      id: string
      effects: {
        apply: RawPatch
      }
      previousRev: string
      resultRev: string
    }
  | {
      type: 'rebased.local'
      id: string
      document: SanityDocumentBase
    }
  | {
      type: 'rebased.remote'
      id: string
      document: SanityDocumentBase
    }
  | {
      type: 'pristine'
      id: string
    },
  MetaObject,
  {
    /** @xstate-layout N4IgpgJg5mDOIC5QBsD2BjAhsgIhgrgLZgB2ALgMTICWsZpA2gAwC6ioADqrNWdaiXYgAHogC0ADgBMAOgkA2ACyKArBICcTdfKXSANCACeiFQHYZpgMxWV800yYqp6gIyn5AXw8G0WXAWJyCnwSGjpGViEuHj4BIVEEKUt5OSlFU1smFwkFW0sDY0T1GRUXRXkXKVN7HSYJJkUvHwxsPHQiUjIZDgAnWj4SMApCfDJMemY2JBBo3n5BaYSXeVlbKRUsiXdFSxcXfKNERSqZTbr1zVMJNyaQX1aAzpkIah6yQwp0VEJCXkmo7hzOKLRDLFwyFxaSzXKTOKQudSKCQFRDOczqCSWJjwjbHCQqRFebwgEioCBwIT3fztQJkAExebxcQuFEIZaWGRIrF7dSXKTyFSNYlUtodcjdPp0aiDelAhagBLpVmWAmc46mNwSZTyLQuFS3EWPcUvN6FTiA2LykSoq6c6wbCqVUzparKioyRFVfkq1zqNSWIkeIA */
    readonly id: 'dataset-mutator'
    readonly context: ({
      input,
    }: {
      spawn: {
        <TSrc extends 'documentMutatorMachine'>(
          logic: TSrc,
          ...[options]: {
            src: 'documentMutatorMachine'
            logic: StateMachine<
              {
                client: SanityClient
                sharedListener?: ReturnType<typeof createSharedListener>
                id: string
                cache?: Map<string, SanityDocument<DocumentType> | null>
                remote: SanityDocument<DocumentType> | null | undefined
                local: SanityDocument<DocumentType> | null | undefined
                mutationEvents: MutationEvent_2[]
                stagedChanges: MutationGroup[]
                stashedChanges: MutationGroup[]
                error: unknown
                fetchRemoteSnapshotAttempts: number
                submitTransactionsAttempts: number
              },
              | WelcomeEvent
              | ReconnectEvent
              | MutationEvent_2
              | {
                  type: 'error'
                }
              | {
                  type: 'retry'
                }
              | {
                  type: 'connect'
                }
              | {
                  type: 'reconnect'
                }
              | {
                  type: 'welcome'
                }
              | {
                  type: 'mutate'
                  mutations: Mutation_2[]
                }
              | {
                  type: 'submit'
                }
              | {
                  type: 'xstate.done.actor.getDocument'
                  output: SanityDocument<DocumentType>
                }
              | {
                  type: 'xstate.done.actor.submitTransactions'
                  output: undefined
                },
              {
                [x: string]:
                  | ActorRefFromLogic<
                      PromiseActorLogic<
                        void | SanityDocument<Record<string, any>> | undefined,
                        {
                          client: SanityClient
                          id: string
                        },
                        EventObject
                      >
                    >
                  | ActorRefFromLogic<
                      PromiseActorLogic<
                        void,
                        {
                          client: SanityClient
                          transactions: Transaction[]
                        },
                        EventObject
                      >
                    >
                  | ActorRefFromLogic<
                      ObservableActorLogic<
                        WelcomeEvent | ReconnectEvent | MutationEvent_2,
                        {
                          listener: ReturnType<typeof createSharedListener>
                          id: string
                        },
                        EventObject
                      >
                    >
                  | undefined
                getDocument?:
                  | ActorRefFromLogic<
                      PromiseActorLogic<
                        void | SanityDocument<Record<string, any>> | undefined,
                        {
                          client: SanityClient
                          id: string
                        },
                        EventObject
                      >
                    >
                  | undefined
                submitTransactions?:
                  | ActorRefFromLogic<
                      PromiseActorLogic<
                        void,
                        {
                          client: SanityClient
                          transactions: Transaction[]
                        },
                        EventObject
                      >
                    >
                  | undefined
              },
              Values<{
                'fetch remote snapshot': {
                  src: 'fetch remote snapshot'
                  logic: PromiseActorLogic<
                    void | SanityDocument<Record<string, any>> | undefined,
                    {
                      client: SanityClient
                      id: string
                    },
                    EventObject
                  >
                  id: 'getDocument'
                }
                'submit mutations as transactions': {
                  src: 'submit mutations as transactions'
                  logic: PromiseActorLogic<
                    void,
                    {
                      client: SanityClient
                      transactions: Transaction[]
                    },
                    EventObject
                  >
                  id: 'submitTransactions'
                }
                'server-sent events': {
                  src: 'server-sent events'
                  logic: ObservableActorLogic<
                    ReconnectEvent | WelcomeEvent | MutationEvent_2,
                    {
                      listener: ReturnType<typeof createSharedListener>
                      id: string
                    },
                    EventObject
                  >
                  id: string | undefined
                }
              }>,
              Values<{
                'assign error to context': {
                  type: 'assign error to context'
                  params: NonReducibleUnknown
                }
                'clear error from context': {
                  type: 'clear error from context'
                  params: NonReducibleUnknown
                }
                'connect to server-sent events': {
                  type: 'connect to server-sent events'
                  params: NonReducibleUnknown
                }
                'listen to server-sent events': {
                  type: 'listen to server-sent events'
                  params: NonReducibleUnknown
                }
                'stop listening to server-sent events': {
                  type: 'stop listening to server-sent events'
                  params: NonReducibleUnknown
                }
                'buffer remote mutation events': {
                  type: 'buffer remote mutation events'
                  params: NonReducibleUnknown
                }
                'restore stashed changes': {
                  type: 'restore stashed changes'
                  params: NonReducibleUnknown
                }
                'rebase fetched remote snapshot': {
                  type: 'rebase fetched remote snapshot'
                  params: NonReducibleUnknown
                }
                'apply mendoza patch': {
                  type: 'apply mendoza patch'
                  params: NonReducibleUnknown
                }
                'increment fetch attempts': {
                  type: 'increment fetch attempts'
                  params: NonReducibleUnknown
                }
                'reset fetch attempts': {
                  type: 'reset fetch attempts'
                  params: NonReducibleUnknown
                }
                'increment submit attempts': {
                  type: 'increment submit attempts'
                  params: NonReducibleUnknown
                }
                'reset submit attempts': {
                  type: 'reset submit attempts'
                  params: NonReducibleUnknown
                }
                'stage mutation': {
                  type: 'stage mutation'
                  params: NonReducibleUnknown
                }
                'stash mutation': {
                  type: 'stash mutation'
                  params: NonReducibleUnknown
                }
                'rebase local snapshot': {
                  type: 'rebase local snapshot'
                  params: NonReducibleUnknown
                }
                'send pristine event to parent': {
                  type: 'send pristine event to parent'
                  params: NonReducibleUnknown
                }
                'send sync event to parent': {
                  type: 'send sync event to parent'
                  params: NonReducibleUnknown
                }
                'send mutation event to parent': {
                  type: 'send mutation event to parent'
                  params: NonReducibleUnknown
                }
              }>,
              never,
              'fetchRemoteSnapshotTimeout' | 'submitTransactionsTimeout',
              | 'disconnected'
              | 'connecting'
              | 'reconnecting'
              | 'connectFailure'
              | {
                  connected:
                    | 'loading'
                    | 'loadFailure'
                    | {
                        loaded: 'pristine' | 'dirty' | 'submitting' | 'submitFailure'
                      }
                },
              'error' | 'busy' | 'ready',
              DocumentMutatorMachineInput,
              NonReducibleUnknown,
              EventObject,
              MetaObject,
              {
                readonly id: 'document-mutator'
                readonly context: ({
                  input,
                }: {
                  spawn: {
                    <
                      TSrc_1 extends
                        | 'fetch remote snapshot'
                        | 'submit mutations as transactions'
                        | 'server-sent events',
                    >(
                      logic: TSrc_1,
                      ...[options]:
                        | ({
                            src: 'fetch remote snapshot'
                            logic: PromiseActorLogic<
                              void | SanityDocument<Record<string, any>> | undefined,
                              {
                                client: SanityClient
                                id: string
                              },
                              EventObject
                            >
                            id: 'getDocument'
                          } extends infer T
                            ? T extends {
                                src: 'fetch remote snapshot'
                                logic: PromiseActorLogic<
                                  void | SanityDocument<Record<string, any>> | undefined,
                                  {
                                    client: SanityClient
                                    id: string
                                  },
                                  EventObject
                                >
                                id: 'getDocument'
                              }
                              ? T extends {
                                  src: TSrc_1
                                }
                                ? ConditionalRequired<
                                    [
                                      options?:
                                        | ({
                                            id?: T['id'] | undefined
                                            systemId?: string
                                            input?: InputFrom<T['logic']> | undefined
                                            syncSnapshot?: boolean
                                          } & {[K in RequiredActorOptions<T>]: unknown})
                                        | undefined,
                                    ],
                                    IsNotNever<RequiredActorOptions<T>>
                                  >
                                : never
                              : never
                            : never)
                        | ({
                            src: 'submit mutations as transactions'
                            logic: PromiseActorLogic<
                              void,
                              {
                                client: SanityClient
                                transactions: Transaction[]
                              },
                              EventObject
                            >
                            id: 'submitTransactions'
                          } extends infer T_1
                            ? T_1 extends {
                                src: 'submit mutations as transactions'
                                logic: PromiseActorLogic<
                                  void,
                                  {
                                    client: SanityClient
                                    transactions: Transaction[]
                                  },
                                  EventObject
                                >
                                id: 'submitTransactions'
                              }
                              ? T_1 extends {
                                  src: TSrc_1
                                }
                                ? ConditionalRequired<
                                    [
                                      options?:
                                        | ({
                                            id?: T_1['id'] | undefined
                                            systemId?: string
                                            input?: InputFrom<T_1['logic']> | undefined
                                            syncSnapshot?: boolean
                                          } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                                        | undefined,
                                    ],
                                    IsNotNever<RequiredActorOptions<T_1>>
                                  >
                                : never
                              : never
                            : never)
                        | ({
                            src: 'server-sent events'
                            logic: ObservableActorLogic<
                              ReconnectEvent | WelcomeEvent | MutationEvent_2,
                              {
                                listener: ReturnType<typeof createSharedListener>
                                id: string
                              },
                              EventObject
                            >
                            id: string | undefined
                          } extends infer T_2
                            ? T_2 extends {
                                src: 'server-sent events'
                                logic: ObservableActorLogic<
                                  ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                  {
                                    listener: ReturnType<typeof createSharedListener>
                                    id: string
                                  },
                                  EventObject
                                >
                                id: string | undefined
                              }
                              ? T_2 extends {
                                  src: TSrc_1
                                }
                                ? ConditionalRequired<
                                    [
                                      options?:
                                        | ({
                                            id?: T_2['id'] | undefined
                                            systemId?: string
                                            input?: InputFrom<T_2['logic']> | undefined
                                            syncSnapshot?: boolean
                                          } & {[K_2 in RequiredActorOptions<T_2>]: unknown})
                                        | undefined,
                                    ],
                                    IsNotNever<RequiredActorOptions<T_2>>
                                  >
                                : never
                              : never
                            : never)
                    ): ActorRefFromLogic<
                      GetConcreteByKey<
                        Values<{
                          'fetch remote snapshot': {
                            src: 'fetch remote snapshot'
                            logic: PromiseActorLogic<
                              void | SanityDocument<Record<string, any>> | undefined,
                              {
                                client: SanityClient
                                id: string
                              },
                              EventObject
                            >
                            id: 'getDocument'
                          }
                          'submit mutations as transactions': {
                            src: 'submit mutations as transactions'
                            logic: PromiseActorLogic<
                              void,
                              {
                                client: SanityClient
                                transactions: Transaction[]
                              },
                              EventObject
                            >
                            id: 'submitTransactions'
                          }
                          'server-sent events': {
                            src: 'server-sent events'
                            logic: ObservableActorLogic<
                              ReconnectEvent | WelcomeEvent | MutationEvent_2,
                              {
                                listener: ReturnType<typeof createSharedListener>
                                id: string
                              },
                              EventObject
                            >
                            id: string | undefined
                          }
                        }>,
                        'src',
                        TSrc_1
                      >['logic']
                    >
                    <TLogic extends AnyActorLogic>(
                      src: TLogic,
                      options?:
                        | {
                            id?: never
                            systemId?: string
                            input?: InputFrom<TLogic> | undefined
                            syncSnapshot?: boolean
                          }
                        | undefined,
                    ): ActorRefFromLogic<TLogic>
                  }
                  input: DocumentMutatorMachineInput
                  self: ActorRef<
                    MachineSnapshot<
                      {
                        client: SanityClient
                        sharedListener?: ReturnType<typeof createSharedListener>
                        id: string
                        cache?: Map<string, SanityDocument<DocumentType> | null>
                        remote: SanityDocument<DocumentType> | null | undefined
                        local: SanityDocument<DocumentType> | null | undefined
                        mutationEvents: MutationEvent_2[]
                        stagedChanges: MutationGroup[]
                        stashedChanges: MutationGroup[]
                        error: unknown
                        fetchRemoteSnapshotAttempts: number
                        submitTransactionsAttempts: number
                      },
                      | ReconnectEvent
                      | WelcomeEvent
                      | MutationEvent_2
                      | {
                          type: 'error'
                        }
                      | {
                          type: 'retry'
                        }
                      | {
                          type: 'connect'
                        }
                      | {
                          type: 'reconnect'
                        }
                      | {
                          type: 'welcome'
                        }
                      | {
                          type: 'mutate'
                          mutations: Mutation_2[]
                        }
                      | {
                          type: 'submit'
                        }
                      | {
                          type: 'xstate.done.actor.getDocument'
                          output: SanityDocument<DocumentType>
                        }
                      | {
                          type: 'xstate.done.actor.submitTransactions'
                          output: undefined
                        },
                      Record<string, AnyActorRef | undefined>,
                      StateValue,
                      string,
                      unknown,
                      any,
                      any
                    >,
                    | ReconnectEvent
                    | WelcomeEvent
                    | MutationEvent_2
                    | {
                        type: 'error'
                      }
                    | {
                        type: 'retry'
                      }
                    | {
                        type: 'connect'
                      }
                    | {
                        type: 'reconnect'
                      }
                    | {
                        type: 'welcome'
                      }
                    | {
                        type: 'mutate'
                        mutations: Mutation_2[]
                      }
                    | {
                        type: 'submit'
                      }
                    | {
                        type: 'xstate.done.actor.getDocument'
                        output: SanityDocument<DocumentType>
                      }
                    | {
                        type: 'xstate.done.actor.submitTransactions'
                        output: undefined
                      },
                    AnyEventObject
                  >
                }) => {
                  client: SanityClient
                  sharedListener:
                    | Observable<ReconnectEvent | WelcomeEvent | MutationEvent_2>
                    | undefined
                  id: string
                  remote: undefined
                  local: undefined
                  mutationEvents: never[]
                  stagedChanges: never[]
                  stashedChanges: never[]
                  error: undefined
                  fetchRemoteSnapshotAttempts: number
                  submitTransactionsAttempts: number
                  cache: Map<string, SanityDocument<DocumentType> | null> | undefined
                }
                readonly entry: readonly ['connect to server-sent events']
                readonly on: {
                  readonly mutate: {
                    readonly actions: readonly ['rebase local snapshot', 'stage mutation']
                  }
                }
                readonly initial: 'disconnected'
                readonly states: {
                  readonly disconnected: {
                    readonly on: {
                      readonly connect: {
                        readonly target: 'connecting'
                        readonly actions: readonly ['listen to server-sent events']
                      }
                    }
                  }
                  readonly connecting: {
                    readonly on: {
                      readonly welcome: 'connected'
                      readonly reconnect: 'reconnecting'
                      readonly error: 'connectFailure'
                    }
                    readonly tags: readonly ['busy']
                  }
                  readonly connectFailure: {
                    readonly on: {
                      readonly connect: {
                        readonly target: 'connecting'
                        readonly actions: readonly ['listen to server-sent events']
                      }
                    }
                    readonly entry: readonly [
                      'stop listening to server-sent events',
                      'assign error to context',
                    ]
                    readonly exit: readonly ['clear error from context']
                    readonly tags: readonly ['error']
                  }
                  readonly reconnecting: {
                    readonly on: {
                      readonly welcome: {
                        readonly target: 'connected'
                      }
                      readonly error: {
                        readonly target: 'connectFailure'
                      }
                    }
                    readonly tags: readonly ['busy', 'error']
                  }
                  readonly connected: {
                    readonly on: {
                      readonly mutation: {
                        readonly actions: readonly ['buffer remote mutation events']
                      }
                      readonly reconnect: 'reconnecting'
                    }
                    readonly entry: readonly ['clear error from context']
                    readonly initial: 'loading'
                    readonly states: {
                      readonly loading: {
                        readonly invoke: {
                          readonly src: 'fetch remote snapshot'
                          readonly id: 'getDocument'
                          readonly input: ({
                            context,
                          }: {
                            context: {
                              client: SanityClient
                              sharedListener?: ReturnType<typeof createSharedListener>
                              id: string
                              cache?: Map<string, SanityDocument<DocumentType> | null>
                              remote: SanityDocument<DocumentType> | null | undefined
                              local: SanityDocument<DocumentType> | null | undefined
                              mutationEvents: MutationEvent_2[]
                              stagedChanges: MutationGroup[]
                              stashedChanges: MutationGroup[]
                              error: unknown
                              fetchRemoteSnapshotAttempts: number
                              submitTransactionsAttempts: number
                            }
                            event:
                              | ReconnectEvent
                              | WelcomeEvent
                              | MutationEvent_2
                              | {
                                  type: 'error'
                                }
                              | {
                                  type: 'retry'
                                }
                              | {
                                  type: 'connect'
                                }
                              | {
                                  type: 'reconnect'
                                }
                              | {
                                  type: 'welcome'
                                }
                              | {
                                  type: 'mutate'
                                  mutations: Mutation_2[]
                                }
                              | {
                                  type: 'submit'
                                }
                              | {
                                  type: 'xstate.done.actor.getDocument'
                                  output: SanityDocument<DocumentType>
                                }
                              | {
                                  type: 'xstate.done.actor.submitTransactions'
                                  output: undefined
                                }
                            self: ActorRef<
                              MachineSnapshot<
                                {
                                  client: SanityClient
                                  sharedListener?: ReturnType<typeof createSharedListener>
                                  id: string
                                  cache?: Map<string, SanityDocument<DocumentType> | null>
                                  remote: SanityDocument<DocumentType> | null | undefined
                                  local: SanityDocument<DocumentType> | null | undefined
                                  mutationEvents: MutationEvent_2[]
                                  stagedChanges: MutationGroup[]
                                  stashedChanges: MutationGroup[]
                                  error: unknown
                                  fetchRemoteSnapshotAttempts: number
                                  submitTransactionsAttempts: number
                                },
                                | ReconnectEvent
                                | WelcomeEvent
                                | MutationEvent_2
                                | {
                                    type: 'error'
                                  }
                                | {
                                    type: 'retry'
                                  }
                                | {
                                    type: 'connect'
                                  }
                                | {
                                    type: 'reconnect'
                                  }
                                | {
                                    type: 'welcome'
                                  }
                                | {
                                    type: 'mutate'
                                    mutations: Mutation_2[]
                                  }
                                | {
                                    type: 'submit'
                                  }
                                | {
                                    type: 'xstate.done.actor.getDocument'
                                    output: SanityDocument<DocumentType>
                                  }
                                | {
                                    type: 'xstate.done.actor.submitTransactions'
                                    output: undefined
                                  },
                                Record<string, AnyActorRef>,
                                StateValue,
                                string,
                                unknown,
                                any,
                                any
                              >,
                              | ReconnectEvent
                              | WelcomeEvent
                              | MutationEvent_2
                              | {
                                  type: 'error'
                                }
                              | {
                                  type: 'retry'
                                }
                              | {
                                  type: 'connect'
                                }
                              | {
                                  type: 'reconnect'
                                }
                              | {
                                  type: 'welcome'
                                }
                              | {
                                  type: 'mutate'
                                  mutations: Mutation_2[]
                                }
                              | {
                                  type: 'submit'
                                }
                              | {
                                  type: 'xstate.done.actor.getDocument'
                                  output: SanityDocument<DocumentType>
                                }
                              | {
                                  type: 'xstate.done.actor.submitTransactions'
                                  output: undefined
                                },
                              AnyEventObject
                            >
                          }) => {
                            client: SanityClient
                            id: string
                          }
                          readonly onError: {
                            readonly target: 'loadFailure'
                          }
                          readonly onDone: {
                            readonly target: 'loaded'
                            readonly actions: readonly [
                              'rebase fetched remote snapshot',
                              'reset fetch attempts',
                            ]
                          }
                        }
                        readonly tags: readonly ['busy']
                      }
                      readonly loaded: {
                        readonly entry: readonly ['send sync event to parent']
                        readonly on: {
                          readonly mutation: {
                            readonly actions: readonly [
                              'apply mendoza patch',
                              'send mutation event to parent',
                            ]
                          }
                        }
                        readonly initial: 'pristine'
                        readonly states: {
                          readonly pristine: {
                            readonly on: {
                              readonly mutate: {
                                readonly actions: readonly [
                                  'rebase local snapshot',
                                  'stage mutation',
                                ]
                                readonly target: 'dirty'
                              }
                            }
                            readonly tags: readonly ['ready']
                          }
                          readonly dirty: {
                            readonly on: {
                              readonly submit: 'submitting'
                            }
                            readonly tags: readonly ['ready']
                          }
                          readonly submitting: {
                            readonly on: {
                              readonly mutate: {
                                readonly actions: readonly [
                                  'rebase local snapshot',
                                  'stash mutation',
                                ]
                              }
                            }
                            readonly invoke: {
                              readonly src: 'submit mutations as transactions'
                              readonly id: 'submitTransactions'
                              readonly input: ({
                                context,
                              }: {
                                context: {
                                  client: SanityClient
                                  sharedListener?: ReturnType<typeof createSharedListener>
                                  id: string
                                  cache?: Map<string, SanityDocument<DocumentType> | null>
                                  remote: SanityDocument<DocumentType> | null | undefined
                                  local: SanityDocument<DocumentType> | null | undefined
                                  mutationEvents: MutationEvent_2[]
                                  stagedChanges: MutationGroup[]
                                  stashedChanges: MutationGroup[]
                                  error: unknown
                                  fetchRemoteSnapshotAttempts: number
                                  submitTransactionsAttempts: number
                                }
                                event:
                                  | ReconnectEvent
                                  | WelcomeEvent
                                  | MutationEvent_2
                                  | {
                                      type: 'error'
                                    }
                                  | {
                                      type: 'retry'
                                    }
                                  | {
                                      type: 'connect'
                                    }
                                  | {
                                      type: 'reconnect'
                                    }
                                  | {
                                      type: 'welcome'
                                    }
                                  | {
                                      type: 'mutate'
                                      mutations: Mutation_2[]
                                    }
                                  | {
                                      type: 'submit'
                                    }
                                  | {
                                      type: 'xstate.done.actor.getDocument'
                                      output: SanityDocument<DocumentType>
                                    }
                                  | {
                                      type: 'xstate.done.actor.submitTransactions'
                                      output: undefined
                                    }
                                self: ActorRef<
                                  MachineSnapshot<
                                    {
                                      client: SanityClient
                                      sharedListener?: ReturnType<typeof createSharedListener>
                                      id: string
                                      cache?: Map<string, SanityDocument<DocumentType> | null>
                                      remote: SanityDocument<DocumentType> | null | undefined
                                      local: SanityDocument<DocumentType> | null | undefined
                                      mutationEvents: MutationEvent_2[]
                                      stagedChanges: MutationGroup[]
                                      stashedChanges: MutationGroup[]
                                      error: unknown
                                      fetchRemoteSnapshotAttempts: number
                                      submitTransactionsAttempts: number
                                    },
                                    | ReconnectEvent
                                    | WelcomeEvent
                                    | MutationEvent_2
                                    | {
                                        type: 'error'
                                      }
                                    | {
                                        type: 'retry'
                                      }
                                    | {
                                        type: 'connect'
                                      }
                                    | {
                                        type: 'reconnect'
                                      }
                                    | {
                                        type: 'welcome'
                                      }
                                    | {
                                        type: 'mutate'
                                        mutations: Mutation_2[]
                                      }
                                    | {
                                        type: 'submit'
                                      }
                                    | {
                                        type: 'xstate.done.actor.getDocument'
                                        output: SanityDocument<DocumentType>
                                      }
                                    | {
                                        type: 'xstate.done.actor.submitTransactions'
                                        output: undefined
                                      },
                                    Record<string, AnyActorRef>,
                                    StateValue,
                                    string,
                                    unknown,
                                    any,
                                    any
                                  >,
                                  | ReconnectEvent
                                  | WelcomeEvent
                                  | MutationEvent_2
                                  | {
                                      type: 'error'
                                    }
                                  | {
                                      type: 'retry'
                                    }
                                  | {
                                      type: 'connect'
                                    }
                                  | {
                                      type: 'reconnect'
                                    }
                                  | {
                                      type: 'welcome'
                                    }
                                  | {
                                      type: 'mutate'
                                      mutations: Mutation_2[]
                                    }
                                  | {
                                      type: 'submit'
                                    }
                                  | {
                                      type: 'xstate.done.actor.getDocument'
                                      output: SanityDocument<DocumentType>
                                    }
                                  | {
                                      type: 'xstate.done.actor.submitTransactions'
                                      output: undefined
                                    },
                                  AnyEventObject
                                >
                              }) => {
                                client: SanityClient
                                transactions: Transaction[]
                              }
                              readonly onError: {
                                readonly target: 'submitFailure'
                              }
                              readonly onDone: {
                                readonly target: 'pristine'
                                readonly actions: readonly [
                                  'restore stashed changes',
                                  'reset submit attempts',
                                  'send pristine event to parent',
                                ]
                              }
                            }
                            readonly tags: readonly ['busy', 'ready']
                          }
                          readonly submitFailure: {
                            readonly exit: readonly ['clear error from context']
                            readonly after: {
                              readonly submitTransactionsTimeout: {
                                readonly actions: readonly ['increment submit attempts']
                                readonly target: 'submitting'
                              }
                            }
                            readonly on: {
                              readonly retry: 'submitting'
                            }
                            readonly tags: readonly ['error', 'ready']
                          }
                        }
                      }
                      readonly loadFailure: {
                        readonly exit: readonly ['clear error from context']
                        readonly after: {
                          readonly fetchRemoteSnapshotTimeout: {
                            readonly actions: readonly ['increment fetch attempts']
                            readonly target: 'loading'
                          }
                        }
                        readonly on: {
                          readonly retry: 'loading'
                        }
                        readonly tags: readonly ['error']
                      }
                    }
                  }
                }
              }
            >
            id: string | undefined
          } extends infer T
            ? T extends {
                src: 'documentMutatorMachine'
                logic: StateMachine<
                  {
                    client: SanityClient
                    sharedListener?: ReturnType<typeof createSharedListener>
                    id: string
                    cache?: Map<string, SanityDocument<DocumentType> | null>
                    remote: SanityDocument<DocumentType> | null | undefined
                    local: SanityDocument<DocumentType> | null | undefined
                    mutationEvents: MutationEvent_2[]
                    stagedChanges: MutationGroup[]
                    stashedChanges: MutationGroup[]
                    error: unknown
                    fetchRemoteSnapshotAttempts: number
                    submitTransactionsAttempts: number
                  },
                  | WelcomeEvent
                  | ReconnectEvent
                  | MutationEvent_2
                  | {
                      type: 'error'
                    }
                  | {
                      type: 'retry'
                    }
                  | {
                      type: 'connect'
                    }
                  | {
                      type: 'reconnect'
                    }
                  | {
                      type: 'welcome'
                    }
                  | {
                      type: 'mutate'
                      mutations: Mutation_2[]
                    }
                  | {
                      type: 'submit'
                    }
                  | {
                      type: 'xstate.done.actor.getDocument'
                      output: SanityDocument<DocumentType>
                    }
                  | {
                      type: 'xstate.done.actor.submitTransactions'
                      output: undefined
                    },
                  {
                    [x: string]:
                      | ActorRefFromLogic<
                          PromiseActorLogic<
                            void | SanityDocument<Record<string, any>> | undefined,
                            {
                              client: SanityClient
                              id: string
                            },
                            EventObject
                          >
                        >
                      | ActorRefFromLogic<
                          PromiseActorLogic<
                            void,
                            {
                              client: SanityClient
                              transactions: Transaction[]
                            },
                            EventObject
                          >
                        >
                      | ActorRefFromLogic<
                          ObservableActorLogic<
                            WelcomeEvent | ReconnectEvent | MutationEvent_2,
                            {
                              listener: ReturnType<typeof createSharedListener>
                              id: string
                            },
                            EventObject
                          >
                        >
                      | undefined
                    getDocument?:
                      | ActorRefFromLogic<
                          PromiseActorLogic<
                            void | SanityDocument<Record<string, any>> | undefined,
                            {
                              client: SanityClient
                              id: string
                            },
                            EventObject
                          >
                        >
                      | undefined
                    submitTransactions?:
                      | ActorRefFromLogic<
                          PromiseActorLogic<
                            void,
                            {
                              client: SanityClient
                              transactions: Transaction[]
                            },
                            EventObject
                          >
                        >
                      | undefined
                  },
                  Values<{
                    'fetch remote snapshot': {
                      src: 'fetch remote snapshot'
                      logic: PromiseActorLogic<
                        void | SanityDocument<Record<string, any>> | undefined,
                        {
                          client: SanityClient
                          id: string
                        },
                        EventObject
                      >
                      id: 'getDocument'
                    }
                    'submit mutations as transactions': {
                      src: 'submit mutations as transactions'
                      logic: PromiseActorLogic<
                        void,
                        {
                          client: SanityClient
                          transactions: Transaction[]
                        },
                        EventObject
                      >
                      id: 'submitTransactions'
                    }
                    'server-sent events': {
                      src: 'server-sent events'
                      logic: ObservableActorLogic<
                        ReconnectEvent | WelcomeEvent | MutationEvent_2,
                        {
                          listener: ReturnType<typeof createSharedListener>
                          id: string
                        },
                        EventObject
                      >
                      id: string | undefined
                    }
                  }>,
                  Values<{
                    'assign error to context': {
                      type: 'assign error to context'
                      params: NonReducibleUnknown
                    }
                    'clear error from context': {
                      type: 'clear error from context'
                      params: NonReducibleUnknown
                    }
                    'connect to server-sent events': {
                      type: 'connect to server-sent events'
                      params: NonReducibleUnknown
                    }
                    'listen to server-sent events': {
                      type: 'listen to server-sent events'
                      params: NonReducibleUnknown
                    }
                    'stop listening to server-sent events': {
                      type: 'stop listening to server-sent events'
                      params: NonReducibleUnknown
                    }
                    'buffer remote mutation events': {
                      type: 'buffer remote mutation events'
                      params: NonReducibleUnknown
                    }
                    'restore stashed changes': {
                      type: 'restore stashed changes'
                      params: NonReducibleUnknown
                    }
                    'rebase fetched remote snapshot': {
                      type: 'rebase fetched remote snapshot'
                      params: NonReducibleUnknown
                    }
                    'apply mendoza patch': {
                      type: 'apply mendoza patch'
                      params: NonReducibleUnknown
                    }
                    'increment fetch attempts': {
                      type: 'increment fetch attempts'
                      params: NonReducibleUnknown
                    }
                    'reset fetch attempts': {
                      type: 'reset fetch attempts'
                      params: NonReducibleUnknown
                    }
                    'increment submit attempts': {
                      type: 'increment submit attempts'
                      params: NonReducibleUnknown
                    }
                    'reset submit attempts': {
                      type: 'reset submit attempts'
                      params: NonReducibleUnknown
                    }
                    'stage mutation': {
                      type: 'stage mutation'
                      params: NonReducibleUnknown
                    }
                    'stash mutation': {
                      type: 'stash mutation'
                      params: NonReducibleUnknown
                    }
                    'rebase local snapshot': {
                      type: 'rebase local snapshot'
                      params: NonReducibleUnknown
                    }
                    'send pristine event to parent': {
                      type: 'send pristine event to parent'
                      params: NonReducibleUnknown
                    }
                    'send sync event to parent': {
                      type: 'send sync event to parent'
                      params: NonReducibleUnknown
                    }
                    'send mutation event to parent': {
                      type: 'send mutation event to parent'
                      params: NonReducibleUnknown
                    }
                  }>,
                  never,
                  'fetchRemoteSnapshotTimeout' | 'submitTransactionsTimeout',
                  | 'disconnected'
                  | 'connecting'
                  | 'reconnecting'
                  | 'connectFailure'
                  | {
                      connected:
                        | 'loading'
                        | 'loadFailure'
                        | {
                            loaded: 'pristine' | 'dirty' | 'submitting' | 'submitFailure'
                          }
                    },
                  'error' | 'busy' | 'ready',
                  DocumentMutatorMachineInput,
                  NonReducibleUnknown,
                  EventObject,
                  MetaObject,
                  {
                    readonly id: 'document-mutator'
                    readonly context: ({
                      input,
                    }: {
                      spawn: {
                        <
                          TSrc_1 extends
                            | 'fetch remote snapshot'
                            | 'submit mutations as transactions'
                            | 'server-sent events',
                        >(
                          logic: TSrc_1,
                          ...[options]:
                            | ({
                                src: 'fetch remote snapshot'
                                logic: PromiseActorLogic<
                                  void | SanityDocument<Record<string, any>> | undefined,
                                  {
                                    client: SanityClient
                                    id: string
                                  },
                                  EventObject
                                >
                                id: 'getDocument'
                              } extends infer T_1
                                ? T_1 extends {
                                    src: 'fetch remote snapshot'
                                    logic: PromiseActorLogic<
                                      void | SanityDocument<Record<string, any>> | undefined,
                                      {
                                        client: SanityClient
                                        id: string
                                      },
                                      EventObject
                                    >
                                    id: 'getDocument'
                                  }
                                  ? T_1 extends {
                                      src: TSrc_1
                                    }
                                    ? ConditionalRequired<
                                        [
                                          options?:
                                            | ({
                                                id?: T_1['id'] | undefined
                                                systemId?: string
                                                input?: InputFrom<T_1['logic']> | undefined
                                                syncSnapshot?: boolean
                                              } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                                            | undefined,
                                        ],
                                        IsNotNever<RequiredActorOptions<T_1>>
                                      >
                                    : never
                                  : never
                                : never)
                            | ({
                                src: 'submit mutations as transactions'
                                logic: PromiseActorLogic<
                                  void,
                                  {
                                    client: SanityClient
                                    transactions: Transaction[]
                                  },
                                  EventObject
                                >
                                id: 'submitTransactions'
                              } extends infer T_1
                                ? T_1 extends {
                                    src: 'submit mutations as transactions'
                                    logic: PromiseActorLogic<
                                      void,
                                      {
                                        client: SanityClient
                                        transactions: Transaction[]
                                      },
                                      EventObject
                                    >
                                    id: 'submitTransactions'
                                  }
                                  ? T_1 extends {
                                      src: TSrc_1
                                    }
                                    ? ConditionalRequired<
                                        [
                                          options?:
                                            | ({
                                                id?: T_1['id'] | undefined
                                                systemId?: string
                                                input?: InputFrom<T_1['logic']> | undefined
                                                syncSnapshot?: boolean
                                              } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                                            | undefined,
                                        ],
                                        IsNotNever<RequiredActorOptions<T_1>>
                                      >
                                    : never
                                  : never
                                : never)
                            | ({
                                src: 'server-sent events'
                                logic: ObservableActorLogic<
                                  ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                  {
                                    listener: ReturnType<typeof createSharedListener>
                                    id: string
                                  },
                                  EventObject
                                >
                                id: string | undefined
                              } extends infer T_2
                                ? T_2 extends {
                                    src: 'server-sent events'
                                    logic: ObservableActorLogic<
                                      ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                      {
                                        listener: ReturnType<typeof createSharedListener>
                                        id: string
                                      },
                                      EventObject
                                    >
                                    id: string | undefined
                                  }
                                  ? T_2 extends {
                                      src: TSrc_1
                                    }
                                    ? ConditionalRequired<
                                        [
                                          options?:
                                            | ({
                                                id?: T_2['id'] | undefined
                                                systemId?: string
                                                input?: InputFrom<T_2['logic']> | undefined
                                                syncSnapshot?: boolean
                                              } & {[K_2 in RequiredActorOptions<T_2>]: unknown})
                                            | undefined,
                                        ],
                                        IsNotNever<RequiredActorOptions<T_2>>
                                      >
                                    : never
                                  : never
                                : never)
                        ): ActorRefFromLogic<
                          GetConcreteByKey<
                            Values<{
                              'fetch remote snapshot': {
                                src: 'fetch remote snapshot'
                                logic: PromiseActorLogic<
                                  void | SanityDocument<Record<string, any>> | undefined,
                                  {
                                    client: SanityClient
                                    id: string
                                  },
                                  EventObject
                                >
                                id: 'getDocument'
                              }
                              'submit mutations as transactions': {
                                src: 'submit mutations as transactions'
                                logic: PromiseActorLogic<
                                  void,
                                  {
                                    client: SanityClient
                                    transactions: Transaction[]
                                  },
                                  EventObject
                                >
                                id: 'submitTransactions'
                              }
                              'server-sent events': {
                                src: 'server-sent events'
                                logic: ObservableActorLogic<
                                  ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                  {
                                    listener: ReturnType<typeof createSharedListener>
                                    id: string
                                  },
                                  EventObject
                                >
                                id: string | undefined
                              }
                            }>,
                            'src',
                            TSrc_1
                          >['logic']
                        >
                        <TLogic extends AnyActorLogic>(
                          src: TLogic,
                          options?:
                            | {
                                id?: never
                                systemId?: string
                                input?: InputFrom<TLogic> | undefined
                                syncSnapshot?: boolean
                              }
                            | undefined,
                        ): ActorRefFromLogic<TLogic>
                      }
                      input: DocumentMutatorMachineInput
                      self: ActorRef<
                        MachineSnapshot<
                          {
                            client: SanityClient
                            sharedListener?: ReturnType<typeof createSharedListener>
                            id: string
                            cache?: Map<string, SanityDocument<DocumentType> | null>
                            remote: SanityDocument<DocumentType> | null | undefined
                            local: SanityDocument<DocumentType> | null | undefined
                            mutationEvents: MutationEvent_2[]
                            stagedChanges: MutationGroup[]
                            stashedChanges: MutationGroup[]
                            error: unknown
                            fetchRemoteSnapshotAttempts: number
                            submitTransactionsAttempts: number
                          },
                          | ReconnectEvent
                          | WelcomeEvent
                          | MutationEvent_2
                          | {
                              type: 'error'
                            }
                          | {
                              type: 'retry'
                            }
                          | {
                              type: 'connect'
                            }
                          | {
                              type: 'reconnect'
                            }
                          | {
                              type: 'welcome'
                            }
                          | {
                              type: 'mutate'
                              mutations: Mutation_2[]
                            }
                          | {
                              type: 'submit'
                            }
                          | {
                              type: 'xstate.done.actor.getDocument'
                              output: SanityDocument<DocumentType>
                            }
                          | {
                              type: 'xstate.done.actor.submitTransactions'
                              output: undefined
                            },
                          Record<string, AnyActorRef | undefined>,
                          StateValue,
                          string,
                          unknown,
                          any,
                          any
                        >,
                        | ReconnectEvent
                        | WelcomeEvent
                        | MutationEvent_2
                        | {
                            type: 'error'
                          }
                        | {
                            type: 'retry'
                          }
                        | {
                            type: 'connect'
                          }
                        | {
                            type: 'reconnect'
                          }
                        | {
                            type: 'welcome'
                          }
                        | {
                            type: 'mutate'
                            mutations: Mutation_2[]
                          }
                        | {
                            type: 'submit'
                          }
                        | {
                            type: 'xstate.done.actor.getDocument'
                            output: SanityDocument<DocumentType>
                          }
                        | {
                            type: 'xstate.done.actor.submitTransactions'
                            output: undefined
                          },
                        AnyEventObject
                      >
                    }) => {
                      client: SanityClient
                      sharedListener:
                        | Observable<ReconnectEvent | WelcomeEvent | MutationEvent_2>
                        | undefined
                      id: string
                      remote: undefined
                      local: undefined
                      mutationEvents: never[]
                      stagedChanges: never[]
                      stashedChanges: never[]
                      error: undefined
                      fetchRemoteSnapshotAttempts: number
                      submitTransactionsAttempts: number
                      cache: Map<string, SanityDocument<DocumentType> | null> | undefined
                    }
                    readonly entry: readonly ['connect to server-sent events']
                    readonly on: {
                      readonly mutate: {
                        readonly actions: readonly ['rebase local snapshot', 'stage mutation']
                      }
                    }
                    readonly initial: 'disconnected'
                    readonly states: {
                      readonly disconnected: {
                        readonly on: {
                          readonly connect: {
                            readonly target: 'connecting'
                            readonly actions: readonly ['listen to server-sent events']
                          }
                        }
                      }
                      readonly connecting: {
                        readonly on: {
                          readonly welcome: 'connected'
                          readonly reconnect: 'reconnecting'
                          readonly error: 'connectFailure'
                        }
                        readonly tags: readonly ['busy']
                      }
                      readonly connectFailure: {
                        readonly on: {
                          readonly connect: {
                            readonly target: 'connecting'
                            readonly actions: readonly ['listen to server-sent events']
                          }
                        }
                        readonly entry: readonly [
                          'stop listening to server-sent events',
                          'assign error to context',
                        ]
                        readonly exit: readonly ['clear error from context']
                        readonly tags: readonly ['error']
                      }
                      readonly reconnecting: {
                        readonly on: {
                          readonly welcome: {
                            readonly target: 'connected'
                          }
                          readonly error: {
                            readonly target: 'connectFailure'
                          }
                        }
                        readonly tags: readonly ['busy', 'error']
                      }
                      readonly connected: {
                        readonly on: {
                          readonly mutation: {
                            readonly actions: readonly ['buffer remote mutation events']
                          }
                          readonly reconnect: 'reconnecting'
                        }
                        readonly entry: readonly ['clear error from context']
                        readonly initial: 'loading'
                        readonly states: {
                          readonly loading: {
                            readonly invoke: {
                              readonly src: 'fetch remote snapshot'
                              readonly id: 'getDocument'
                              readonly input: ({
                                context,
                              }: {
                                context: {
                                  client: SanityClient
                                  sharedListener?: ReturnType<typeof createSharedListener>
                                  id: string
                                  cache?: Map<string, SanityDocument<DocumentType> | null>
                                  remote: SanityDocument<DocumentType> | null | undefined
                                  local: SanityDocument<DocumentType> | null | undefined
                                  mutationEvents: MutationEvent_2[]
                                  stagedChanges: MutationGroup[]
                                  stashedChanges: MutationGroup[]
                                  error: unknown
                                  fetchRemoteSnapshotAttempts: number
                                  submitTransactionsAttempts: number
                                }
                                event:
                                  | ReconnectEvent
                                  | WelcomeEvent
                                  | MutationEvent_2
                                  | {
                                      type: 'error'
                                    }
                                  | {
                                      type: 'retry'
                                    }
                                  | {
                                      type: 'connect'
                                    }
                                  | {
                                      type: 'reconnect'
                                    }
                                  | {
                                      type: 'welcome'
                                    }
                                  | {
                                      type: 'mutate'
                                      mutations: Mutation_2[]
                                    }
                                  | {
                                      type: 'submit'
                                    }
                                  | {
                                      type: 'xstate.done.actor.getDocument'
                                      output: SanityDocument<DocumentType>
                                    }
                                  | {
                                      type: 'xstate.done.actor.submitTransactions'
                                      output: undefined
                                    }
                                self: ActorRef<
                                  MachineSnapshot<
                                    {
                                      client: SanityClient
                                      sharedListener?: ReturnType<typeof createSharedListener>
                                      id: string
                                      cache?: Map<string, SanityDocument<DocumentType> | null>
                                      remote: SanityDocument<DocumentType> | null | undefined
                                      local: SanityDocument<DocumentType> | null | undefined
                                      mutationEvents: MutationEvent_2[]
                                      stagedChanges: MutationGroup[]
                                      stashedChanges: MutationGroup[]
                                      error: unknown
                                      fetchRemoteSnapshotAttempts: number
                                      submitTransactionsAttempts: number
                                    },
                                    | ReconnectEvent
                                    | WelcomeEvent
                                    | MutationEvent_2
                                    | {
                                        type: 'error'
                                      }
                                    | {
                                        type: 'retry'
                                      }
                                    | {
                                        type: 'connect'
                                      }
                                    | {
                                        type: 'reconnect'
                                      }
                                    | {
                                        type: 'welcome'
                                      }
                                    | {
                                        type: 'mutate'
                                        mutations: Mutation_2[]
                                      }
                                    | {
                                        type: 'submit'
                                      }
                                    | {
                                        type: 'xstate.done.actor.getDocument'
                                        output: SanityDocument<DocumentType>
                                      }
                                    | {
                                        type: 'xstate.done.actor.submitTransactions'
                                        output: undefined
                                      },
                                    Record<string, AnyActorRef>,
                                    StateValue,
                                    string,
                                    unknown,
                                    any,
                                    any
                                  >,
                                  | ReconnectEvent
                                  | WelcomeEvent
                                  | MutationEvent_2
                                  | {
                                      type: 'error'
                                    }
                                  | {
                                      type: 'retry'
                                    }
                                  | {
                                      type: 'connect'
                                    }
                                  | {
                                      type: 'reconnect'
                                    }
                                  | {
                                      type: 'welcome'
                                    }
                                  | {
                                      type: 'mutate'
                                      mutations: Mutation_2[]
                                    }
                                  | {
                                      type: 'submit'
                                    }
                                  | {
                                      type: 'xstate.done.actor.getDocument'
                                      output: SanityDocument<DocumentType>
                                    }
                                  | {
                                      type: 'xstate.done.actor.submitTransactions'
                                      output: undefined
                                    },
                                  AnyEventObject
                                >
                              }) => {
                                client: SanityClient
                                id: string
                              }
                              readonly onError: {
                                readonly target: 'loadFailure'
                              }
                              readonly onDone: {
                                readonly target: 'loaded'
                                readonly actions: readonly [
                                  'rebase fetched remote snapshot',
                                  'reset fetch attempts',
                                ]
                              }
                            }
                            readonly tags: readonly ['busy']
                          }
                          readonly loaded: {
                            readonly entry: readonly ['send sync event to parent']
                            readonly on: {
                              readonly mutation: {
                                readonly actions: readonly [
                                  'apply mendoza patch',
                                  'send mutation event to parent',
                                ]
                              }
                            }
                            readonly initial: 'pristine'
                            readonly states: {
                              readonly pristine: {
                                readonly on: {
                                  readonly mutate: {
                                    readonly actions: readonly [
                                      'rebase local snapshot',
                                      'stage mutation',
                                    ]
                                    readonly target: 'dirty'
                                  }
                                }
                                readonly tags: readonly ['ready']
                              }
                              readonly dirty: {
                                readonly on: {
                                  readonly submit: 'submitting'
                                }
                                readonly tags: readonly ['ready']
                              }
                              readonly submitting: {
                                readonly on: {
                                  readonly mutate: {
                                    readonly actions: readonly [
                                      'rebase local snapshot',
                                      'stash mutation',
                                    ]
                                  }
                                }
                                readonly invoke: {
                                  readonly src: 'submit mutations as transactions'
                                  readonly id: 'submitTransactions'
                                  readonly input: ({
                                    context,
                                  }: {
                                    context: {
                                      client: SanityClient
                                      sharedListener?: ReturnType<typeof createSharedListener>
                                      id: string
                                      cache?: Map<string, SanityDocument<DocumentType> | null>
                                      remote: SanityDocument<DocumentType> | null | undefined
                                      local: SanityDocument<DocumentType> | null | undefined
                                      mutationEvents: MutationEvent_2[]
                                      stagedChanges: MutationGroup[]
                                      stashedChanges: MutationGroup[]
                                      error: unknown
                                      fetchRemoteSnapshotAttempts: number
                                      submitTransactionsAttempts: number
                                    }
                                    event:
                                      | ReconnectEvent
                                      | WelcomeEvent
                                      | MutationEvent_2
                                      | {
                                          type: 'error'
                                        }
                                      | {
                                          type: 'retry'
                                        }
                                      | {
                                          type: 'connect'
                                        }
                                      | {
                                          type: 'reconnect'
                                        }
                                      | {
                                          type: 'welcome'
                                        }
                                      | {
                                          type: 'mutate'
                                          mutations: Mutation_2[]
                                        }
                                      | {
                                          type: 'submit'
                                        }
                                      | {
                                          type: 'xstate.done.actor.getDocument'
                                          output: SanityDocument<DocumentType>
                                        }
                                      | {
                                          type: 'xstate.done.actor.submitTransactions'
                                          output: undefined
                                        }
                                    self: ActorRef<
                                      MachineSnapshot<
                                        {
                                          client: SanityClient
                                          sharedListener?: ReturnType<typeof createSharedListener>
                                          id: string
                                          cache?: Map<string, SanityDocument<DocumentType> | null>
                                          remote: SanityDocument<DocumentType> | null | undefined
                                          local: SanityDocument<DocumentType> | null | undefined
                                          mutationEvents: MutationEvent_2[]
                                          stagedChanges: MutationGroup[]
                                          stashedChanges: MutationGroup[]
                                          error: unknown
                                          fetchRemoteSnapshotAttempts: number
                                          submitTransactionsAttempts: number
                                        },
                                        | ReconnectEvent
                                        | WelcomeEvent
                                        | MutationEvent_2
                                        | {
                                            type: 'error'
                                          }
                                        | {
                                            type: 'retry'
                                          }
                                        | {
                                            type: 'connect'
                                          }
                                        | {
                                            type: 'reconnect'
                                          }
                                        | {
                                            type: 'welcome'
                                          }
                                        | {
                                            type: 'mutate'
                                            mutations: Mutation_2[]
                                          }
                                        | {
                                            type: 'submit'
                                          }
                                        | {
                                            type: 'xstate.done.actor.getDocument'
                                            output: SanityDocument<DocumentType>
                                          }
                                        | {
                                            type: 'xstate.done.actor.submitTransactions'
                                            output: undefined
                                          },
                                        Record<string, AnyActorRef>,
                                        StateValue,
                                        string,
                                        unknown,
                                        any,
                                        any
                                      >,
                                      | ReconnectEvent
                                      | WelcomeEvent
                                      | MutationEvent_2
                                      | {
                                          type: 'error'
                                        }
                                      | {
                                          type: 'retry'
                                        }
                                      | {
                                          type: 'connect'
                                        }
                                      | {
                                          type: 'reconnect'
                                        }
                                      | {
                                          type: 'welcome'
                                        }
                                      | {
                                          type: 'mutate'
                                          mutations: Mutation_2[]
                                        }
                                      | {
                                          type: 'submit'
                                        }
                                      | {
                                          type: 'xstate.done.actor.getDocument'
                                          output: SanityDocument<DocumentType>
                                        }
                                      | {
                                          type: 'xstate.done.actor.submitTransactions'
                                          output: undefined
                                        },
                                      AnyEventObject
                                    >
                                  }) => {
                                    client: SanityClient
                                    transactions: Transaction[]
                                  }
                                  readonly onError: {
                                    readonly target: 'submitFailure'
                                  }
                                  readonly onDone: {
                                    readonly target: 'pristine'
                                    readonly actions: readonly [
                                      'restore stashed changes',
                                      'reset submit attempts',
                                      'send pristine event to parent',
                                    ]
                                  }
                                }
                                readonly tags: readonly ['busy', 'ready']
                              }
                              readonly submitFailure: {
                                readonly exit: readonly ['clear error from context']
                                readonly after: {
                                  readonly submitTransactionsTimeout: {
                                    readonly actions: readonly ['increment submit attempts']
                                    readonly target: 'submitting'
                                  }
                                }
                                readonly on: {
                                  readonly retry: 'submitting'
                                }
                                readonly tags: readonly ['error', 'ready']
                              }
                            }
                          }
                          readonly loadFailure: {
                            readonly exit: readonly ['clear error from context']
                            readonly after: {
                              readonly fetchRemoteSnapshotTimeout: {
                                readonly actions: readonly ['increment fetch attempts']
                                readonly target: 'loading'
                              }
                            }
                            readonly on: {
                              readonly retry: 'loading'
                            }
                            readonly tags: readonly ['error']
                          }
                        }
                      }
                    }
                  }
                >
                id: string | undefined
              }
              ? T extends {
                  src: TSrc
                }
                ? ConditionalRequired<
                    [
                      options?:
                        | ({
                            id?: T['id'] | undefined
                            systemId?: string
                            input?: InputFrom<T['logic']> | undefined
                            syncSnapshot?: boolean
                          } & {[K in RequiredActorOptions<T>]: unknown})
                        | undefined,
                    ],
                    IsNotNever<RequiredActorOptions<T>>
                  >
                : never
              : never
            : never
        ): ActorRefFromLogic<
          GetConcreteByKey<
            {
              src: 'documentMutatorMachine'
              logic: StateMachine<
                {
                  client: SanityClient
                  sharedListener?: ReturnType<typeof createSharedListener>
                  id: string
                  cache?: Map<string, SanityDocument<DocumentType> | null>
                  remote: SanityDocument<DocumentType> | null | undefined
                  local: SanityDocument<DocumentType> | null | undefined
                  mutationEvents: MutationEvent_2[]
                  stagedChanges: MutationGroup[]
                  stashedChanges: MutationGroup[]
                  error: unknown
                  fetchRemoteSnapshotAttempts: number
                  submitTransactionsAttempts: number
                },
                | WelcomeEvent
                | ReconnectEvent
                | MutationEvent_2
                | {
                    type: 'error'
                  }
                | {
                    type: 'retry'
                  }
                | {
                    type: 'connect'
                  }
                | {
                    type: 'reconnect'
                  }
                | {
                    type: 'welcome'
                  }
                | {
                    type: 'mutate'
                    mutations: Mutation_2[]
                  }
                | {
                    type: 'submit'
                  }
                | {
                    type: 'xstate.done.actor.getDocument'
                    output: SanityDocument<DocumentType>
                  }
                | {
                    type: 'xstate.done.actor.submitTransactions'
                    output: undefined
                  },
                {
                  [x: string]:
                    | ActorRefFromLogic<
                        PromiseActorLogic<
                          void | SanityDocument<Record<string, any>> | undefined,
                          {
                            client: SanityClient
                            id: string
                          },
                          EventObject
                        >
                      >
                    | ActorRefFromLogic<
                        PromiseActorLogic<
                          void,
                          {
                            client: SanityClient
                            transactions: Transaction[]
                          },
                          EventObject
                        >
                      >
                    | ActorRefFromLogic<
                        ObservableActorLogic<
                          WelcomeEvent | ReconnectEvent | MutationEvent_2,
                          {
                            listener: ReturnType<typeof createSharedListener>
                            id: string
                          },
                          EventObject
                        >
                      >
                    | undefined
                  getDocument?:
                    | ActorRefFromLogic<
                        PromiseActorLogic<
                          void | SanityDocument<Record<string, any>> | undefined,
                          {
                            client: SanityClient
                            id: string
                          },
                          EventObject
                        >
                      >
                    | undefined
                  submitTransactions?:
                    | ActorRefFromLogic<
                        PromiseActorLogic<
                          void,
                          {
                            client: SanityClient
                            transactions: Transaction[]
                          },
                          EventObject
                        >
                      >
                    | undefined
                },
                Values<{
                  'fetch remote snapshot': {
                    src: 'fetch remote snapshot'
                    logic: PromiseActorLogic<
                      void | SanityDocument<Record<string, any>> | undefined,
                      {
                        client: SanityClient
                        id: string
                      },
                      EventObject
                    >
                    id: 'getDocument'
                  }
                  'submit mutations as transactions': {
                    src: 'submit mutations as transactions'
                    logic: PromiseActorLogic<
                      void,
                      {
                        client: SanityClient
                        transactions: Transaction[]
                      },
                      EventObject
                    >
                    id: 'submitTransactions'
                  }
                  'server-sent events': {
                    src: 'server-sent events'
                    logic: ObservableActorLogic<
                      ReconnectEvent | WelcomeEvent | MutationEvent_2,
                      {
                        listener: ReturnType<typeof createSharedListener>
                        id: string
                      },
                      EventObject
                    >
                    id: string | undefined
                  }
                }>,
                Values<{
                  'assign error to context': {
                    type: 'assign error to context'
                    params: NonReducibleUnknown
                  }
                  'clear error from context': {
                    type: 'clear error from context'
                    params: NonReducibleUnknown
                  }
                  'connect to server-sent events': {
                    type: 'connect to server-sent events'
                    params: NonReducibleUnknown
                  }
                  'listen to server-sent events': {
                    type: 'listen to server-sent events'
                    params: NonReducibleUnknown
                  }
                  'stop listening to server-sent events': {
                    type: 'stop listening to server-sent events'
                    params: NonReducibleUnknown
                  }
                  'buffer remote mutation events': {
                    type: 'buffer remote mutation events'
                    params: NonReducibleUnknown
                  }
                  'restore stashed changes': {
                    type: 'restore stashed changes'
                    params: NonReducibleUnknown
                  }
                  'rebase fetched remote snapshot': {
                    type: 'rebase fetched remote snapshot'
                    params: NonReducibleUnknown
                  }
                  'apply mendoza patch': {
                    type: 'apply mendoza patch'
                    params: NonReducibleUnknown
                  }
                  'increment fetch attempts': {
                    type: 'increment fetch attempts'
                    params: NonReducibleUnknown
                  }
                  'reset fetch attempts': {
                    type: 'reset fetch attempts'
                    params: NonReducibleUnknown
                  }
                  'increment submit attempts': {
                    type: 'increment submit attempts'
                    params: NonReducibleUnknown
                  }
                  'reset submit attempts': {
                    type: 'reset submit attempts'
                    params: NonReducibleUnknown
                  }
                  'stage mutation': {
                    type: 'stage mutation'
                    params: NonReducibleUnknown
                  }
                  'stash mutation': {
                    type: 'stash mutation'
                    params: NonReducibleUnknown
                  }
                  'rebase local snapshot': {
                    type: 'rebase local snapshot'
                    params: NonReducibleUnknown
                  }
                  'send pristine event to parent': {
                    type: 'send pristine event to parent'
                    params: NonReducibleUnknown
                  }
                  'send sync event to parent': {
                    type: 'send sync event to parent'
                    params: NonReducibleUnknown
                  }
                  'send mutation event to parent': {
                    type: 'send mutation event to parent'
                    params: NonReducibleUnknown
                  }
                }>,
                never,
                'fetchRemoteSnapshotTimeout' | 'submitTransactionsTimeout',
                | 'disconnected'
                | 'connecting'
                | 'reconnecting'
                | 'connectFailure'
                | {
                    connected:
                      | 'loading'
                      | 'loadFailure'
                      | {
                          loaded: 'pristine' | 'dirty' | 'submitting' | 'submitFailure'
                        }
                  },
                'error' | 'busy' | 'ready',
                DocumentMutatorMachineInput,
                NonReducibleUnknown,
                EventObject,
                MetaObject,
                {
                  readonly id: 'document-mutator'
                  readonly context: ({
                    input,
                  }: {
                    spawn: {
                      <
                        TSrc_1 extends
                          | 'fetch remote snapshot'
                          | 'submit mutations as transactions'
                          | 'server-sent events',
                      >(
                        logic: TSrc_1,
                        ...[options]:
                          | ({
                              src: 'fetch remote snapshot'
                              logic: PromiseActorLogic<
                                void | SanityDocument<Record<string, any>> | undefined,
                                {
                                  client: SanityClient
                                  id: string
                                },
                                EventObject
                              >
                              id: 'getDocument'
                            } extends infer T_1
                              ? T_1 extends {
                                  src: 'fetch remote snapshot'
                                  logic: PromiseActorLogic<
                                    void | SanityDocument<Record<string, any>> | undefined,
                                    {
                                      client: SanityClient
                                      id: string
                                    },
                                    EventObject
                                  >
                                  id: 'getDocument'
                                }
                                ? T_1 extends {
                                    src: TSrc_1
                                  }
                                  ? ConditionalRequired<
                                      [
                                        options?:
                                          | ({
                                              id?: T_1['id'] | undefined
                                              systemId?: string
                                              input?: InputFrom<T_1['logic']> | undefined
                                              syncSnapshot?: boolean
                                            } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                                          | undefined,
                                      ],
                                      IsNotNever<RequiredActorOptions<T_1>>
                                    >
                                  : never
                                : never
                              : never)
                          | ({
                              src: 'submit mutations as transactions'
                              logic: PromiseActorLogic<
                                void,
                                {
                                  client: SanityClient
                                  transactions: Transaction[]
                                },
                                EventObject
                              >
                              id: 'submitTransactions'
                            } extends infer T_1
                              ? T_1 extends {
                                  src: 'submit mutations as transactions'
                                  logic: PromiseActorLogic<
                                    void,
                                    {
                                      client: SanityClient
                                      transactions: Transaction[]
                                    },
                                    EventObject
                                  >
                                  id: 'submitTransactions'
                                }
                                ? T_1 extends {
                                    src: TSrc_1
                                  }
                                  ? ConditionalRequired<
                                      [
                                        options?:
                                          | ({
                                              id?: T_1['id'] | undefined
                                              systemId?: string
                                              input?: InputFrom<T_1['logic']> | undefined
                                              syncSnapshot?: boolean
                                            } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                                          | undefined,
                                      ],
                                      IsNotNever<RequiredActorOptions<T_1>>
                                    >
                                  : never
                                : never
                              : never)
                          | ({
                              src: 'server-sent events'
                              logic: ObservableActorLogic<
                                ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                {
                                  listener: ReturnType<typeof createSharedListener>
                                  id: string
                                },
                                EventObject
                              >
                              id: string | undefined
                            } extends infer T_2
                              ? T_2 extends {
                                  src: 'server-sent events'
                                  logic: ObservableActorLogic<
                                    ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                    {
                                      listener: ReturnType<typeof createSharedListener>
                                      id: string
                                    },
                                    EventObject
                                  >
                                  id: string | undefined
                                }
                                ? T_2 extends {
                                    src: TSrc_1
                                  }
                                  ? ConditionalRequired<
                                      [
                                        options?:
                                          | ({
                                              id?: T_2['id'] | undefined
                                              systemId?: string
                                              input?: InputFrom<T_2['logic']> | undefined
                                              syncSnapshot?: boolean
                                            } & {[K_2 in RequiredActorOptions<T_2>]: unknown})
                                          | undefined,
                                      ],
                                      IsNotNever<RequiredActorOptions<T_2>>
                                    >
                                  : never
                                : never
                              : never)
                      ): ActorRefFromLogic<
                        GetConcreteByKey<
                          Values<{
                            'fetch remote snapshot': {
                              src: 'fetch remote snapshot'
                              logic: PromiseActorLogic<
                                void | SanityDocument<Record<string, any>> | undefined,
                                {
                                  client: SanityClient
                                  id: string
                                },
                                EventObject
                              >
                              id: 'getDocument'
                            }
                            'submit mutations as transactions': {
                              src: 'submit mutations as transactions'
                              logic: PromiseActorLogic<
                                void,
                                {
                                  client: SanityClient
                                  transactions: Transaction[]
                                },
                                EventObject
                              >
                              id: 'submitTransactions'
                            }
                            'server-sent events': {
                              src: 'server-sent events'
                              logic: ObservableActorLogic<
                                ReconnectEvent | WelcomeEvent | MutationEvent_2,
                                {
                                  listener: ReturnType<typeof createSharedListener>
                                  id: string
                                },
                                EventObject
                              >
                              id: string | undefined
                            }
                          }>,
                          'src',
                          TSrc_1
                        >['logic']
                      >
                      <TLogic extends AnyActorLogic>(
                        src: TLogic,
                        options?:
                          | {
                              id?: never
                              systemId?: string
                              input?: InputFrom<TLogic> | undefined
                              syncSnapshot?: boolean
                            }
                          | undefined,
                      ): ActorRefFromLogic<TLogic>
                    }
                    input: DocumentMutatorMachineInput
                    self: ActorRef<
                      MachineSnapshot<
                        {
                          client: SanityClient
                          sharedListener?: ReturnType<typeof createSharedListener>
                          id: string
                          cache?: Map<string, SanityDocument<DocumentType> | null>
                          remote: SanityDocument<DocumentType> | null | undefined
                          local: SanityDocument<DocumentType> | null | undefined
                          mutationEvents: MutationEvent_2[]
                          stagedChanges: MutationGroup[]
                          stashedChanges: MutationGroup[]
                          error: unknown
                          fetchRemoteSnapshotAttempts: number
                          submitTransactionsAttempts: number
                        },
                        | ReconnectEvent
                        | WelcomeEvent
                        | MutationEvent_2
                        | {
                            type: 'error'
                          }
                        | {
                            type: 'retry'
                          }
                        | {
                            type: 'connect'
                          }
                        | {
                            type: 'reconnect'
                          }
                        | {
                            type: 'welcome'
                          }
                        | {
                            type: 'mutate'
                            mutations: Mutation_2[]
                          }
                        | {
                            type: 'submit'
                          }
                        | {
                            type: 'xstate.done.actor.getDocument'
                            output: SanityDocument<DocumentType>
                          }
                        | {
                            type: 'xstate.done.actor.submitTransactions'
                            output: undefined
                          },
                        Record<string, AnyActorRef | undefined>,
                        StateValue,
                        string,
                        unknown,
                        any,
                        any
                      >,
                      | ReconnectEvent
                      | WelcomeEvent
                      | MutationEvent_2
                      | {
                          type: 'error'
                        }
                      | {
                          type: 'retry'
                        }
                      | {
                          type: 'connect'
                        }
                      | {
                          type: 'reconnect'
                        }
                      | {
                          type: 'welcome'
                        }
                      | {
                          type: 'mutate'
                          mutations: Mutation_2[]
                        }
                      | {
                          type: 'submit'
                        }
                      | {
                          type: 'xstate.done.actor.getDocument'
                          output: SanityDocument<DocumentType>
                        }
                      | {
                          type: 'xstate.done.actor.submitTransactions'
                          output: undefined
                        },
                      AnyEventObject
                    >
                  }) => {
                    client: SanityClient
                    sharedListener:
                      | Observable<ReconnectEvent | WelcomeEvent | MutationEvent_2>
                      | undefined
                    id: string
                    remote: undefined
                    local: undefined
                    mutationEvents: never[]
                    stagedChanges: never[]
                    stashedChanges: never[]
                    error: undefined
                    fetchRemoteSnapshotAttempts: number
                    submitTransactionsAttempts: number
                    cache: Map<string, SanityDocument<DocumentType> | null> | undefined
                  }
                  readonly entry: readonly ['connect to server-sent events']
                  readonly on: {
                    readonly mutate: {
                      readonly actions: readonly ['rebase local snapshot', 'stage mutation']
                    }
                  }
                  readonly initial: 'disconnected'
                  readonly states: {
                    readonly disconnected: {
                      readonly on: {
                        readonly connect: {
                          readonly target: 'connecting'
                          readonly actions: readonly ['listen to server-sent events']
                        }
                      }
                    }
                    readonly connecting: {
                      readonly on: {
                        readonly welcome: 'connected'
                        readonly reconnect: 'reconnecting'
                        readonly error: 'connectFailure'
                      }
                      readonly tags: readonly ['busy']
                    }
                    readonly connectFailure: {
                      readonly on: {
                        readonly connect: {
                          readonly target: 'connecting'
                          readonly actions: readonly ['listen to server-sent events']
                        }
                      }
                      readonly entry: readonly [
                        'stop listening to server-sent events',
                        'assign error to context',
                      ]
                      readonly exit: readonly ['clear error from context']
                      readonly tags: readonly ['error']
                    }
                    readonly reconnecting: {
                      readonly on: {
                        readonly welcome: {
                          readonly target: 'connected'
                        }
                        readonly error: {
                          readonly target: 'connectFailure'
                        }
                      }
                      readonly tags: readonly ['busy', 'error']
                    }
                    readonly connected: {
                      readonly on: {
                        readonly mutation: {
                          readonly actions: readonly ['buffer remote mutation events']
                        }
                        readonly reconnect: 'reconnecting'
                      }
                      readonly entry: readonly ['clear error from context']
                      readonly initial: 'loading'
                      readonly states: {
                        readonly loading: {
                          readonly invoke: {
                            readonly src: 'fetch remote snapshot'
                            readonly id: 'getDocument'
                            readonly input: ({
                              context,
                            }: {
                              context: {
                                client: SanityClient
                                sharedListener?: ReturnType<typeof createSharedListener>
                                id: string
                                cache?: Map<string, SanityDocument<DocumentType> | null>
                                remote: SanityDocument<DocumentType> | null | undefined
                                local: SanityDocument<DocumentType> | null | undefined
                                mutationEvents: MutationEvent_2[]
                                stagedChanges: MutationGroup[]
                                stashedChanges: MutationGroup[]
                                error: unknown
                                fetchRemoteSnapshotAttempts: number
                                submitTransactionsAttempts: number
                              }
                              event:
                                | ReconnectEvent
                                | WelcomeEvent
                                | MutationEvent_2
                                | {
                                    type: 'error'
                                  }
                                | {
                                    type: 'retry'
                                  }
                                | {
                                    type: 'connect'
                                  }
                                | {
                                    type: 'reconnect'
                                  }
                                | {
                                    type: 'welcome'
                                  }
                                | {
                                    type: 'mutate'
                                    mutations: Mutation_2[]
                                  }
                                | {
                                    type: 'submit'
                                  }
                                | {
                                    type: 'xstate.done.actor.getDocument'
                                    output: SanityDocument<DocumentType>
                                  }
                                | {
                                    type: 'xstate.done.actor.submitTransactions'
                                    output: undefined
                                  }
                              self: ActorRef<
                                MachineSnapshot<
                                  {
                                    client: SanityClient
                                    sharedListener?: ReturnType<typeof createSharedListener>
                                    id: string
                                    cache?: Map<string, SanityDocument<DocumentType> | null>
                                    remote: SanityDocument<DocumentType> | null | undefined
                                    local: SanityDocument<DocumentType> | null | undefined
                                    mutationEvents: MutationEvent_2[]
                                    stagedChanges: MutationGroup[]
                                    stashedChanges: MutationGroup[]
                                    error: unknown
                                    fetchRemoteSnapshotAttempts: number
                                    submitTransactionsAttempts: number
                                  },
                                  | ReconnectEvent
                                  | WelcomeEvent
                                  | MutationEvent_2
                                  | {
                                      type: 'error'
                                    }
                                  | {
                                      type: 'retry'
                                    }
                                  | {
                                      type: 'connect'
                                    }
                                  | {
                                      type: 'reconnect'
                                    }
                                  | {
                                      type: 'welcome'
                                    }
                                  | {
                                      type: 'mutate'
                                      mutations: Mutation_2[]
                                    }
                                  | {
                                      type: 'submit'
                                    }
                                  | {
                                      type: 'xstate.done.actor.getDocument'
                                      output: SanityDocument<DocumentType>
                                    }
                                  | {
                                      type: 'xstate.done.actor.submitTransactions'
                                      output: undefined
                                    },
                                  Record<string, AnyActorRef>,
                                  StateValue,
                                  string,
                                  unknown,
                                  any,
                                  any
                                >,
                                | ReconnectEvent
                                | WelcomeEvent
                                | MutationEvent_2
                                | {
                                    type: 'error'
                                  }
                                | {
                                    type: 'retry'
                                  }
                                | {
                                    type: 'connect'
                                  }
                                | {
                                    type: 'reconnect'
                                  }
                                | {
                                    type: 'welcome'
                                  }
                                | {
                                    type: 'mutate'
                                    mutations: Mutation_2[]
                                  }
                                | {
                                    type: 'submit'
                                  }
                                | {
                                    type: 'xstate.done.actor.getDocument'
                                    output: SanityDocument<DocumentType>
                                  }
                                | {
                                    type: 'xstate.done.actor.submitTransactions'
                                    output: undefined
                                  },
                                AnyEventObject
                              >
                            }) => {
                              client: SanityClient
                              id: string
                            }
                            readonly onError: {
                              readonly target: 'loadFailure'
                            }
                            readonly onDone: {
                              readonly target: 'loaded'
                              readonly actions: readonly [
                                'rebase fetched remote snapshot',
                                'reset fetch attempts',
                              ]
                            }
                          }
                          readonly tags: readonly ['busy']
                        }
                        readonly loaded: {
                          readonly entry: readonly ['send sync event to parent']
                          readonly on: {
                            readonly mutation: {
                              readonly actions: readonly [
                                'apply mendoza patch',
                                'send mutation event to parent',
                              ]
                            }
                          }
                          readonly initial: 'pristine'
                          readonly states: {
                            readonly pristine: {
                              readonly on: {
                                readonly mutate: {
                                  readonly actions: readonly [
                                    'rebase local snapshot',
                                    'stage mutation',
                                  ]
                                  readonly target: 'dirty'
                                }
                              }
                              readonly tags: readonly ['ready']
                            }
                            readonly dirty: {
                              readonly on: {
                                readonly submit: 'submitting'
                              }
                              readonly tags: readonly ['ready']
                            }
                            readonly submitting: {
                              readonly on: {
                                readonly mutate: {
                                  readonly actions: readonly [
                                    'rebase local snapshot',
                                    'stash mutation',
                                  ]
                                }
                              }
                              readonly invoke: {
                                readonly src: 'submit mutations as transactions'
                                readonly id: 'submitTransactions'
                                readonly input: ({
                                  context,
                                }: {
                                  context: {
                                    client: SanityClient
                                    sharedListener?: ReturnType<typeof createSharedListener>
                                    id: string
                                    cache?: Map<string, SanityDocument<DocumentType> | null>
                                    remote: SanityDocument<DocumentType> | null | undefined
                                    local: SanityDocument<DocumentType> | null | undefined
                                    mutationEvents: MutationEvent_2[]
                                    stagedChanges: MutationGroup[]
                                    stashedChanges: MutationGroup[]
                                    error: unknown
                                    fetchRemoteSnapshotAttempts: number
                                    submitTransactionsAttempts: number
                                  }
                                  event:
                                    | ReconnectEvent
                                    | WelcomeEvent
                                    | MutationEvent_2
                                    | {
                                        type: 'error'
                                      }
                                    | {
                                        type: 'retry'
                                      }
                                    | {
                                        type: 'connect'
                                      }
                                    | {
                                        type: 'reconnect'
                                      }
                                    | {
                                        type: 'welcome'
                                      }
                                    | {
                                        type: 'mutate'
                                        mutations: Mutation_2[]
                                      }
                                    | {
                                        type: 'submit'
                                      }
                                    | {
                                        type: 'xstate.done.actor.getDocument'
                                        output: SanityDocument<DocumentType>
                                      }
                                    | {
                                        type: 'xstate.done.actor.submitTransactions'
                                        output: undefined
                                      }
                                  self: ActorRef<
                                    MachineSnapshot<
                                      {
                                        client: SanityClient
                                        sharedListener?: ReturnType<typeof createSharedListener>
                                        id: string
                                        cache?: Map<string, SanityDocument<DocumentType> | null>
                                        remote: SanityDocument<DocumentType> | null | undefined
                                        local: SanityDocument<DocumentType> | null | undefined
                                        mutationEvents: MutationEvent_2[]
                                        stagedChanges: MutationGroup[]
                                        stashedChanges: MutationGroup[]
                                        error: unknown
                                        fetchRemoteSnapshotAttempts: number
                                        submitTransactionsAttempts: number
                                      },
                                      | ReconnectEvent
                                      | WelcomeEvent
                                      | MutationEvent_2
                                      | {
                                          type: 'error'
                                        }
                                      | {
                                          type: 'retry'
                                        }
                                      | {
                                          type: 'connect'
                                        }
                                      | {
                                          type: 'reconnect'
                                        }
                                      | {
                                          type: 'welcome'
                                        }
                                      | {
                                          type: 'mutate'
                                          mutations: Mutation_2[]
                                        }
                                      | {
                                          type: 'submit'
                                        }
                                      | {
                                          type: 'xstate.done.actor.getDocument'
                                          output: SanityDocument<DocumentType>
                                        }
                                      | {
                                          type: 'xstate.done.actor.submitTransactions'
                                          output: undefined
                                        },
                                      Record<string, AnyActorRef>,
                                      StateValue,
                                      string,
                                      unknown,
                                      any,
                                      any
                                    >,
                                    | ReconnectEvent
                                    | WelcomeEvent
                                    | MutationEvent_2
                                    | {
                                        type: 'error'
                                      }
                                    | {
                                        type: 'retry'
                                      }
                                    | {
                                        type: 'connect'
                                      }
                                    | {
                                        type: 'reconnect'
                                      }
                                    | {
                                        type: 'welcome'
                                      }
                                    | {
                                        type: 'mutate'
                                        mutations: Mutation_2[]
                                      }
                                    | {
                                        type: 'submit'
                                      }
                                    | {
                                        type: 'xstate.done.actor.getDocument'
                                        output: SanityDocument<DocumentType>
                                      }
                                    | {
                                        type: 'xstate.done.actor.submitTransactions'
                                        output: undefined
                                      },
                                    AnyEventObject
                                  >
                                }) => {
                                  client: SanityClient
                                  transactions: Transaction[]
                                }
                                readonly onError: {
                                  readonly target: 'submitFailure'
                                }
                                readonly onDone: {
                                  readonly target: 'pristine'
                                  readonly actions: readonly [
                                    'restore stashed changes',
                                    'reset submit attempts',
                                    'send pristine event to parent',
                                  ]
                                }
                              }
                              readonly tags: readonly ['busy', 'ready']
                            }
                            readonly submitFailure: {
                              readonly exit: readonly ['clear error from context']
                              readonly after: {
                                readonly submitTransactionsTimeout: {
                                  readonly actions: readonly ['increment submit attempts']
                                  readonly target: 'submitting'
                                }
                              }
                              readonly on: {
                                readonly retry: 'submitting'
                              }
                              readonly tags: readonly ['error', 'ready']
                            }
                          }
                        }
                        readonly loadFailure: {
                          readonly exit: readonly ['clear error from context']
                          readonly after: {
                            readonly fetchRemoteSnapshotTimeout: {
                              readonly actions: readonly ['increment fetch attempts']
                              readonly target: 'loading'
                            }
                          }
                          readonly on: {
                            readonly retry: 'loading'
                          }
                          readonly tags: readonly ['error']
                        }
                      }
                    }
                  }
                }
              >
              id: string | undefined
            },
            'src',
            TSrc
          >['logic']
        >
        <TLogic extends AnyActorLogic>(
          src: TLogic,
          options?:
            | {
                id?: never
                systemId?: string
                input?: InputFrom<TLogic> | undefined
                syncSnapshot?: boolean
              }
            | undefined,
        ): ActorRefFromLogic<TLogic>
      }
      input: DatasetMutatorMachineInput
      self: ActorRef<
        MachineSnapshot<
          {
            client: SanityClient
            /** A shared listener can be provided, if not it'll be created using `client.listen()` */
            sharedListener?: ReturnType<typeof createSharedListener>
            documents: Record<string, ActorRefFrom<ReturnType<typeof createDocumentMutator>>>
          },
          | {
              type: 'sync'
              id: string
              document: SanityDocumentBase
            }
          | {
              type: 'mutation'
              id: string
              effects: {
                apply: RawPatch
              }
              previousRev: string
              resultRev: string
            }
          | {
              type: 'rebased.local'
              id: string
              document: SanityDocumentBase
            }
          | {
              type: 'rebased.remote'
              id: string
              document: SanityDocumentBase
            }
          | {
              type: 'pristine'
              id: string
            }
          | {
              type: 'observe'
              documentId: string
            }
          | {
              type: 'unobserve'
              documentId: string
            }
          | {
              type: 'add document actor'
              documentId: string
            }
          | {
              type: 'stop document actor'
              documentId: string
            },
          Record<string, AnyActorRef | undefined>,
          StateValue,
          string,
          unknown,
          any,
          any
        >,
        | {
            type: 'sync'
            id: string
            document: SanityDocumentBase
          }
        | {
            type: 'mutation'
            id: string
            effects: {
              apply: RawPatch
            }
            previousRev: string
            resultRev: string
          }
        | {
            type: 'rebased.local'
            id: string
            document: SanityDocumentBase
          }
        | {
            type: 'rebased.remote'
            id: string
            document: SanityDocumentBase
          }
        | {
            type: 'pristine'
            id: string
          }
        | {
            type: 'observe'
            documentId: string
          }
        | {
            type: 'unobserve'
            documentId: string
          }
        | {
            type: 'add document actor'
            documentId: string
          }
        | {
            type: 'stop document actor'
            documentId: string
          },
        AnyEventObject
      >
    }) => {
      documents: {}
      client: SanityClient
      sharedListener: Observable<WelcomeEvent | ReconnectEvent | MutationEvent_2> | undefined
    }
    readonly on: {
      readonly 'sync': {
        readonly actions: readonly ['emit sync event']
      }
      readonly 'mutation': {
        readonly actions: readonly ['emit mutation event']
      }
      readonly 'rebased.*': {
        readonly actions: readonly ['emit rebased event']
      }
      readonly 'pristine': {
        readonly actions: readonly ['emit pristine event']
      }
      readonly 'observe': {
        readonly actions: readonly ['add document actor']
      }
      readonly 'unobserve': {
        readonly actions: readonly ['stop remote snapshot', 'remove remote snapshot from context']
      }
    }
    readonly initial: 'pristine'
    readonly states: {
      readonly pristine: {}
    }
  }
>

export declare const createDocumentMutator: (comlink: VisualEditingNode) => StateMachine<
  {
    client: SanityClient
    sharedListener?: ReturnType<createSharedListener>
    id: string
    cache?: Map<string, SanityDocument<DocumentType> | null>
    remote: SanityDocument<DocumentType> | null | undefined
    local: SanityDocument<DocumentType> | null | undefined
    mutationEvents: MutationEvent_2[]
    stagedChanges: MutationGroup[]
    stashedChanges: MutationGroup[]
    error: unknown
    fetchRemoteSnapshotAttempts: number
    submitTransactionsAttempts: number
  },
  | WelcomeEvent
  | ReconnectEvent
  | MutationEvent_2
  | {
      type: 'error'
    }
  | {
      type: 'retry'
    }
  | {
      type: 'connect'
    }
  | {
      type: 'reconnect'
    }
  | {
      type: 'welcome'
    }
  | {
      type: 'mutate'
      mutations: Mutation_2[]
    }
  | {
      type: 'submit'
    }
  | {
      type: 'xstate.done.actor.getDocument'
      output: SanityDocument<DocumentType>
    }
  | {
      type: 'xstate.done.actor.submitTransactions'
      output: undefined
    },
  {
    [x: string]:
      | ActorRefFromLogic<
          PromiseActorLogic<
            void | SanityDocument<Record<string, any>> | undefined,
            {
              client: SanityClient
              id: string
            },
            EventObject
          >
        >
      | ActorRefFromLogic<
          PromiseActorLogic<
            void,
            {
              client: SanityClient
              transactions: Transaction[]
            },
            EventObject
          >
        >
      | ActorRefFromLogic<
          ObservableActorLogic<
            WelcomeEvent | ReconnectEvent | MutationEvent_2,
            {
              listener: ReturnType<createSharedListener>
              id: string
            },
            EventObject
          >
        >
      | undefined
    getDocument?:
      | ActorRefFromLogic<
          PromiseActorLogic<
            void | SanityDocument<Record<string, any>> | undefined,
            {
              client: SanityClient
              id: string
            },
            EventObject
          >
        >
      | undefined
    submitTransactions?:
      | ActorRefFromLogic<
          PromiseActorLogic<
            void,
            {
              client: SanityClient
              transactions: Transaction[]
            },
            EventObject
          >
        >
      | undefined
  },
  Values<{
    'fetch remote snapshot': {
      src: 'fetch remote snapshot'
      logic: PromiseActorLogic<
        void | SanityDocument<Record<string, any>> | undefined,
        {
          client: SanityClient
          id: string
        },
        EventObject
      >
      id: 'getDocument'
    }
    'submit mutations as transactions': {
      src: 'submit mutations as transactions'
      logic: PromiseActorLogic<
        void,
        {
          client: SanityClient
          transactions: Transaction[]
        },
        EventObject
      >
      id: 'submitTransactions'
    }
    'server-sent events': {
      src: 'server-sent events'
      logic: ObservableActorLogic<
        ReconnectEvent | WelcomeEvent | MutationEvent_2,
        {
          listener: ReturnType<createSharedListener>
          id: string
        },
        EventObject
      >
      id: string | undefined
    }
  }>,
  Values<{
    'assign error to context': {
      type: 'assign error to context'
      params: NonReducibleUnknown
    }
    'clear error from context': {
      type: 'clear error from context'
      params: NonReducibleUnknown
    }
    'connect to server-sent events': {
      type: 'connect to server-sent events'
      params: NonReducibleUnknown
    }
    'listen to server-sent events': {
      type: 'listen to server-sent events'
      params: NonReducibleUnknown
    }
    'stop listening to server-sent events': {
      type: 'stop listening to server-sent events'
      params: NonReducibleUnknown
    }
    'buffer remote mutation events': {
      type: 'buffer remote mutation events'
      params: NonReducibleUnknown
    }
    'restore stashed changes': {
      type: 'restore stashed changes'
      params: NonReducibleUnknown
    }
    'rebase fetched remote snapshot': {
      type: 'rebase fetched remote snapshot'
      params: NonReducibleUnknown
    }
    'apply mendoza patch': {
      type: 'apply mendoza patch'
      params: NonReducibleUnknown
    }
    'increment fetch attempts': {
      type: 'increment fetch attempts'
      params: NonReducibleUnknown
    }
    'reset fetch attempts': {
      type: 'reset fetch attempts'
      params: NonReducibleUnknown
    }
    'increment submit attempts': {
      type: 'increment submit attempts'
      params: NonReducibleUnknown
    }
    'reset submit attempts': {
      type: 'reset submit attempts'
      params: NonReducibleUnknown
    }
    'stage mutation': {
      type: 'stage mutation'
      params: NonReducibleUnknown
    }
    'stash mutation': {
      type: 'stash mutation'
      params: NonReducibleUnknown
    }
    'rebase local snapshot': {
      type: 'rebase local snapshot'
      params: NonReducibleUnknown
    }
    'send pristine event to parent': {
      type: 'send pristine event to parent'
      params: NonReducibleUnknown
    }
    'send sync event to parent': {
      type: 'send sync event to parent'
      params: NonReducibleUnknown
    }
    'send mutation event to parent': {
      type: 'send mutation event to parent'
      params: NonReducibleUnknown
    }
  }>,
  never,
  'fetchRemoteSnapshotTimeout' | 'submitTransactionsTimeout',
  | 'disconnected'
  | 'connecting'
  | 'reconnecting'
  | 'connectFailure'
  | {
      connected:
        | 'loading'
        | 'loadFailure'
        | {
            loaded: 'pristine' | 'dirty' | 'submitting' | 'submitFailure'
          }
    },
  'error' | 'busy' | 'ready',
  DocumentMutatorMachineInput,
  NonReducibleUnknown,
  EventObject,
  MetaObject,
  {
    readonly id: 'document-mutator'
    readonly context: ({
      input,
    }: {
      spawn: {
        <
          TSrc extends
            | 'fetch remote snapshot'
            | 'submit mutations as transactions'
            | 'server-sent events',
        >(
          logic: TSrc,
          ...[options]:
            | ({
                src: 'fetch remote snapshot'
                logic: PromiseActorLogic<
                  void | SanityDocument<Record<string, any>> | undefined,
                  {
                    client: SanityClient
                    id: string
                  },
                  EventObject
                >
                id: 'getDocument'
              } extends infer T
                ? T extends {
                    src: 'fetch remote snapshot'
                    logic: PromiseActorLogic<
                      void | SanityDocument<Record<string, any>> | undefined,
                      {
                        client: SanityClient
                        id: string
                      },
                      EventObject
                    >
                    id: 'getDocument'
                  }
                  ? T extends {
                      src: TSrc
                    }
                    ? ConditionalRequired<
                        [
                          options?:
                            | ({
                                id?: T['id'] | undefined
                                systemId?: string
                                input?: InputFrom<T['logic']> | undefined
                                syncSnapshot?: boolean
                              } & {[K in RequiredActorOptions<T>]: unknown})
                            | undefined,
                        ],
                        IsNotNever<RequiredActorOptions<T>>
                      >
                    : never
                  : never
                : never)
            | ({
                src: 'submit mutations as transactions'
                logic: PromiseActorLogic<
                  void,
                  {
                    client: SanityClient
                    transactions: Transaction[]
                  },
                  EventObject
                >
                id: 'submitTransactions'
              } extends infer T_1
                ? T_1 extends {
                    src: 'submit mutations as transactions'
                    logic: PromiseActorLogic<
                      void,
                      {
                        client: SanityClient
                        transactions: Transaction[]
                      },
                      EventObject
                    >
                    id: 'submitTransactions'
                  }
                  ? T_1 extends {
                      src: TSrc
                    }
                    ? ConditionalRequired<
                        [
                          options?:
                            | ({
                                id?: T_1['id'] | undefined
                                systemId?: string
                                input?: InputFrom<T_1['logic']> | undefined
                                syncSnapshot?: boolean
                              } & {[K_1 in RequiredActorOptions<T_1>]: unknown})
                            | undefined,
                        ],
                        IsNotNever<RequiredActorOptions<T_1>>
                      >
                    : never
                  : never
                : never)
            | ({
                src: 'server-sent events'
                logic: ObservableActorLogic<
                  ReconnectEvent | WelcomeEvent | MutationEvent_2,
                  {
                    listener: ReturnType<createSharedListener>
                    id: string
                  },
                  EventObject
                >
                id: string | undefined
              } extends infer T_2
                ? T_2 extends {
                    src: 'server-sent events'
                    logic: ObservableActorLogic<
                      ReconnectEvent | WelcomeEvent | MutationEvent_2,
                      {
                        listener: ReturnType<createSharedListener>
                        id: string
                      },
                      EventObject
                    >
                    id: string | undefined
                  }
                  ? T_2 extends {
                      src: TSrc
                    }
                    ? ConditionalRequired<
                        [
                          options?:
                            | ({
                                id?: T_2['id'] | undefined
                                systemId?: string
                                input?: InputFrom<T_2['logic']> | undefined
                                syncSnapshot?: boolean
                              } & {[K_2 in RequiredActorOptions<T_2>]: unknown})
                            | undefined,
                        ],
                        IsNotNever<RequiredActorOptions<T_2>>
                      >
                    : never
                  : never
                : never)
        ): ActorRefFromLogic<
          GetConcreteByKey<
            Values<{
              'fetch remote snapshot': {
                src: 'fetch remote snapshot'
                logic: PromiseActorLogic<
                  void | SanityDocument<Record<string, any>> | undefined,
                  {
                    client: SanityClient
                    id: string
                  },
                  EventObject
                >
                id: 'getDocument'
              }
              'submit mutations as transactions': {
                src: 'submit mutations as transactions'
                logic: PromiseActorLogic<
                  void,
                  {
                    client: SanityClient
                    transactions: Transaction[]
                  },
                  EventObject
                >
                id: 'submitTransactions'
              }
              'server-sent events': {
                src: 'server-sent events'
                logic: ObservableActorLogic<
                  ReconnectEvent | WelcomeEvent | MutationEvent_2,
                  {
                    listener: ReturnType<createSharedListener>
                    id: string
                  },
                  EventObject
                >
                id: string | undefined
              }
            }>,
            'src',
            TSrc
          >['logic']
        >
        <TLogic extends AnyActorLogic>(
          src: TLogic,
          options?:
            | {
                id?: never
                systemId?: string
                input?: InputFrom<TLogic> | undefined
                syncSnapshot?: boolean
              }
            | undefined,
        ): ActorRefFromLogic<TLogic>
      }
      input: DocumentMutatorMachineInput
      self: ActorRef<
        MachineSnapshot<
          {
            client: SanityClient
            sharedListener?: ReturnType<createSharedListener>
            id: string
            cache?: Map<string, SanityDocument<DocumentType> | null>
            remote: SanityDocument<DocumentType> | null | undefined
            local: SanityDocument<DocumentType> | null | undefined
            mutationEvents: MutationEvent_2[]
            stagedChanges: MutationGroup[]
            stashedChanges: MutationGroup[]
            error: unknown
            fetchRemoteSnapshotAttempts: number
            submitTransactionsAttempts: number
          },
          | ReconnectEvent
          | WelcomeEvent
          | MutationEvent_2
          | {
              type: 'error'
            }
          | {
              type: 'retry'
            }
          | {
              type: 'connect'
            }
          | {
              type: 'reconnect'
            }
          | {
              type: 'welcome'
            }
          | {
              type: 'mutate'
              mutations: Mutation_2[]
            }
          | {
              type: 'submit'
            }
          | {
              type: 'xstate.done.actor.getDocument'
              output: SanityDocument<DocumentType>
            }
          | {
              type: 'xstate.done.actor.submitTransactions'
              output: undefined
            },
          Record<string, AnyActorRef | undefined>,
          StateValue,
          string,
          unknown,
          any,
          any
        >,
        | ReconnectEvent
        | WelcomeEvent
        | MutationEvent_2
        | {
            type: 'error'
          }
        | {
            type: 'retry'
          }
        | {
            type: 'connect'
          }
        | {
            type: 'reconnect'
          }
        | {
            type: 'welcome'
          }
        | {
            type: 'mutate'
            mutations: Mutation_2[]
          }
        | {
            type: 'submit'
          }
        | {
            type: 'xstate.done.actor.getDocument'
            output: SanityDocument<DocumentType>
          }
        | {
            type: 'xstate.done.actor.submitTransactions'
            output: undefined
          },
        AnyEventObject
      >
    }) => {
      client: SanityClient
      sharedListener: Observable<ReconnectEvent | WelcomeEvent | MutationEvent_2> | undefined
      id: string
      remote: undefined
      local: undefined
      mutationEvents: never[]
      stagedChanges: never[]
      stashedChanges: never[]
      error: undefined
      fetchRemoteSnapshotAttempts: number
      submitTransactionsAttempts: number
      cache: Map<string, SanityDocument<DocumentType> | null> | undefined
    }
    readonly entry: readonly ['connect to server-sent events']
    readonly on: {
      readonly mutate: {
        readonly actions: readonly ['rebase local snapshot', 'stage mutation']
      }
    }
    readonly initial: 'disconnected'
    readonly states: {
      readonly disconnected: {
        readonly on: {
          readonly connect: {
            readonly target: 'connecting'
            readonly actions: readonly ['listen to server-sent events']
          }
        }
      }
      readonly connecting: {
        readonly on: {
          readonly welcome: 'connected'
          readonly reconnect: 'reconnecting'
          readonly error: 'connectFailure'
        }
        readonly tags: readonly ['busy']
      }
      readonly connectFailure: {
        readonly on: {
          readonly connect: {
            readonly target: 'connecting'
            readonly actions: readonly ['listen to server-sent events']
          }
        }
        readonly entry: readonly ['stop listening to server-sent events', 'assign error to context']
        readonly exit: readonly ['clear error from context']
        readonly tags: readonly ['error']
      }
      readonly reconnecting: {
        readonly on: {
          readonly welcome: {
            readonly target: 'connected'
          }
          readonly error: {
            readonly target: 'connectFailure'
          }
        }
        readonly tags: readonly ['busy', 'error']
      }
      readonly connected: {
        readonly on: {
          readonly mutation: {
            readonly actions: readonly ['buffer remote mutation events']
          }
          readonly reconnect: 'reconnecting'
        }
        readonly entry: readonly ['clear error from context']
        readonly initial: 'loading'
        readonly states: {
          readonly loading: {
            readonly invoke: {
              readonly src: 'fetch remote snapshot'
              readonly id: 'getDocument'
              readonly input: ({
                context,
              }: {
                context: {
                  client: SanityClient
                  sharedListener?: ReturnType<createSharedListener>
                  id: string
                  cache?: Map<string, SanityDocument<DocumentType> | null>
                  remote: SanityDocument<DocumentType> | null | undefined
                  local: SanityDocument<DocumentType> | null | undefined
                  mutationEvents: MutationEvent_2[]
                  stagedChanges: MutationGroup[]
                  stashedChanges: MutationGroup[]
                  error: unknown
                  fetchRemoteSnapshotAttempts: number
                  submitTransactionsAttempts: number
                }
                event:
                  | ReconnectEvent
                  | WelcomeEvent
                  | MutationEvent_2
                  | {
                      type: 'error'
                    }
                  | {
                      type: 'retry'
                    }
                  | {
                      type: 'connect'
                    }
                  | {
                      type: 'reconnect'
                    }
                  | {
                      type: 'welcome'
                    }
                  | {
                      type: 'mutate'
                      mutations: Mutation_2[]
                    }
                  | {
                      type: 'submit'
                    }
                  | {
                      type: 'xstate.done.actor.getDocument'
                      output: SanityDocument<DocumentType>
                    }
                  | {
                      type: 'xstate.done.actor.submitTransactions'
                      output: undefined
                    }
                self: ActorRef<
                  MachineSnapshot<
                    {
                      client: SanityClient
                      sharedListener?: ReturnType<createSharedListener>
                      id: string
                      cache?: Map<string, SanityDocument<DocumentType> | null>
                      remote: SanityDocument<DocumentType> | null | undefined
                      local: SanityDocument<DocumentType> | null | undefined
                      mutationEvents: MutationEvent_2[]
                      stagedChanges: MutationGroup[]
                      stashedChanges: MutationGroup[]
                      error: unknown
                      fetchRemoteSnapshotAttempts: number
                      submitTransactionsAttempts: number
                    },
                    | ReconnectEvent
                    | WelcomeEvent
                    | MutationEvent_2
                    | {
                        type: 'error'
                      }
                    | {
                        type: 'retry'
                      }
                    | {
                        type: 'connect'
                      }
                    | {
                        type: 'reconnect'
                      }
                    | {
                        type: 'welcome'
                      }
                    | {
                        type: 'mutate'
                        mutations: Mutation_2[]
                      }
                    | {
                        type: 'submit'
                      }
                    | {
                        type: 'xstate.done.actor.getDocument'
                        output: SanityDocument<DocumentType>
                      }
                    | {
                        type: 'xstate.done.actor.submitTransactions'
                        output: undefined
                      },
                    Record<string, AnyActorRef>,
                    StateValue,
                    string,
                    unknown,
                    any,
                    any
                  >,
                  | ReconnectEvent
                  | WelcomeEvent
                  | MutationEvent_2
                  | {
                      type: 'error'
                    }
                  | {
                      type: 'retry'
                    }
                  | {
                      type: 'connect'
                    }
                  | {
                      type: 'reconnect'
                    }
                  | {
                      type: 'welcome'
                    }
                  | {
                      type: 'mutate'
                      mutations: Mutation_2[]
                    }
                  | {
                      type: 'submit'
                    }
                  | {
                      type: 'xstate.done.actor.getDocument'
                      output: SanityDocument<DocumentType>
                    }
                  | {
                      type: 'xstate.done.actor.submitTransactions'
                      output: undefined
                    },
                  AnyEventObject
                >
              }) => {
                client: SanityClient
                id: string
              }
              readonly onError: {
                readonly target: 'loadFailure'
              }
              readonly onDone: {
                readonly target: 'loaded'
                readonly actions: readonly [
                  'rebase fetched remote snapshot',
                  'reset fetch attempts',
                ]
              }
            }
            readonly tags: readonly ['busy']
          }
          readonly loaded: {
            readonly entry: readonly ['send sync event to parent']
            readonly on: {
              readonly mutation: {
                readonly actions: readonly ['apply mendoza patch', 'send mutation event to parent']
              }
            }
            readonly initial: 'pristine'
            readonly states: {
              readonly pristine: {
                readonly on: {
                  readonly mutate: {
                    readonly actions: readonly ['rebase local snapshot', 'stage mutation']
                    readonly target: 'dirty'
                  }
                }
                readonly tags: readonly ['ready']
              }
              readonly dirty: {
                readonly on: {
                  readonly submit: 'submitting'
                }
                readonly tags: readonly ['ready']
              }
              readonly submitting: {
                readonly on: {
                  readonly mutate: {
                    readonly actions: readonly ['rebase local snapshot', 'stash mutation']
                  }
                }
                readonly invoke: {
                  readonly src: 'submit mutations as transactions'
                  readonly id: 'submitTransactions'
                  readonly input: ({
                    context,
                  }: {
                    context: {
                      client: SanityClient
                      sharedListener?: ReturnType<createSharedListener>
                      id: string
                      cache?: Map<string, SanityDocument<DocumentType> | null>
                      remote: SanityDocument<DocumentType> | null | undefined
                      local: SanityDocument<DocumentType> | null | undefined
                      mutationEvents: MutationEvent_2[]
                      stagedChanges: MutationGroup[]
                      stashedChanges: MutationGroup[]
                      error: unknown
                      fetchRemoteSnapshotAttempts: number
                      submitTransactionsAttempts: number
                    }
                    event:
                      | ReconnectEvent
                      | WelcomeEvent
                      | MutationEvent_2
                      | {
                          type: 'error'
                        }
                      | {
                          type: 'retry'
                        }
                      | {
                          type: 'connect'
                        }
                      | {
                          type: 'reconnect'
                        }
                      | {
                          type: 'welcome'
                        }
                      | {
                          type: 'mutate'
                          mutations: Mutation_2[]
                        }
                      | {
                          type: 'submit'
                        }
                      | {
                          type: 'xstate.done.actor.getDocument'
                          output: SanityDocument<DocumentType>
                        }
                      | {
                          type: 'xstate.done.actor.submitTransactions'
                          output: undefined
                        }
                    self: ActorRef<
                      MachineSnapshot<
                        {
                          client: SanityClient
                          sharedListener?: ReturnType<createSharedListener>
                          id: string
                          cache?: Map<string, SanityDocument<DocumentType> | null>
                          remote: SanityDocument<DocumentType> | null | undefined
                          local: SanityDocument<DocumentType> | null | undefined
                          mutationEvents: MutationEvent_2[]
                          stagedChanges: MutationGroup[]
                          stashedChanges: MutationGroup[]
                          error: unknown
                          fetchRemoteSnapshotAttempts: number
                          submitTransactionsAttempts: number
                        },
                        | ReconnectEvent
                        | WelcomeEvent
                        | MutationEvent_2
                        | {
                            type: 'error'
                          }
                        | {
                            type: 'retry'
                          }
                        | {
                            type: 'connect'
                          }
                        | {
                            type: 'reconnect'
                          }
                        | {
                            type: 'welcome'
                          }
                        | {
                            type: 'mutate'
                            mutations: Mutation_2[]
                          }
                        | {
                            type: 'submit'
                          }
                        | {
                            type: 'xstate.done.actor.getDocument'
                            output: SanityDocument<DocumentType>
                          }
                        | {
                            type: 'xstate.done.actor.submitTransactions'
                            output: undefined
                          },
                        Record<string, AnyActorRef>,
                        StateValue,
                        string,
                        unknown,
                        any,
                        any
                      >,
                      | ReconnectEvent
                      | WelcomeEvent
                      | MutationEvent_2
                      | {
                          type: 'error'
                        }
                      | {
                          type: 'retry'
                        }
                      | {
                          type: 'connect'
                        }
                      | {
                          type: 'reconnect'
                        }
                      | {
                          type: 'welcome'
                        }
                      | {
                          type: 'mutate'
                          mutations: Mutation_2[]
                        }
                      | {
                          type: 'submit'
                        }
                      | {
                          type: 'xstate.done.actor.getDocument'
                          output: SanityDocument<DocumentType>
                        }
                      | {
                          type: 'xstate.done.actor.submitTransactions'
                          output: undefined
                        },
                      AnyEventObject
                    >
                  }) => {
                    client: SanityClient
                    transactions: Transaction[]
                  }
                  readonly onError: {
                    readonly target: 'submitFailure'
                  }
                  readonly onDone: {
                    readonly target: 'pristine'
                    readonly actions: readonly [
                      'restore stashed changes',
                      'reset submit attempts',
                      'send pristine event to parent',
                    ]
                  }
                }
                readonly tags: readonly ['busy', 'ready']
              }
              readonly submitFailure: {
                readonly exit: readonly ['clear error from context']
                readonly after: {
                  readonly submitTransactionsTimeout: {
                    readonly actions: readonly ['increment submit attempts']
                    readonly target: 'submitting'
                  }
                }
                readonly on: {
                  readonly retry: 'submitting'
                }
                readonly tags: readonly ['error', 'ready']
              }
            }
          }
          readonly loadFailure: {
            readonly exit: readonly ['clear error from context']
            readonly after: {
              readonly fetchRemoteSnapshotTimeout: {
                readonly actions: readonly ['increment fetch attempts']
                readonly target: 'loading'
              }
            }
            readonly on: {
              readonly retry: 'loading'
            }
            readonly tags: readonly ['error']
          }
        }
      }
    }
  }
>

export declare interface DatasetMutatorMachineInput
  extends Omit<DocumentMutatorMachineInput, 'id'> {
  client: SanityClient
  /** A shared listener can be provided, if not it'll be created using `client.listen()` */
  sharedListener?: ReturnType<typeof createSharedListener>
}

export declare interface DocumentSchema {
  type: 'document'
  name: string
  title?: string
  icon?: string
  fields: Record<string, SchemaObjectField>
}

export declare type DocumentsGet = <T extends Record<string, any>>(
  documentId: string,
) => OptimisticDocument<T>

export declare type DocumentsMutate = (
  documentId: string,
  mutations: Mutation[],
  options?: {
    commit?:
      | boolean
      | {
          debounce: number
        }
  },
) => void

export declare type EmptyActor = typeof emptyActor

export declare const emptyActor: ActorRef<Snapshot<undefined>, AnyEventObject, AnyEventObject>

/**
 * Preview frame history refresh event, emitted by Presentation Tool
 * @public
 */
export declare type HistoryRefresh =
  | {
      /**
       * source 'manual' means the refresh button were clicked by the user
       */
      source: 'manual'
      /**
       * If true then there's either preview-kit or a loader connected on the page
       */
      livePreviewEnabled: boolean
    }
  | {
      /**
       * source 'mutation' means a document were mutated and the preview might need to refresh
       */
      source: 'mutation'
      /**
       * If true then there's either preview-kit or a loader connected on the page
       */
      livePreviewEnabled: boolean
      /**
       * Select metadata about the document that were mutated
       * If it's prefixed with `drafts.` then it's a draft document, otherwise it's a published document.
       */
      document: {
        /**
         * If it's prefixed with `drafts.` then it's a draft document, otherwise it's a published document.
         */
        _id: string
        /**
         * The document type is frequently used in `revalidateTag` scenarios with Next.js App Router
         */
        _type: string
        /**
         * The document revision, can be used to dedupe requests, as we always send two due to debouncing and handling Content Lake eventual consistency
         */
        _rev: string
        /**
         * If the document has a top level slug field named `slug` with the type `slug`, then it'll be included here
         */
        slug?: {
          current?: string | null
        }
      }
    }

/**
 * Preview frame history update
 * @public
 */
export declare type HistoryUpdate = {
  type: 'push' | 'pop' | 'replace'
  title?: string
  url: string
}

export declare function isEmptyActor(actor: MutatorActor | EmptyActor): actor is EmptyActor

export declare const listeners: Set<() => void>

export declare type MutatorActor = ActorRefFrom<ReturnType<typeof createDatasetMutator>>

export declare type OptimisticDocument<T extends Record<string, any> = Record<string, any>> = {
  /**
   * The document ID
   */
  id: string
  /**
   * Commits any locally applied mutations to the remote document
   */
  commit: () => void
  /**
   * @deprecated - use `getSnapshot` instead
   */
  get: {
    (): SanityDocument<T> | undefined
    <P extends Path<T, keyof T>>(path: P): PathValue<T, P> | undefined
  }
  /**
   * Returns a promise that resolves to the current document snapshot
   */
  getSnapshot: () => Promise<SanityDocument<T> | null>
  /**
   * Applies the given patches to the document
   */
  patch: (
    patches: OptimisticDocumentPatches<T>,
    options?: {
      commit?:
        | boolean
        | {
            debounce: number
          }
    },
  ) => void
}

export declare type OptimisticDocumentPatches<T extends Record<string, any> = Record<string, any>> =

    | ((context: {
        draftId: string
        publishedId: string
        /**
         * @deprecated - use `getSnapshot` instead
         */
        snapshot: SanityDocument<T> | undefined
        getSnapshot: () => Promise<SanityDocument<T> | null>
      }) => Promise<NodePatchList> | NodePatchList)
    | NodePatchList

export declare type OptimisticReducer<T, U> = (state: T, action: OptimisticReducerAction<U>) => T

export declare type OptimisticReducerAction<T> = {
  document: T
  id: string
  originalId: string
  type: 'appear' | 'mutate' | 'disappear'
}

export declare type Path<T, K extends keyof T> = K extends string
  ? T[K] extends Record<string, any>
    ? `${K}.${Path<T[K], keyof T[K]>}` | K
    : K
  : never

export declare type PathValue<T, P extends string> = P extends `${infer K}.${infer Rest}`
  ? K extends keyof T
    ? PathValue<T[K], Rest>
    : never
  : P extends keyof T
    ? T[P]
    : never

/**
 * @public
 */
export declare type PreviewSnapshot = {
  [K in keyof Omit<PreviewValue, 'media'>]?: Omit<PreviewValue, 'media'>[K]
} & {
  _id: string
}

export declare type ResolvedSchemaTypeMap = Map<string, Map<string, StudioPathLike>>

/**
 * Data resolved from a Sanity node
 * @public
 */
export declare type SanityNode = {
  baseUrl: string
  dataset?: string
  id: string
  isDraft?: string
  path: string
  projectId?: string
  tool?: string
  type?: string
  workspace?: string
}

/**
 * Data resolved from a Sanity Stega node
 * @public
 */
export declare type SanityStegaNode = {
  origin: string
  href: string
  data?: unknown
}

export declare interface SchemaArrayItem<T extends SchemaNode = SchemaNode> {
  type: 'arrayItem'
  name: string
  title?: string
  value: T
}

export declare interface SchemaArrayNode<T extends SchemaNode = SchemaNode> {
  type: 'array'
  of: SchemaArrayItem<T>
}

export declare interface SchemaBooleanNode {
  type: 'boolean'
  value?: boolean
}

export declare interface SchemaInlineNode {
  type: 'inline'
  /** the name of the referenced type */
  name: string
}

export declare type SchemaNode =
  | SchemaArrayNode
  | SchemaBooleanNode
  | SchemaInlineNode
  | SchemaNullNode
  | SchemaNumberNode
  | SchemaObjectNode
  | SchemaStringNode
  | SchemaUnionNode
  | SchemaUnknownNode

export declare interface SchemaNullNode {
  type: 'null'
}

export declare interface SchemaNumberNode {
  type: 'number'
  value?: number
}

export declare interface SchemaObjectField<T extends SchemaNode = SchemaNode> {
  type: 'objectField'
  name: string
  title?: string
  value: T
  optional?: boolean
}

export declare interface SchemaObjectNode<T extends SchemaNode = SchemaNode> {
  type: 'object'
  fields: Record<string, SchemaObjectField<T>>
  rest?: SchemaObjectNode | SchemaUnknownNode | SchemaInlineNode
  dereferencesTo?: string
}

export declare interface SchemaStringNode {
  type: 'string'
  value?: string
}

export declare type SchemaType = DocumentSchema | TypeSchema

export declare interface SchemaUnionNode<T extends SchemaNode = SchemaNode> {
  type: 'union'
  of: SchemaUnionOption<T>[] | SchemaStringNode[] | SchemaNumberNode[]
  options?: SchemaUnionNodeOptions
}

export declare type SchemaUnionNodeOptions = Omit<ArrayOptions, 'insertMenu'> & {
  insertMenu?: Omit<InsertMenuOptions, 'views'> & {
    views?: Array<
      | {
          name: 'list'
        }
      | {
          name: 'grid'
          previewImageUrls?: Record<string, string | undefined>
        }
    >
  }
}

export declare interface SchemaUnionOption<T extends SchemaNode = SchemaNode> {
  type: 'unionOption'
  name: string
  title?: string
  icon?: string
  value: T
}

export declare interface SchemaUnknownNode {
  type: 'unknown'
}

/**
 * @public
 */
export declare type Serializable = SerializablePrimitive | SerializableObject | SerializableArray

/**
 * @public
 */
export declare type SerializableArray = Serializable[]

/**
 * @public
 */
export declare type SerializableObject = {
  [key: string]: Serializable
}

/**
 * @public
 */
export declare type SerializablePrimitive = string | number | boolean | null | undefined

export declare function setActor(nextActor: MutatorActor): void

export declare interface TypeSchema {
  type: 'type'
  name: string
  title?: string
  value: SchemaNode
}

export declare interface UnresolvedPath {
  id: string
  path: string
}

/**
 * @public
 */
export declare type VisualEditingControllerMsg =
  | {
      type: 'presentation/focus'
      data: {
        id: string
        path: string
      }
    }
  | {
      type: 'presentation/blur'
      data: undefined
    }
  | {
      type: 'presentation/navigate'
      data: HistoryUpdate
    }
  | {
      type: 'presentation/toggle-overlay'
      data: undefined
    }
  | {
      type: 'presentation/refresh'
      data: HistoryRefresh
    }
  | {
      type: 'presentation/perspective'
      data: {
        perspective: ClientPerspective
      }
    }
  /**
   * @deprecated switch to explict schema fetching (using
   * 'visual-editing/schema') at next major
   */
  | {
      type: 'presentation/schema'
      data: {
        schema: SchemaType[]
      }
    }
  | {
      type: 'presentation/preview-snapshots'
      data: {
        snapshots: PreviewSnapshot[]
      }
    }
  | {
      type: 'presentation/snapshot-event'
      data: {
        event: ReconnectEvent | WelcomeEvent | MutationEvent_2
      }
    }
  | {
      type: 'presentation/shared-state'
      data: {
        key: string
        value?: Serializable
      }
    }

/**
 * @internal
 */
export declare type VisualEditingNode = Node_2<VisualEditingNodeMsg, VisualEditingControllerMsg>

/**
 * @public
 */
export declare type VisualEditingNodeMsg =
  | {
      type: 'visual-editing/focus'
      data: SanityNode | SanityStegaNode
    }
  | {
      type: 'overlay/focus'
      data: SanityNode | SanityStegaNode
    }
  | {
      type: 'visual-editing/navigate'
      data: HistoryUpdate
    }
  | {
      type: 'overlay/navigate'
      data: HistoryUpdate
    }
  | {
      type: 'visual-editing/toggle'
      data: {
        enabled: boolean
      }
    }
  | {
      type: 'overlay/toggle'
      data: {
        enabled: boolean
      }
    }
  | {
      type: 'visual-editing/meta'
      data: {
        title: string
      }
    }
  | {
      type: 'visual-editing/documents'
      data: {
        projectId?: string
        dataset?: string
        perspective: ClientPerspective
        documents: ContentSourceMapDocuments
      }
    }
  | {
      type: 'visual-editing/refreshing'
      data: HistoryRefresh
    }
  | {
      type: 'visual-editing/refreshed'
      data: HistoryRefresh
    }
  | {
      type: 'visual-editing/schema'
      data: undefined
      response: {
        schema: SchemaType[]
      }
    }
  | {
      type: 'visual-editing/schema-union-types'
      data: {
        paths: UnresolvedPath[]
      }
      response: {
        types: ResolvedSchemaTypeMap
      }
    }
  | {
      type: 'visual-editing/observe-documents'
      data: {
        documentIds: string[]
      }
    }
  | {
      type: 'visual-editing/fetch-snapshot'
      data: {
        documentId: string
      }
      response: {
        snapshot: SanityDocument<Record<string, any>> | undefined | void
      }
    }
  | {
      type: 'visual-editing/mutate'
      data: {
        transactionId: string | undefined
        mutations: any[]
      }
      response: any
    }
  | {
      type: 'visual-editing/snapshot-welcome'
      data: undefined
      response: {
        event: WelcomeEvent
      }
    }
  | {
      type: 'visual-editing/fetch-perspective'
      data: undefined
      response: {
        perspective: ClientPerspective
      }
    }
  | {
      type: 'visual-editing/features'
      data: undefined
      response: {
        features: Record<string, boolean>
      }
    }
  | {
      type: 'visual-editing/shared-state'
      data: undefined
      response: {
        state: SerializableObject
      }
    }
  | {
      type: 'visual-editing/telemetry-log'
      data: {
        event: any
        data: any
      }
    }

export {}
