import { listChanges, changeSchema } from "../../data/index.js";
import { authenticate } from "../authentication.js";
export async function listChangeRoutes(fastify) {
    const response = {
        200: {
            type: "array",
            items: changeSchema.change,
        },
    };
    const baseSchema = {
        description: "List of changes",
        tags: ["change"],
        summary: "",
        response,
        security: [
            {
                apiKey: [],
            },
        ],
    };
    const baseParams = {
        type: "object",
        properties: {
            changeType: {
                type: "string",
            },
            targetType: {
                type: "string",
            },
        },
        required: ["changeType", "targetType"],
    };
    try {
        const params = baseParams;
        const schema = {
            ...baseSchema,
            params
        };
        fastify.get("/", {
            schema,
            preHandler: authenticate(fastify, { anonymous: true }),
            async handler(request, response) {
                response.send(await listChanges({
                    type: request.params.changeType,
                    target: {
                        type: request.params.targetType
                    }
                }));
            },
        });
    }
    catch { }
    try {
        const params = {
            ...baseParams,
            properties: {
                ...baseParams.properties
            },
            required: [
                ...baseParams.required
            ]
        };
        const schema = {
            ...baseSchema,
            params
        };
        fastify.get("/:targetId", {
            schema,
            preHandler: authenticate(fastify, { anonymous: true }),
            async handler(request, response) {
                const changes = await listChanges({
                    type: request.params.changeType,
                    target: {
                        type: request.params.targetType,
                        id: request.params.targetId
                    }
                });
                response.send(changes);
            },
        });
    }
    catch { }
}
//# sourceMappingURL=list-changes.js.map