"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ytAccessLogApi = ytAccessLogApi;
exports.ytAccesLogCheckAvailable = ytAccesLogCheckAvailable;
const axios_1 = __importDefault(require("axios"));
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
const qs_1 = __importDefault(require("qs"));
const ServerFactory_1 = __importDefault(require("../ServerFactory"));
const utils_1 = require("../utils");
const config_utils_1 = require("../utils/config-utils");
async function ytAccessLogApi(req, res) {
    try {
        await ytAccessLogApiImpl(req, res);
    }
    catch (e) {
        await (0, utils_1.sendAndLogError)(req.ctx, res, 500, e, {
            method: 'nodejs',
            query: req.query,
            page: req.headers.referer,
        });
    }
}
async function ytAccesLogCheckAvailable(req, res) {
    try {
        const { baseUrl } = await getAccessLogBaseUrl(req);
        res.send({ is_access_log_available: Boolean(baseUrl) });
    }
    catch (e) {
        await (0, utils_1.sendAndLogError)(req.ctx, res, 500, e, {
            method: 'nodejs',
            query: req.query,
            page: req.headers.referer,
        });
    }
}
function getAccessLogBaseUrl(req) {
    const { ytAuthCluster: cluster } = req.params;
    const isDeveloper = req.cookies.ui_config_mode === 'developer';
    return (0, config_utils_1.getBaseUrlFromConfiguration)(req, {
        cluster,
        isDeveloper,
        uiSettingsFieldName: 'accessLogBasePath',
    });
}
const ALLOWED_ACTIONS = new Set(['ready', 'visible-time-range', 'access_log', 'qt_access_log']);
async function ytAccessLogApiImpl(req, res) {
    const { action } = req.params;
    if (!ALLOWED_ACTIONS.has(action)) {
        throw new utils_1.ErrorWithCode(404, 'Unexpected action for Navigation/Access log');
    }
    const { baseUrl, testing } = await getAccessLogBaseUrl(req);
    if (!baseUrl) {
        throw new utils_1.ErrorWithCode(404, 'The UI installation is not configured to display "Navigation/Access log" tab for the current cluster. Please check your configuration: config.uiSettings.accessLogBasePath, //sys/@ui_config/access_log_base_url');
    }
    const authType = testing ? 'accessLogTest' : 'accessLogProd';
    const authHeaders = ServerFactory_1.default.getAuthHeaders(authType, req);
    const search = (0, isEmpty_1.default)(req.query) ? '' : `?${qs_1.default.stringify(req.query)}`;
    return axios_1.default
        .request({
        url: `${baseUrl}/${action}${search}`,
        method: req.method,
        headers: { ...req.ctx.getMetadata(), ...authHeaders, 'accept-encoding': 'gzip' },
        data: req.body,
        timeout: 100000,
        responseType: 'stream',
    })
        .then(async (response) => {
        const pipedSize = await (0, utils_1.pipeAxiosResponse)(req.ctx, res, response);
        if (!pipedSize) {
            throw new Error(utils_1.UNEXPECTED_PIPE_AXIOS_RESPONSE);
        }
    });
}
