"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockInstancesIndex = void 0;
const tslib_1 = require("tslib");
const express_1 = (0, tslib_1.__importDefault)(require("express"));
const shared_1 = require("./shared");
class MockInstancesIndex {
    constructor() {
        this.indexInstances = [];
    }
    initialize() {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const app = (0, express_1.default)();
            app.use('/', (req, res, next) => {
                if (process.env.DEBUG)
                    console.log('Receiving request on mocked server %s.', req.url);
                return next();
            });
            app.get('/api/v1/instances/hosts', (req, res) => {
                const since = req.query.since;
                const filtered = this.indexInstances.filter(i => {
                    if (!since)
                        return true;
                    return i.createdAt > since;
                });
                return res.json({
                    total: filtered.length,
                    data: filtered
                });
            });
            this.server = yield (0, shared_1.randomListen)(app);
            return (0, shared_1.getPort)(this.server);
        });
    }
    addInstance(host) {
        this.indexInstances.push({ host, createdAt: new Date().toISOString() });
    }
    terminate() {
        return (0, shared_1.terminateServer)(this.server);
    }
}
exports.MockInstancesIndex = MockInstancesIndex;
//# sourceMappingURL=mock-instances-index.js.map