import { EnvironmentRegistry, removedWithRelay } from "@t3tools/client-runtime/connection";
import { createRuntimeCommand } from "@t3tools/client-runtime/state/runtime";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import * as SubscriptionRef from "effect/SubscriptionRef";
import { connectionAtomRuntime } from "../../connection/runtime";
import { archiveCloudComposerDrafts } from "../../state/use-composer-drafts";
export class CloudDraftArchiveError extends Schema.TaggedError<CloudDraftArchiveError>()(
"CloudDraftArchiveError",
{
environmentCount: Schema.Number,
hasAccountId: Schema.Boolean,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Could not preserve local drafts for ${this.environmentCount} cloud environments before sign-out.`;
}
}
export const removeCloudEnvironments = createRuntimeCommand(connectionAtomRuntime, {
label: "cloud:preserve-drafts-and-remove-environments",
execute: Effect.fn("removeCloudEnvironments")(function* (accountId: string | null) {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const entries = yield* SubscriptionRef.get(registry.entries);
const environmentIds = new Set(
[...entries.values()].filter(removedWithRelay).map((entry) => entry.target.environmentId),
);
// Backup failure must retain local file owners so a later sign-in can retry.
yield* Effect.tryPromise({
// Resolve draft owners against every environment, not just the removed ones.
try: () =>
archiveCloudComposerDrafts(
accountId,
environmentIds,
[...entries.values()].map((entry) => entry.target.environmentId),
),
catch: (cause) =>
new CloudDraftArchiveError({
environmentCount: environmentIds.size,
hasAccountId: accountId !== null,
cause,
}),
});
yield* registry.removeRelayEnvironments();
}),
});