import express from 'express';
import { getAvailablePort } from '../development/port-finder.js';
export async function startInternalApp(mainApp, port) {
    const app = express();
    app.locals.mainApp = mainApp;
    const finalPort = port === 0 ? await getAvailablePort(3001) : port;
    app.get('/health', async (req, res) => {
        if (mainApp.locals.service?.healthy) {
            try {
                const ok = await mainApp.locals.service.healthy(mainApp);
                res.sendStatus(ok ? 200 : 500);
            }
            catch (error) {
                mainApp.locals.logger.error(error, 'Health check failed');
            }
        }
        else {
            res.sendStatus(200);
        }
    });
    const listenPromise = new Promise((accept) => {
        app.locals.server = app.listen(finalPort, () => {
            accept();
        });
        app.locals.server.on('error', (error) => {
            mainApp.locals.logger.error(error, 'Internal app server error');
        });
    });
    await listenPromise;
    mainApp.locals.logger.info({ port: finalPort }, 'Internal metadata server started');
    return app;
}
//# sourceMappingURL=internal-server.js.map