"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.httpWorkflow = exports.HttpWorkflowToken = void 0;
exports.createHttpWorkflowEvent = createHttpWorkflowEvent;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const events_1 = require("../events");
let _HttpWorkflowTokenMetadata_1;
class HttpWorkflowToken extends events_1.EventToken {
    static __tsfType = _HttpWorkflowTokenMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"HttpWorkflowToken\",\"typeName\":\"HttpWorkflowToken\",\"classType\":{\"$tsf\":0},\"properties\":[{\"name\":\"event\",\"type\":{\"kind\":16,\"typeName\":\"TEvent\",\"classType\":{\"$tsf\":1}},\"optional\":false}],\"methods\":[],\"hasConstructor\":false,\"constructorParameters\":[]}]", [() => HttpWorkflowToken, () => (typeof TEvent !== "undefined" ? TEvent : (typeof exports !== "undefined" ? exports.TEvent : undefined))]);
}
exports.HttpWorkflowToken = HttpWorkflowToken;
_HttpWorkflowTokenMetadata_1.classType = HttpWorkflowToken;
HttpWorkflowToken.__tsfType = _HttpWorkflowTokenMetadata_1;
exports.httpWorkflow = {
    onRoute: new HttpWorkflowToken('http.route'),
    onController: new HttpWorkflowToken('http.controller'),
    onResponse: new HttpWorkflowToken('http.response'),
    onRouteNotFound: new HttpWorkflowToken('http.route-not-found')
};
function createHttpWorkflowEvent(request, response, options = {}) {
    return {
        request,
        response,
        get sent() {
            return response.writableEnded;
        },
        hasNext: options.hasNext ?? (() => false),
        send: async (value) => {
            if (value instanceof Response) {
                response.statusCode = value.status;
                value.headers.forEach((headerValue, headerName) => response.setHeader(headerName, headerValue));
                response.end(Buffer.from(await value.arrayBuffer()));
                return;
            }
            response.end(value);
        },
        ...(options.route ? { route: options.route } : {})
    };
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"HttpRouteNotFoundWorkflowEvent\":{\"kind\":18,\"typeName\":\"HttpRouteNotFoundWorkflowEvent\",\"types\":[{\"kind\":20,\"name\":\"request\",\"type\":{\"kind\":16,\"typeName\":\"HttpRequest\",\"classType\":{\"$tsfImport\":[0,\"./request\",\"HttpRequest\"]}},\"optional\":false},{\"kind\":20,\"name\":\"response\",\"type\":{\"kind\":16,\"typeName\":\"HttpResponse\",\"classType\":{\"$tsfImport\":[0,\"./response\",\"HttpResponse\"]}},\"optional\":false},{\"kind\":20,\"name\":\"sent\",\"type\":{\"kind\":8},\"optional\":false}]},\"HttpWorkflowEvent\":{\"kind\":18,\"typeName\":\"HttpWorkflowEvent\",\"types\":[{\"kind\":20,\"name\":\"request\",\"type\":{\"kind\":16,\"typeName\":\"HttpRequest\",\"classType\":{\"$tsfImport\":[0,\"./request\",\"HttpRequest\"]}},\"optional\":false},{\"kind\":20,\"name\":\"response\",\"type\":{\"kind\":16,\"typeName\":\"HttpResponse\",\"classType\":{\"$tsfImport\":[0,\"./response\",\"HttpResponse\"]}},\"optional\":false},{\"kind\":20,\"name\":\"sent\",\"type\":{\"kind\":8},\"optional\":false}]}}]", [require]);
//# sourceMappingURL=workflow.js.map