import { getDurableRequestStore } from "./store.js";
import { isDurableBody } from "./is.js";
import { unlink } from "../file/unlink.js";
export async function deleteDurableRequest(durableRequestId) {
    const store = getDurableRequestStore();
    const existing = await store.get(durableRequestId);
    if (!existing) {
        return;
    }
    await deleteDurableRequestBody(existing);
    return store.delete(durableRequestId);
}
export async function deleteDurableRequestBody(durableRequest) {
    const fileIds = new Set();
    if (isDurableBody(durableRequest.body) && durableRequest.body.type === "file" && typeof durableRequest.body.value === "string") {
        fileIds.add(durableRequest.body.value);
    }
    if (isDurableBody(durableRequest.response?.body) && durableRequest.response.body.type === "file" && typeof durableRequest.response.body.value === "string") {
        fileIds.add(durableRequest.response.body.value);
    }
    if (!fileIds.size) {
        return;
    }
    await Promise.all([...fileIds].map(id => unlink(id)));
}
//# sourceMappingURL=delete-durable-request.js.map