var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
    var useValue = arguments.length > 2;
    for (var i = 0; i < initializers.length; i++) {
        value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
    }
    return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
    var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
    var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
    var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
    var _, done = false;
    for (var i = decorators.length - 1; i >= 0; i--) {
        var context = {};
        for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
        for (var p in contextIn.access) context.access[p] = contextIn.access[p];
        context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
        var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
        if (kind === "accessor") {
            if (result === void 0) continue;
            if (result === null || typeof result !== "object") throw new TypeError("Object expected");
            if (_ = accept(result.get)) descriptor.get = _;
            if (_ = accept(result.set)) descriptor.set = _;
            if (_ = accept(result.init)) initializers.unshift(_);
        }
        else if (_ = accept(result)) {
            if (kind === "field") initializers.unshift(_);
            else descriptor[key] = _;
        }
    }
    if (target) Object.defineProperty(target, contextIn.name, descriptor);
    done = true;
};
import * as plugins from '../plugins.js';
/**
 * TypedRequest controller for type-safe RPC endpoint
 */
let TypedRequestController = (() => {
    let _classDecorators = [plugins.smartserve.Route('/typedrequest')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _instanceExtraInitializers = [];
    let _handleTypedRequest_decorators;
    let _handleTypedRequestHead_decorators;
    var TypedRequestController = class {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
            _handleTypedRequest_decorators = [plugins.smartserve.Post('')];
            _handleTypedRequestHead_decorators = [plugins.smartserve.Head('')];
            __esDecorate(this, null, _handleTypedRequest_decorators, { kind: "method", name: "handleTypedRequest", static: false, private: false, access: { has: obj => "handleTypedRequest" in obj, get: obj => obj.handleTypedRequest }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _handleTypedRequestHead_decorators, { kind: "method", name: "handleTypedRequestHead", static: false, private: false, access: { has: obj => "handleTypedRequestHead" in obj, get: obj => obj.handleTypedRequestHead }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            TypedRequestController = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
            __runInitializers(_classThis, _classExtraInitializers);
        }
        typedRouter = __runInitializers(this, _instanceExtraInitializers);
        constructor(typedRouter) {
            this.typedRouter = typedRouter;
        }
        async handleTypedRequest(ctx) {
            try {
                const body = await ctx.json();
                const response = await this.typedRouter.routeAndAddResponse(body);
                return new Response(plugins.smartjson.stringify(response), {
                    status: 200,
                    headers: {
                        'Content-Type': 'application/json',
                    },
                });
            }
            catch (error) {
                return new Response(JSON.stringify({ error: 'Invalid request' }), {
                    status: 400,
                    headers: {
                        'Content-Type': 'application/json',
                    },
                });
            }
        }
        async handleTypedRequestHead() {
            // HEAD request for online checking from service worker
            return new Response(null, {
                status: 200,
                headers: {
                    'Content-Type': 'application/json',
                },
            });
        }
    };
    return TypedRequestController = _classThis;
})();
export { TypedRequestController };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udHJvbGxlci50eXBlZHJlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8u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