import { StandardSchemaV1 } from "@standard-schema/spec";
//#region src/index.d.ts
/** Type-discriminated job envelope — dispatch keys on `type`. */
type JobEnvelope<T = unknown> = {
  type: string;
  payload: T;
};
/**
 * Structurally matches `@cloudflare/workers-types` `Message` — declared locally so
 * core needs no CF types at the `.` entry.
 */
type QueueMessage<T = unknown> = {
  readonly id: string;
  readonly timestamp: Date;
  readonly body: T;
  readonly attempts: number;
  ack(): void;
  retry(options?: {
    delaySeconds?: number;
  }): void;
};
/**
 * Structurally matches `@cloudflare/workers-types` `MessageBatch` — declared locally.
 */
type QueueBatch<T = unknown> = {
  readonly queue: string;
  readonly messages: readonly QueueMessage<T>[];
  retryAll(options?: {
    delaySeconds?: number;
  }): void;
  ackAll(): void;
};
/** Marker for domain-terminal failures — ack after writing FAILED state, never retry. */
declare class TerminalJobError extends Error {
  readonly name = "TerminalJobError";
}
/** Injected idempotency strategy — typical impls use KV, table, unique-index, processedAt, status-claim. */
interface IdempotencyStore {
  seen(key: string): Promise<boolean>;
  mark(key: string, ttl?: number): Promise<void>;
}
type DispatchOpts<E> = {
  /** Backstop path: missing-secret / unknown-type → throw. Live path: log + ack. */
  strict?: boolean;
  idempotency?: {
    key: string;
    store: IdempotencyStore;
  };
  /** Fired before ack on `TerminalJobError` — write FAILED state here. */
  onTerminalFailure?: (err: TerminalJobError, env: E, msg: JobEnvelope) => void | Promise<void>;
};
type JobRegistry<E> = {
  register<T>(type: string, schema: StandardSchemaV1<unknown, T>, handle: (env: E, payload: T, msg: JobEnvelope<T>) => Promise<void>): void;
  dispatch(env: E, msg: JobEnvelope, opts?: DispatchOpts<E>): Promise<void>;
};
declare function createJobRegistry<E>(): JobRegistry<E>;
//#endregion
export { DispatchOpts, IdempotencyStore, JobEnvelope, JobRegistry, QueueBatch, QueueMessage, TerminalJobError, createJobRegistry };