import * as Cause from "../../Cause.ts";
import * as Context from "../../Context.ts";
import * as Effect from "../../Effect.ts";
import * as Exit from "../../Exit.ts";
import * as Layer from "../../Layer.ts";
import * as Option from "../../Option.ts";
import type * as Schedule from "../../Schedule.ts";
import * as Schema from "../../Schema.ts";
import * as Scope from "../../Scope.ts";
import type { ExitEncoded } from "../rpc/RpcMessage.ts";
import type { WorkflowEngine, WorkflowInstance } from "./WorkflowEngine.ts";
declare const TypeId = "~effect/workflow/Workflow";
/**
 * Durable workflow definition with typed payload, success, and error schemas
 * plus operations for execution, polling, interruption, resumption, and
 * registration.
 *
 * @category models
 * @since 4.0.0
 */
export interface Workflow<Name extends string, Payload extends AnyStructSchema, Success extends Schema.Top, Error extends Schema.Top> {
    readonly [TypeId]: typeof TypeId;
    readonly name: Name;
    readonly payloadSchema: Payload;
    readonly successSchema: Success;
    readonly errorSchema: Error;
    readonly annotations: Context.Context<never>;
    /**
     * Add an annotation to the workflow.
     */
    annotate<I, S>(key: Context.Key<I, S>, value: S): Workflow<Name, Payload, Success, Error>;
    /**
     * Merge multiple annotations into the workflow.
     */
    annotateMerge<I>(annotations: Context.Context<I>): Workflow<Name, Payload, Success, Error>;
    /**
     * Execute the workflow with the given payload.
     */
    readonly execute: <const Discard extends boolean = false>(payload: Payload["~type.make.in"], options?: {
        readonly discard?: Discard;
    }) => Effect.Effect<Discard extends true ? string : Success["Type"], Discard extends true ? never : Error["Type"], WorkflowEngine | Payload["EncodingServices"] | Success["DecodingServices"] | Error["DecodingServices"]>;
    /**
     * Execute the workflow with the given payload.
     */
    readonly poll: (executionId: string) => Effect.Effect<Option.Option<Result<Success["Type"], Error["Type"]>>, never, WorkflowEngine | Success["DecodingServices"] | Error["DecodingServices"]>;
    /**
     * Interrupt a workflow execution for the given execution ID.
     */
    readonly interrupt: (executionId: string) => Effect.Effect<void, never, WorkflowEngine>;
    /**
     * Manually resume a workflow execution for the given execution ID.
     */
    readonly resume: (executionId: string) => Effect.Effect<void, never, WorkflowEngine>;
    /**
     * Create a layer that registers the workflow and provides an effect to
     * execute it.
     */
    readonly toLayer: <R>(execute: (payload: Payload["Type"], executionId: string) => Effect.Effect<Success["Type"], Error["Type"], R>) => Layer.Layer<never, never, WorkflowEngine | Exclude<R, WorkflowEngine | WorkflowInstance | Execution<Name> | Scope.Scope> | Payload["DecodingServices"] | Payload["EncodingServices"] | Success["DecodingServices"] | Success["EncodingServices"] | Error["DecodingServices"] | Error["EncodingServices"]>;
    /**
     * For the given payload, compute the deterministic execution ID.
     */
    readonly executionId: (payload: Payload["~type.make.in"]) => Effect.Effect<string>;
    /**
     * Add compensation logic to an effect inside a Workflow.
     *
     * **Details**
     *
     * The compensation finalizer is called if the entire workflow fails, allowing you to perform cleanup or other actions based on the success value and the cause of the workflow failure.
     *
     * **Gotchas**
     *
     * Compensation finalizers are only registered for top-level effects in the workflow and do not work for nested activities.
     */
    readonly withCompensation: {
        <A, R2>(compensation: (value: A, cause: Cause.Cause<Error["Type"]>) => Effect.Effect<void, never, R2>): <E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | R2 | WorkflowInstance | Execution<Name> | Scope.Scope>;
        <A, E, R, R2>(effect: Effect.Effect<A, E, R>, compensation: (value: A, cause: Cause.Cause<Error["Type"]>) => Effect.Effect<void, never, R2>): Effect.Effect<A, E, R | R2 | WorkflowInstance | Execution<Name> | Scope.Scope>;
    };
}
/**
 * Schema constraint for workflow payload schemas that expose struct fields.
 *
 * @category schemas
 * @since 4.0.0
 */
export interface AnyStructSchema extends Schema.Top {
    readonly fields: Schema.Struct.Fields;
}
/**
 * Type-level marker for services associated with a specific workflow
 * execution name.
 *
 * @category models
 * @since 4.0.0
 */
export interface Execution<Name extends string> {
    readonly _: unique symbol;
    readonly name: Name;
}
/**
 * Type-erased workflow shape for APIs that operate on workflows without
 * preserving their specific payload, success, or error types.
 *
 * @category models
 * @since 4.0.0
 */
export interface Any {
    readonly [TypeId]: typeof TypeId;
    readonly name: string;
    readonly executionId: (payload: any) => Effect.Effect<string>;
    readonly payloadSchema: AnyStructSchema;
    readonly successSchema: Schema.Top;
    readonly errorSchema: Schema.Top;
    readonly annotations: Context.Context<never>;
}
/**
 * Type-erased workflow shape that also exposes executable operations needed by
 * workflow proxy and engine helpers.
 *
 * @category models
 * @since 4.0.0
 */
export interface AnyWithProps extends Any {
    readonly payloadSchema: AnyStructSchema;
    readonly successSchema: Schema.Top;
    readonly errorSchema: Schema.Top;
    readonly execute: (payload: any, options?: {
        readonly discard?: boolean;
    }) => Effect.Effect<any, any, any>;
    readonly resume: (executionId: string) => Effect.Effect<void, never, WorkflowEngine>;
}
/**
 * Extracts the payload schema from a `Workflow`.
 *
 * @category models
 * @since 4.0.0
 */
export type PayloadSchema<W> = W extends Workflow<infer _Name, infer _Payload, infer _Success, infer _Error> ? _Payload : never;
/**
 * Computes the schema services required by clients that execute or poll
 * workflows.
 *
 * @category models
 * @since 4.0.0
 */
export type RequirementsClient<Workflows extends Any> = Workflows extends Workflow<infer _Name, infer _Payload, infer _Success, infer _Error> ? _Payload["EncodingServices"] | _Success["DecodingServices"] | _Error["DecodingServices"] : never;
/**
 * Computes the schema services required by handlers that decode workflow
 * payloads and encode workflow results.
 *
 * @category models
 * @since 4.0.0
 */
export type RequirementsHandler<Workflows extends Any> = Workflows extends Workflow<infer _Name, infer _Payload, infer _Success, infer _Error> ? _Payload["DecodingServices"] | _Payload["EncodingServices"] | _Success["DecodingServices"] | _Success["EncodingServices"] | _Error["DecodingServices"] | _Error["EncodingServices"] : never;
/**
 * Creates a durable workflow definition with schemas, annotations, and
 * deterministic execution IDs derived from the workflow name and idempotency
 * key.
 *
 * @category constructors
 * @since 4.0.0
 */
export declare const make: <const Name extends string, Payload extends Schema.Struct.Fields | AnyStructSchema, Success extends Schema.Top = Schema.Void, Error extends Schema.Top = Schema.Never>(options: {
    readonly name: Name;
    readonly payload: Payload;
    readonly idempotencyKey: (payload: Payload extends Schema.Struct.Fields ? Schema.Struct.Type<Payload> : Payload["Type"]) => string;
    readonly success?: Success;
    readonly error?: Error;
    readonly suspendedRetrySchedule?: Schedule.Schedule<any, unknown> | undefined;
    readonly annotations?: Context.Context<never>;
}) => Workflow<Name, Payload extends Schema.Struct.Fields ? Schema.Struct<Payload> : Payload, Success, Error>;
declare const ResultTypeId = "~effect/workflow/Workflow/Result";
/**
 * Returns `true` when a value is a workflow `Result`.
 *
 * @category results
 * @since 4.0.0
 */
export declare const isResult: <A = unknown, E = unknown>(u: unknown) => u is Result<A, E>;
/**
 * Result of a workflow execution, either a completed exit or a suspended
 * workflow state.
 *
 * @category results
 * @since 4.0.0
 */
export type Result<A, E> = Complete<A, E> | Suspended;
/**
 * Encoded representation of a workflow `Result`.
 *
 * @category results
 * @since 4.0.0
 */
export type ResultEncoded<A, E> = CompleteEncoded<A, E> | typeof Suspended.Encoded;
/**
 * Encoded representation of a completed workflow result containing an encoded
 * `Exit`.
 *
 * @category results
 * @since 4.0.0
 */
export interface CompleteEncoded<A, E> {
    readonly _tag: "Complete";
    readonly exit: ExitEncoded<A, E>;
}
/**
 * Schema constructor for `Complete` workflow results using the supplied
 * success and error schemas.
 *
 * @category schemas
 * @since 4.0.0
 */
export interface CompleteSchema<Success extends Schema.Top, Error extends Schema.Top> extends Schema.declareConstructor<Complete<Success["Type"], Error["Type"]>, Complete<Success["Encoded"], Error["Encoded"]>, readonly [Schema.Exit<Success, Error, Schema.Defect>]> {
    readonly success: Success;
    readonly error: Error;
}
declare const Complete_base: new <A_1 extends Record<string, any> = {}>(args: import("../../Types.ts").VoidIfEmpty<{ readonly [P in keyof A_1 as P extends "_tag" ? never : P]: A_1[P]; }>) => Readonly<A_1> & {
    readonly _tag: "Complete";
} & import("../../Pipeable.ts").Pipeable;
/**
 * Represents a completed workflow execution with its success or failure `Exit`.
 *
 * @category results
 * @since 4.0.0
 */
export declare class Complete<A, E> extends Complete_base<{
    readonly exit: Exit.Exit<A, E>;
}> {
    /**
     * Marks this value as a workflow result for runtime guards.
     *
     * @since 4.0.0
     */
    readonly [ResultTypeId] = "~effect/workflow/Workflow/Result";
    /**
     * Builds the schema for completed workflow results from success and error schemas.
     *
     * @since 4.0.0
     */
    static Schema<Success extends Schema.Top, Error extends Schema.Top>(options: {
        readonly success: Success;
        readonly error: Error;
    }): CompleteSchema<Success, Error>;
}
declare const Suspended_base: Schema.Class<Suspended, Schema.Struct<{
    readonly _tag: Schema.tag<"Suspended">;
    readonly cause: Schema.optional<Schema.Cause<Schema.Never, Schema.Defect>>;
}>, {}>;
/**
 * Represents a suspended workflow execution, optionally carrying the cause that
 * triggered suspension.
 *
 * @category results
 * @since 4.0.0
 */
export declare class Suspended extends Suspended_base {
    /**
     * Marks this value as a workflow result for runtime guards.
     *
     * @since 4.0.0
     */
    readonly [ResultTypeId] = "~effect/workflow/Workflow/Result";
}
/**
 * Creates a schema for workflow results using the supplied success and error
 * schemas.
 *
 * @category results
 * @since 4.0.0
 */
export declare const Result: <Success extends Schema.Top, Error extends Schema.Top>(options: {
    readonly success: Success;
    readonly error: Error;
}) => Schema.Union<readonly [CompleteSchema<Success, Error>, typeof Suspended]>;
/**
 * Schema for encoded workflow results with generic success and error payloads.
 *
 * @category results
 * @since 4.0.0
 */
export declare const ResultEncoded: Schema.Codec<ResultEncoded<any, any>>;
/**
 * Runs an effect as a workflow execution and converts its outcome into a
 * `Result`, handling suspension, defect capture, interruption, and workflow
 * scope finalization.
 *
 * @category results
 * @since 4.0.0
 */
export declare const intoResult: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<Result<A, E>, never, Exclude<R, Scope.Scope> | WorkflowInstance>;
/**
 * Wraps an activity-like effect so workflow suspension waits for currently
 * running activities to finish or suspend.
 *
 * @category results
 * @since 4.0.0
 */
export declare const wrapActivityResult: <A, E, R>(effect: Effect.Effect<A, E, R>, isSuspend: (value: A) => boolean) => Effect.Effect<A, E, R | WorkflowInstance>;
/**
 * Accesses the workflow scope, which is only closed when the workflow execution fully completes.
 *
 * @category resource management
 * @since 4.0.0
 */
export declare const scope: Effect.Effect<Scope.Scope, never, WorkflowInstance>;
/**
 * Provides the workflow scope to the given effect, and closes the scope only when the workflow execution fully completes.
 *
 * @category resource management
 * @since 4.0.0
 */
export declare const provideScope: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Scope.Scope> | WorkflowInstance>;
/**
 * Adds an exit finalizer to the current workflow scope, preserving the
 * services available when the finalizer is registered.
 *
 * @category resource management
 * @since 4.0.0
 */
export declare const addFinalizer: <R>(f: (exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void, never, R>) => Effect.Effect<void, never, WorkflowInstance | R>;
/**
 * Adds compensation logic to an effect inside a Workflow.
 *
 * **Details**
 *
 * The compensation finalizer is called if the entire workflow fails, allowing you to perform cleanup or other actions based on the success value and the cause of the workflow failure.
 *
 * **Gotchas**
 *
 * Compensation finalizers are only registered for top-level effects in the workflow and do not work for nested activities.
 *
 * @category Compensation
 * @since 4.0.0
 */
export declare const withCompensation: {
    /**
     * Adds compensation logic to an effect inside a Workflow.
     *
     * **Details**
     *
     * The compensation finalizer is called if the entire workflow fails, allowing you to perform cleanup or other actions based on the success value and the cause of the workflow failure.
     *
     * **Gotchas**
     *
     * Compensation finalizers are only registered for top-level effects in the workflow and do not work for nested activities.
     *
     * @category Compensation
     * @since 4.0.0
     */
    <A, R2>(compensation: (value: A, cause: Cause.Cause<unknown>) => Effect.Effect<void, never, R2>): <E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | R2 | WorkflowInstance | Scope.Scope>;
    /**
     * Adds compensation logic to an effect inside a Workflow.
     *
     * **Details**
     *
     * The compensation finalizer is called if the entire workflow fails, allowing you to perform cleanup or other actions based on the success value and the cause of the workflow failure.
     *
     * **Gotchas**
     *
     * Compensation finalizers are only registered for top-level effects in the workflow and do not work for nested activities.
     *
     * @category Compensation
     * @since 4.0.0
     */
    <A, E, R, R2>(effect: Effect.Effect<A, E, R>, compensation: (value: A, cause: Cause.Cause<unknown>) => Effect.Effect<void, never, R2>): Effect.Effect<A, E, R | R2 | WorkflowInstance | Scope.Scope>;
};
/**
 * Marks a workflow instance as suspended and interrupts the current fiber to
 * stop execution until it is resumed.
 *
 * @category results
 * @since 4.0.0
 */
export declare const suspend: (instance: WorkflowInstance["Service"]) => Effect.Effect<never>;
/**
 * Captures defects for a workflow and includes them in the result of the workflow or its activities.
 *
 * **Details**
 *
 * By default, this annotation is set to `true`, meaning defects are captured.
 *
 * @category annotations
 * @since 4.0.0
 */
export declare const CaptureDefects: Context.Reference<boolean>;
/**
 * Marks a workflow to suspend when it encounters any error.
 *
 * **Details**
 *
 * The suspended execution can later be resumed with the workflow's `resume` method, for example `MyWorkflow.resume(executionId)`.
 *
 * @category annotations
 * @since 4.0.0
 */
export declare const SuspendOnFailure: Context.Reference<boolean>;
export {};
//# sourceMappingURL=Workflow.d.ts.map