"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const control_1 = require("@platformatic/control");
const log_1 = require("../utils/log");
const schemas_1 = require("../schemas");
async function default_1(fastify) {
    const typedFastify = fastify.withTypeProvider();
    const api = new control_1.RuntimeApiClient();
    const emptyMetrics = { dataCpu: [], dataLatency: [], dataMem: [], dataReq: [] };
    typedFastify.get('/runtimes', {
        schema: {
            querystring: {
                type: 'object',
                properties: {
                    includeAdmin: {
                        type: 'boolean',
                        default: false,
                    },
                },
            },
            response: { 200: { type: 'array', items: schemas_1.selectableRuntimeSchema } }
        }
    }, async (request) => {
        const runtimes = await api.getRuntimes();
        const selectableRuntimes = [];
        for (const runtime of runtimes) {
            if (!request.query.includeAdmin && runtime.packageName === 'watt-admin') {
                continue;
            }
            let selected = true;
            if (process.env.SELECTED_RUNTIME) {
                selected = process.env.SELECTED_RUNTIME === runtime.pid.toString();
            }
            selectableRuntimes.push({ ...runtime, packageName: runtime.packageName || '', packageVersion: runtime.packageVersion || '', url: runtime.url || '', selected });
        }
        return selectableRuntimes;
    });
    typedFastify.get('/runtimes/:pid/health', {
        schema: {
            params: { type: 'object', properties: { pid: { type: 'number' } }, required: ['pid'] },
            response: {
                200: {
                    type: 'object',
                    additionalProperties: false,
                    properties: {
                        status: {
                            type: 'string',
                            enum: ['OK', 'KO'],
                            description: "Status can only be 'OK' or 'KO'"
                        }
                    },
                    required: ['status']
                }
            }
        }
    }, async ({ params: { pid } }) => {
        try {
            const result = await api.getMatchingRuntime({ pid: pid.toString() });
            if (result.pid === pid) {
                return { status: 'OK' };
            }
            return { status: 'KO' };
        }
        catch {
            return { status: 'KO' };
        }
    });
    typedFastify.get('/runtimes/:pid/metrics', {
        schema: {
            params: { type: 'object', properties: { pid: { type: 'number' } }, required: ['pid'] },
            response: { 200: schemas_1.metricResponseSchema }
        },
    }, async ({ params: { pid } }) => {
        return typedFastify.mappedMetrics[pid]?.aggregated || emptyMetrics;
    });
    typedFastify.get('/runtimes/:pid/metrics/:serviceId', {
        schema: {
            params: {
                type: 'object',
                properties: {
                    pid: { type: 'number' },
                    serviceId: { type: 'string' }
                },
                required: ['pid', 'serviceId']
            },
            response: { 200: schemas_1.metricResponseSchema }
        }
    }, async ({ params: { pid, serviceId } }) => {
        return fastify.mappedMetrics[pid]?.services[serviceId]?.all || emptyMetrics;
    });
    typedFastify.get('/runtimes/:pid/metrics/:serviceId/:workerId', {
        schema: {
            params: {
                type: 'object',
                properties: {
                    pid: { type: 'number' },
                    serviceId: { type: 'string' },
                    workerId: { type: 'number' },
                },
                required: ['pid', 'serviceId', 'workerId']
            },
            response: { 200: schemas_1.metricResponseSchema }
        }
    }, async ({ params: { pid, serviceId, workerId } }) => {
        return fastify.mappedMetrics[pid]?.services[serviceId]?.[workerId] || emptyMetrics;
    });
    typedFastify.get('/runtimes/:pid/services', {
        schema: {
            params: { type: 'object', properties: { pid: { type: 'number' } }, required: ['pid'] },
            response: {
                200: {
                    type: 'object',
                    additionalProperties: false,
                    required: ['entrypoint', 'production', 'services'],
                    properties: {
                        entrypoint: {
                            type: 'string'
                        },
                        production: {
                            type: 'boolean'
                        },
                        services: {
                            type: 'array',
                            items: {
                                anyOf: [
                                    {
                                        type: 'object',
                                        required: ['id', 'type', 'status', 'version', 'localUrl', 'entrypoint', 'dependencies'],
                                        properties: {
                                            id: {
                                                type: 'string'
                                            },
                                            type: {
                                                type: 'string'
                                            },
                                            status: {
                                                type: 'string'
                                            },
                                            version: {
                                                type: 'string'
                                            },
                                            localUrl: {
                                                type: 'string'
                                            },
                                            entrypoint: {
                                                type: 'boolean'
                                            },
                                            workers: {
                                                type: 'number'
                                            },
                                            url: {
                                                type: 'string'
                                            },
                                            dependencies: {
                                                type: 'array',
                                                items: {
                                                    type: 'object',
                                                    required: ['id', 'url', 'local'],
                                                    properties: {
                                                        id: {
                                                            type: 'string'
                                                        },
                                                        url: {
                                                            type: 'string'
                                                        },
                                                        local: {
                                                            type: 'boolean'
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    {
                                        type: 'object',
                                        required: ['id', 'status'],
                                        properties: {
                                            id: {
                                                type: 'string'
                                            },
                                            status: {
                                                type: 'string'
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }, async (request) => {
        return api.getRuntimeServices(request.params.pid);
    });
    typedFastify.get('/runtimes/:pid/logs', {
        schema: {
            params: { type: 'object', properties: { pid: { type: 'number' } }, required: ['pid'] }
        }
    }, async ({ params: { pid }, log }) => (0, log_1.getLogsFromReadable)(await api.getRuntimeAllLogsStream(pid), log));
    typedFastify.get('/runtimes/:pid/openapi/:serviceId', {
        schema: {
            params: { type: 'object', properties: { pid: { type: 'number' }, serviceId: { type: 'string' } }, required: ['pid', 'serviceId'] }
        }
    }, async ({ params: { pid, serviceId } }) => {
        return api.getRuntimeOpenapi(pid, serviceId);
    });
    typedFastify.post('/runtimes/:pid/restart', {
        schema: {
            params: { type: 'object', properties: { pid: { type: 'number' } }, required: ['pid'] },
            body: { type: 'object' }
        }
    }, async (request) => {
        try {
            await api.restartRuntime(request.params.pid);
        }
        catch (err) {
            fastify.log.warn({ err }, 'Issue restarting the runtime');
        }
    });
}
//# sourceMappingURL=root.js.map