"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CronJob = exports.Queue = void 0;
const connect_1 = __importDefault(require("connect"));
const client_1 = require("./client");
const body_parser_1 = __importDefault(require("body-parser"));
function Queue(route, handler, options) {
    const quirrel = new client_1.QuirrelClient({
        route,
        handler,
        options,
    });
    const server = (0, connect_1.default)();
    server.use(body_parser_1.default.text());
    server.use(async (req, res, next) => {
        if (req.url !== "/" + route) {
            return next();
        }
        const { body, status, headers } = await quirrel.respondTo(req.body, req.headers);
        res.statusCode = status;
        for (const [header, value] of Object.entries(headers)) {
            res.setHeader(header, value);
        }
        res.write(body);
        res.end();
    });
    server.enqueue = (payload, options) => quirrel.enqueue(payload, options);
    server.enqueueMany = (jobs) => quirrel.enqueueMany(jobs);
    server.get = () => quirrel.get();
    server.delete = (id) => quirrel.delete(id);
    server.getById = (id) => quirrel.getById(id);
    server.invoke = (id) => quirrel.invoke(id);
    return server;
}
exports.Queue = Queue;
function CronJob(route, cronSchedule, handler, options) {
    return Queue(route, handler, options);
}
exports.CronJob = CronJob;
//# sourceMappingURL=connect.js.map