"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Gateway_1 = require("../remote-services/Gateway");
const Session_1 = require("../store/Session");
exports.default = async (ctx, next) => {
    const gapperToken = String(ctx.get('x-gapper-oauth-token') || ctx.query['gapper-oauth-token']);
    if (gapperToken) {
        const gateway = new Gateway_1.default(gapperToken);
        const owner = await gateway.getOwner();
        if (owner) {
            ctx.state.ownerType = owner.type;
            ctx.state.ownerId = owner.id;
            Session_1.default.set(`${owner.type}/${owner.id}`, { gapperToken });
        }
    }
    await next();
};
