"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
const { Headers } = require("@devlego/backend-core/constants");
const { getAppIdFromCtx } = require("@devlego/backend-core/utils");
module.exports = function ({ requiresAppId } = {}) {
    return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
        const appId = yield getAppIdFromCtx(ctx);
        if (requiresAppId && !appId) {
            ctx.throw(400, `Invalid app ID provided, please check the ${Headers.APP_ID} header.`);
        }
        if (!ctx.headers[Headers.API_KEY]) {
            ctx.throw(400, `Invalid API key provided, please check the ${Headers.API_KEY} header.`);
        }
        return next();
    });
};
