1import type { ConfigError } from "effect/Config";
2import { Effect } from "effect";
3import type * as Scope from "effect/Scope";
4import type { AlchemyContext } from "./AlchemyContext.ts";
5import * as Apply from "./Apply.ts";
6import * as Plan from "./Plan.ts";
7import type { CompiledStack, StackEffect } from "./Stack.ts";
8import { evalStack } from "./Stack.ts";
9import type { Stage } from "./Stage.ts";
10
11export const destroy = ({
12 stack,
13 stage,
14 dev,
15 scope,
16 include,
17 exclude,
18}: {
19 stack: StackEffect<CompiledStack, ConfigError, Stage | AlchemyContext>;
20 stage: string;
21 dev?: boolean;
22 /** See {@link evalStack} — when set, scoped resources outlive `destroy`. */
23 scope?: Scope.Scope;
24 /** Destroy always operates on the full stack. */
25 include?: never;
26 exclude?: never;
27}) =>
28 include !== undefined || exclude !== undefined
29 ? Effect.die(
30 new Plan.InvalidResourceSelection({
31 message: "Filtered destroy is not supported.",
32 }),
33 )
34 : evalStack(
35 stack,
36 (stack) => Plan.destroy(stack).pipe(Effect.flatMap(Apply.apply)),
37 { stage, dev, scope },
38 );