"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createOAuthAuthorizationResolver = createOAuthAuthorizationResolver;
const oauth_1 = require("../components/oauth");
const constants_1 = require("../../shared/constants");
function createOAuthAuthorizationResolver() {
    return async function resoleOAuthAuthorize(req, res, next) {
        if (!(0, oauth_1.isOAuthAllowed)(req) || !(0, oauth_1.isUserOAuthLogged)(req)) {
            next();
            return;
        }
        try {
            const { ytAuthCluster } = req.params;
            if (ytAuthCluster) {
                res.setHeader(constants_1.YT_UI_CLUSTER_HEADER_NAME, ytAuthCluster);
            }
            const token = await (0, oauth_1.getOAuthAccessToken)(req, res);
            req.yt = {
                ytApiAuthHeaders: {
                    Cookie: `access_token=${token}`,
                },
            };
            req.ctx.log('OAuth: provide a token');
        }
        catch (e) {
            req.ctx.logError('Can not resolve access token', e);
        }
        next();
        return;
    };
}
