import { getUserCredential, deleteUserCredential, } from "../../data/index.js";
import { authenticate } from "../authentication.js";
import { getUser } from "../../authentication/index.js";
import { ok } from "../../is.js";
export async function deleteUserCredentialRoutes(fastify) {
    try {
        const params = {
            type: "object",
            properties: {
                userId: {
                    type: "string"
                },
                userCredentialId: {
                    type: "string"
                }
            },
            required: ["userId", "userCredentialId"]
        };
        const schema = {
            description: "Delete a user credential",
            tags: ["user credential"],
            summary: "",
            params,
            security: [
                {
                    apiKey: [],
                },
            ],
        };
        async function handler(request, response) {
            const { userCredentialId, userId } = request.params;
            const user = getUser();
            ok(user.userId === userId, "Expected userId to match");
            const userCredential = await getUserCredential({
                userCredentialId,
                userId,
            });
            if (!userCredential) {
                response.status(404);
                response.send();
                return;
            }
            await deleteUserCredential(userCredential);
            const { redirect } = request.query;
            if (redirect) {
                response.header("Location", redirect);
                response.status(302);
                response.send();
                return;
            }
            response.status(204);
            response.send();
        }
        fastify.delete("/:userCredentialId", {
            schema,
            preHandler: authenticate(fastify),
            handler,
        });
        fastify.get("/:userCredentialId/delete", {
            schema,
            preHandler: authenticate(fastify),
            handler,
        });
    }
    catch { }
}
//# sourceMappingURL=delete-user-credential.js.map