import { t as SanityInstance } from "./createSanityInstance--Lrldto8.js";
import { ClientPerspective } from "@sanity/client";
import { Observable, Subscription } from "rxjs";
import "zustand/middleware";
/**
 * Represents a reactive store state container with multiple access patterns
 */
interface StoreState<TState> {
  /**
   * Gets the current state value
   *
   * @remarks
   * This is a direct synchronous accessor that doesn't trigger subscriptions
   */
  get: () => TState;
  /**
   * Updates the store state
   * @param name - Action name for devtools tracking
   * @param updatedState - New state value or updater function
   *
   * @remarks
   * When providing a partial object, previous top-level keys not included in
   * the update will be preserved.
   */
  set: (name: string, updatedState: Partial<TState> | ((s: TState) => Partial<TState>)) => void;
  /**
   * Observable stream of state changes
   * @remarks
   * - Emits immediately with current state on subscription
   * - Shares underlying subscription between observers
   * - Only emits when state reference changes
   * - Completes when store is disposed
   */
  observable: Observable<TState>;
}
/**
 * Context object provided to store initialization functions
 */
interface StoreContext<TState, TKey = unknown> {
  /**
   * Sanity instance associated with this store
   *
   * @remarks
   * Provides access to the Sanity configuration and instance lifecycle methods
   */
  instance: SanityInstance;
  /**
   * Reactive store state management utilities
   *
   * @remarks
   * Contains methods for getting/setting state and observing changes
   */
  state: StoreState<TState>;
  /**
   * The key used to instantiate the store.
   */
  key: TKey;
  /**
   * The store's upstream subscription. Subscribing keeps it open. Provided to
   * actions bound through an action binder.
   */
  upstream$?: Observable<never>;
  /**
   * Why the upstream last failed to open, if it did. Provided to actions bound
   * through an action binder.
   */
  upstreamError?: () => {
    error: unknown;
  } | undefined;
}
/**
 * Defines a store action that operates on a specific state type
 */
type StoreAction<TState, TParams extends unknown[], TReturn, TKey = unknown> = (context: StoreContext<TState, TKey>, ...params: TParams) => TReturn;
/**
 * Represents a store action that has been bound to a specific store instance
 */
type BoundStoreAction<_TState, TParams extends unknown[], TReturn> = (instance: SanityInstance, ...params: TParams) => TReturn;
/**
 * Represents a reactive state source that provides synchronized access to store data
 *
 * @remarks
 * Designed to work with React's useSyncExternalStore hook. Provides three ways to access data:
 * 1. `getCurrent()` for synchronous current value access
 * 2. `subscribe()` for imperative change notifications
 * 3. `observable` for reactive stream access
 *
 * @public
 */
interface StateSource<T> {
  /**
   * Subscribes to state changes with optional callback
   * @param onStoreChanged - Called whenever relevant state changes occur
   * @returns Unsubscribe function to clean up the subscription
   */
  subscribe: (onStoreChanged?: () => void) => () => void;
  /**
   * Gets the current derived state value
   *
   * @remarks
   * Safe to call without subscription. Will always return the latest value
   * based on the current store state and selector parameters.
   */
  getCurrent: () => T;
  /**
   * Observable stream of state values
   *
   * @remarks
   * Shares a single underlying subscription between all observers. Emits:
   * - Immediately with current value on subscription
   * - On every relevant state change
   * - Errors if selector throws
   */
  observable: Observable<T>;
}
/**
 * Context passed to selectors when deriving state
 *
 * @remarks
 * Provides access to both the current state value and the Sanity instance,
 * allowing selectors to use configuration values when computing derived state.
 * The context is memoized for each state object and instance combination
 * to optimize performance and prevent unnecessary recalculations.
 *
 * @example
 * ```ts
 * // Using both state and instance in a selector (psuedo example)
 * const getUserByProjectId = createStateSourceAction(
 *   ({ state, instance }: SelectorContext<UsersState>, options?: ProjectHandle) => {
 *     const allUsers = state.users
 *     const projectId = options?.projectId ?? instance.config.projectId
 *     return allUsers.filter(user => user.projectId === projectId)
 *   }
 * )
 * ```
 */
interface SelectorContext<TState> {
  /**
   * The current state object from the store
   */
  state: TState;
  /**
   * The Sanity instance associated with this state
   */
  instance: SanityInstance;
}
/**
 * Function type for selecting derived state from store state and parameters
 * @public
 */
type Selector<TState, TParams extends unknown[], TReturn> = (context: SelectorContext<TState>, ...params: TParams) => TReturn;
export { StoreAction as i, StateSource as n, BoundStoreAction as r, Selector as t };
//# sourceMappingURL=createStateSourceAction-CICvr0ZE.d.ts.map