"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFileUploadDirectory = createFileUploadDirectory;
exports.validateApiTestMessage = validateApiTestMessage;
const fs_1 = __importDefault(require("fs"));
const Globals_1 = require("../../Globals");
const mablApi_1 = require("../../mablApi");
const utils_1 = require("../utils");
function createFileUploadDirectory(testContext) {
    const dir = Globals_1.Globals.getUploadDirectory();
    const prefix = Globals_1.Globals.getUploadDirectoryPrefix();
    const uploadDirectoryName = `${dir}/${prefix}${Date.now()}-`;
    testContext.addFileUploadDir(uploadDirectoryName);
    return fs_1.default.mkdtempSync(uploadDirectoryName);
}
function validateApiTestMessage(request, response) {
    const authorizationHeader = request.get('authorization');
    if (authorizationHeader === undefined) {
        (0, utils_1.handleExecutionFailure)(response, 'Illegal authorization');
        return false;
    }
    const contentType = request.get('content-type');
    if (contentType?.toLowerCase() !== 'application/json') {
        (0, utils_1.handleExecutionFailure)(response, 'Illegal content type. Must be application/json');
        return false;
    }
    const pubsubMessage = request.body;
    if (typeof pubsubMessage?.message?.data !== 'string') {
        (0, utils_1.handleExecutionFailure)(response, 'Illegal message data format');
        return false;
    }
    if (typeof pubsubMessage?.subscription !== 'string') {
        (0, utils_1.handleExecutionFailure)(response, 'Illegal message subscription format');
        return false;
    }
    if (pubsubMessage?.message?.attributes?.test_type !==
        mablApi_1.TestTypeEnum.Api.toString()) {
        (0, utils_1.handleExecutionFailure)(response, 'Illegal test type');
        return false;
    }
    return true;
}
