import type { Options as MutativeOptions, Patches, Draft, Immutable, PatchesOptions, } from 'mutative'; export type TravelPatches

= { patches: Patches

[]; inversePatches: Patches

[]; }; export type TravelMetadata = { label?: string; timestamp?: number; source?: string; [key: string]: unknown; }; export type TravelHistoryEntry

= { patches: Patches

; inversePatches: Patches

; metadata?: TravelMetadata; }; export type PatchesOption = Exclude; export type JsonPrimitive = string | number | boolean | null; export type JsonObject = { [key: string]: JsonValue }; export type JsonArray = JsonValue[]; export type JsonValue = JsonPrimitive | JsonObject | JsonArray; export type PatchableState = | JsonValue | Map | Set; export type TravelsHistory

= { patches: TravelPatches

; position: number; metadata?: Array; }; export type TravelsSerializedHistory< S, P extends PatchesOption = {}, > = TravelsHistory

& { version: 1; state: S; }; export type TravelsPersistenceErrorCode = | 'PARSE_ERROR' | 'INVALID_SCHEMA' | 'UNSUPPORTED_VERSION' | 'INVALID_PATCHES' | 'MIGRATION_FAILED'; export type TravelsMigration< S, P extends PatchesOption = {}, > = (snapshot: unknown) => TravelsSerializedHistory | unknown; export type TravelsDeserializeOptions< S, P extends PatchesOption = {}, > = { /** * Migrate old persisted snapshots into the current schema before validation. */ migrate?: TravelsMigration; /** * Fallback snapshot used when parsing, migration, or validation fails. */ fallback?: | TravelsSerializedHistory | (() => TravelsSerializedHistory); /** * Receive typed persistence errors before a fallback is returned or the error is thrown. */ onError?: (error: Error) => void; }; export type TravelsBranchDiscardEvent

= { position: number; discarded: TravelHistoryEntry

[]; }; export type TravelsDevtoolsEvent< S, P extends PatchesOption = {}, > = { type: | 'setState' | 'archive' | 'transaction' | 'go' | 'reset' | 'rebase' | 'replaceStateWithoutHistory'; state: S; position: number; patches: TravelPatches

; metadata?: TravelMetadata; }; export type TravelsErrorCode = 'TRANSACTION_FAILED'; export type TravelsOptions< F extends boolean, A extends boolean, P extends PatchesOption = {}, > = { /** * The maximum number of history to keep, by default `10` */ maxHistory?: number; /** * The initial position in the history, by default `0` */ initialPosition?: number; /** * The initial patches of the history */ initialPatches?: TravelPatches

; /** * Restore validated serialized history. This is equivalent to passing * initialPatches and initialPosition directly, but preserves the first-class * persistence API shape returned by `Travels.deserialize(...)`. */ history?: TravelsHistory

; /** * Whether to throw when `initialPatches` is invalid. * When false (default), invalid patches are discarded and history starts empty. */ strictInitialPatches?: boolean; /** * Whether to automatically archive the current state, by default `true` */ autoArchive?: A; /** * Whether to mutate the state in place (for observable state like MobX, Vue, Pinia) * When true, apply patches directly to the existing state object * When false (default), create new immutable state objects * @default false */ mutable?: boolean; /** * Whether to warn in development when state contains values with weak patch or * JSON persistence semantics, by default `true` in development. */ warnOnUnsupportedState?: boolean; /** * Called when Travels wraps a thrown core operation error. */ onError?: (error: Error) => void; /** * Called when undoing and then making a new edit discards redo history. */ onBranchDiscard?: (event: TravelsBranchDiscardEvent

) => void; /** * Optional hook for external devtools or debugging timelines. */ devtools?: (event: TravelsDevtoolsEvent) => void; } & Omit, 'enablePatches'> & { patchesOptions?: P; }; export type InitialValue = I extends ( ...args: unknown[] ) => infer R ? R : I; type DraftFunction = (draft: Draft) => void; export type Updater = S | (() => S) | DraftFunction; export type Value = F extends true ? Immutable> : InitialValue; export interface TravelsControls< S, F extends boolean, P extends PatchesOption = {}, > { /** * The current position in the history */ position: number; /** * Get the history of the state */ getHistory: () => Value[]; /** * The patches of the history */ patches: TravelPatches

; /** * Go back in the history */ back: (amount?: number) => void; /** * Go forward in the history */ forward: (amount?: number) => void; /** * Reset the history */ reset: () => void; /** * Go to a specific position in the history */ go: (position: number) => void; /** * Check if it's possible to go back */ canBack: () => boolean; /** * Check if it's possible to go forward */ canForward: () => boolean; } export type RebasableTravelsControls< S, F extends boolean, P extends PatchesOption = {}, > = TravelsControls & { /** * Remove all history and make the current state as the new initial state. * * @remarks * **IMPORTANT**: This is a destructive operation. All previous and future history entries are discarded, * and the current state (including any unarchived temp patches) becomes the new baseline (position 0). Any subsequent `reset()` * calls will return to this new baseline, not the original initial state. */ rebase: () => void; }; export interface ManualTravelsControls< S, F extends boolean, P extends PatchesOption = {}, > extends TravelsControls { /** * Archive the current state */ archive: (metadata?: TravelMetadata) => void; /** * Check if it's possible to archive the current state */ canArchive: () => boolean; } export type RebasableManualTravelsControls< S, F extends boolean, P extends PatchesOption = {}, > = ManualTravelsControls & { /** * Remove all history and make the current state as the new initial state. * * @remarks * **IMPORTANT**: This is a destructive operation. All previous and future history entries are discarded, * and the current state (including any unarchived temp patches) becomes the new baseline (position 0). Any subsequent `reset()` * calls will return to this new baseline, not the original initial state. */ rebase: () => void; };