import { Dr as StateTopic, Er as ReplyOf, Mr as ValueOf, _r as EventTopic, jr as Topics, kr as TopicName, t as Application, wr as PayloadOf, xr as MessageBusTopics } from "./applications-DowvM7CN.js";
import { Observable } from "rxjs";
/**
 * A message bus protocol error code.
 * @public
 * @category Message Bus
 */
type MessageBusErrorCode = 'NO_RESPONDER' | 'TIMEOUT' | 'ABORTED' | 'HANDLER_THREW' | 'PROTOCOL_MISMATCH' | 'OWNERSHIP_MISMATCH' | 'MISSING_APP_ID' | 'REFUSED';
/**
 * An error raised by the message bus protocol.
 * @public
 * @category Message Bus
 */
declare class MessageBusError extends Error {
  /** The machine-readable protocol error code. */
  readonly code: MessageBusErrorCode;
  /** Creates a message bus protocol error. */
  constructor(code: MessageBusErrorCode, message?: string, options?: {
    cause?: unknown;
  });
}
/**
 * Metadata that travels with each message bus event.
 * @public
 * @category Message Bus
 */
interface MessageBusMeta {
  /** The application that produced the message. */
  appId: Application['id'];
  /**
   * The federation module id of the connection that sent the message (e.g.
   * `favorites/views/list/panel`). Absent from older copies and when the host
   * supplied none, so readers normalise with `message.meta.moduleId ?? message.meta.appId`.
   */
  moduleId?: string;
  /** The Unix timestamp in milliseconds when the message was produced. */
  timestamp: number;
}
/**
 * A message delivered to an event topic responder.
 * @public
 * @category Message Bus
 */
interface MessageBusMessage<T, R = never, K extends PropertyKey = TopicName> {
  /** The topic that carries the message. */
  type: K;
  /** The event payload. */
  payload: T;
  /** The message provenance. */
  meta: MessageBusMeta;
  /** Replies to an awaiting sender. */
  reply(value: R): void;
  /**
   * Refuses the message: the sender's awaited `emit()` rejects with a
   * {@link MessageBusError} whose `code` is `'REFUSED'`. Ignored, with a warning,
   * once the reply has already settled.
   */
  reject(message?: string): void;
  /** Aborts when the sender stops waiting for a reply. */
  readonly signal: AbortSignal;
}
/**
 * An observable state topic with access to its current and first values.
 * @public
 * @category Message Bus
 */
interface MessageBusStateSource<T> extends Observable<T> {
  /** Returns the current value, or `undefined` before the first value is published. */
  getCurrent(): T | undefined;
  /** Resolves with the first published value. */
  readonly firstValue: Promise<T>;
}
/**
 * Options for aborting a message bus operation.
 * @public
 * @category Message Bus
 */
interface MessageBusAbortOptions {
  /** Aborts the operation when signaled. */
  signal?: AbortSignal;
}
/**
 * Options for reading a state topic.
 * @public
 * @category Message Bus
 */
interface MessageBusQueryOptions extends MessageBusAbortOptions {
  /** Wait timeout in milliseconds. Defaults to 5 seconds; `null` disables it. */
  timeout?: number | null;
}
/**
 * Options for emitting an event topic.
 * @public
 * @category Message Bus
 */
interface MessageBusEmitOptions extends MessageBusAbortOptions {
  /** Reply timeout in milliseconds. Defaults to 5 seconds; `null` disables it. */
  timeout?: number | null;
}
/**
 * A lazily awaited event reply.
 * @public
 * @category Message Bus
 */
interface MessageBusEmitResult<R> extends PromiseLike<R> {
  /** Handles a rejected event reply. */
  catch<T = never>(onRejected?: (reason: unknown) => T | PromiseLike<T>): Promise<R | T>;
  /** Runs after the event reply settles. */
  finally(onFinally?: () => void): Promise<R>;
}
/**
 * Sends events and reads typed state and event topics.
 *
 * @remarks
 * State topics are written by the host to each connection separately through
 * {@link MessageBusClient.emit}; a connection only ever reads its own values.
 * @public
 * @category Message Bus
 */
interface MessageBus<TTopics = MessageBusTopics> {
  /** Emits an event topic and provides its reply when awaited. */
  emit<K extends EventTopic<TTopics>>(type: K, ...rest: PayloadOf<K, TTopics> extends void ? [payload?: void, options?: MessageBusEmitOptions] : [payload: PayloadOf<K, TTopics>, options?: MessageBusEmitOptions]): MessageBusEmitResult<ReplyOf<K, TTopics>>;
  /** Reads the current or next value of a state topic. */
  query<K extends StateTopic<TTopics>>(type: K, options?: MessageBusQueryOptions): Promise<ValueOf<K, TTopics>>;
  /** Runs a responder for each event until its signal aborts. */
  subscribe<K extends EventTopic<TTopics>>(type: K, handler: (message: MessageBusMessage<PayloadOf<K, TTopics>, ReplyOf<K, TTopics>, K>) => void, options?: MessageBusAbortOptions): void;
  /** Runs a handler for each state value until its signal aborts. */
  subscribe<K extends StateTopic<TTopics>>(type: K, handler: (value: ValueOf<K, TTopics>) => void, options?: MessageBusAbortOptions): void;
  /** Returns a state topic as a `MessageBusStateSource`. */
  subscribe<K extends StateTopic<TTopics>>(type: K): MessageBusStateSource<ValueOf<K, TTopics>>;
  /** Returns an event topic as an observable of its payloads. */
  subscribe<K extends EventTopic<TTopics>>(type: K): Observable<PayloadOf<K, TTopics>>;
}
/**
 * A message bus connection that can be torn down independently of its siblings.
 * @public
 * @category Message Bus
 */
interface MessageBusConnection<TTopics = MessageBusTopics> extends MessageBus<TTopics> {
  /** The application this connection belongs to. */
  readonly appId: Application['id'];
  /**
   * Tears down this connection: pending requests reject `ABORTED`, subscriptions
   * complete, and later `emit`, `query`, and `subscribe` calls fail with `ABORTED`.
   * Siblings and the shared bus are untouched.
   */
  disconnect(): void;
}
/**
 * The host's handle to one connection: who it is, when it closes, and where to write its state.
 * @public
 * @category Message Bus
 */
interface MessageBusClient {
  /** The application the connection belongs to. */
  readonly appId: Application['id'];
  /** The federation module id of the connection. Equals `appId` when the host supplied none. */
  readonly moduleId: string;
  /** Aborts when the connection disconnects. Drop per-client state here. */
  readonly closed: AbortSignal;
  /** Writes a state value to this connection only. Ignored once the connection has closed. */
  emit<K extends StateTopic>(type: K, value: ValueOf<K>): void;
  /**
   * Rejects a state topic for this connection: its pending and later {@link MessageBus.query}
   * calls reject with a {@link MessageBusError} whose `code` is `'REFUSED'`, until the host next
   * writes a value with {@link MessageBusClient.emit}, which clears the rejection.
   *
   * @remarks
   * A rejected promise cannot be un-rejected and an errored subject cannot resume, so the
   * rejection is tracked per connection alongside the topic's subject rather than by erroring
   * it: `query` consults it before awaiting, and {@link MessageBus.subscribe} and the topic's
   * {@link MessageBusStateSource} stay silent (they never error) until a value is written.
   * Ignored once the connection has closed, the same as {@link MessageBusClient.emit}.
   */
  reject<K extends StateTopic>(type: K, message?: string): void;
}
/**
 * The host's connection to the bus.
 *
 * @remarks
 * State reaches an application only through {@link MessageBusClient.emit}. There is no
 * broadcast: sending the same value to everyone is a loop over `connections`, and giving a
 * new connection the current values is done when it appears there, as an SSE server writes
 * a snapshot to each new client.
 * @public
 * @category Message Bus
 */
interface MessageBusHost<TTopics = MessageBusTopics> extends MessageBusConnection<TTopics> {
  /**
   * Every open connection other than this one first, then each new one as it joins. The same
   * {@link MessageBusClient} object is handed out for a connection across subscriptions, so it
   * can be kept in a `Set`. A connection stays listed until it calls `disconnect()`; the SDK
   * does that when the owning `SanityInstance` is disposed.
   */
  readonly connections: Observable<MessageBusClient>;
}
/**
 * Returns whether this application runs on an installed message bus.
 * @internal
 */
declare function isMessageBusEnvironment(): boolean;
/**
 * Options for connecting to an installed message bus.
 * @public
 * @category Message Bus
 */
interface ConnectMessageBusOptions {
  /** The application ID. Defaults to the ID embedded by the Sanity CLI. */
  appId?: string;
  /** The federation module id stamped on emitted messages. Defaults to `appId`. */
  moduleId?: string;
}
/**
 * Connects to the installed message bus, or returns `undefined` when no compatible connection exists.
 * @public
 * @category Message Bus
 */
declare function connectMessageBus(options?: ConnectMessageBusOptions): MessageBusConnection | undefined;
/**
 * Resets the installed message bus for test isolation.
 * @internal
 */
declare function resetMessageBus(): void;
/**
 * Options for installing the shared message bus. The host is the whole app, so it carries
 * no `moduleId`.
 * @internal
 */
interface InstallMessageBusOptions {
  /** The application ID. Defaults to the ID embedded by the Sanity CLI. */
  appId?: string;
}
/**
 * Installs the shared message bus and returns the host's connection, or connects to its
 * existing installation. Only the host calls this; another app's ID is rejected because the
 * connection it would get has no `connections`.
 * @internal
 */
declare function installMessageBus(options?: InstallMessageBusOptions): MessageBusHost<Topics>;
export { installMessageBus as _, MessageBusClient as a, MessageBusEmitResult as c, MessageBusHost as d, MessageBusMessage as f, connectMessageBus as g, MessageBusStateSource as h, MessageBusAbortOptions as i, MessageBusError as l, MessageBusQueryOptions as m, InstallMessageBusOptions as n, MessageBusConnection as o, MessageBusMeta as p, MessageBus as r, MessageBusEmitOptions as s, ConnectMessageBusOptions as t, MessageBusErrorCode as u, isMessageBusEnvironment as v, resetMessageBus as y };
//# sourceMappingURL=bus-ClLO0kQU.d.ts.map