"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const AWS = __importStar(require("aws-sdk/global"));
// import Config from '@botsbotsbots/common/config';
const localstorage_1 = __importDefault(require("../utils/localstorage"));
const scopes = [
    'bot',
    'files:write:user',
    'files:read',
    'links:read',
    'links:write',
    'channels:read',
    'channels:history',
    'team:read',
    'emoji:read',
    'users:read'
];
const { SLACK_CLIENT_ID } = process.env;
function makeIdentityId() {
    return __awaiter(this, void 0, void 0, function* () {
        AWS.config.region = 'us-west-2';
        const creds = new AWS.CognitoIdentityCredentials({
            IdentityPoolId: process.env.COGNITO_IDENTITY_POOL_ID
        }, {
            region: process.env.AWS_REGION
        });
        yield creds.getPromise();
        AWS.config.credentials = creds;
        if (!creds.identityId) {
            return Promise.reject(new Error('Expected identityId'));
        }
        // await Storage.setItem("bbb-id", creds.identityId);
        return creds.identityId;
    });
}
exports.makeIdentityId = makeIdentityId;
function getOauthUrl(identityId) {
    return `https://slack.com/oauth/authorize?client_id=${SLACK_CLIENT_ID}&scope=${scopes.join(',')}&state=${encodeURIComponent(identityId)}&redirect_uri=${encodeURIComponent(`https://www.botsbotsbots.party/oauth`)}`;
}
exports.getOauthUrl = getOauthUrl;
function doRedirect(identityId) {
    const redirectUri = getOauthUrl(identityId);
    window.location.replace(redirectUri);
}
function getIdentityId() {
    return __awaiter(this, void 0, void 0, function* () {
        let bbbId = yield localstorage_1.default.getItem('bbb-id');
        let identityId;
        if (bbbId) {
            identityId = bbbId;
        }
        else {
            identityId = yield makeIdentityId();
        }
        return identityId;
    });
}
exports.getIdentityId = getIdentityId;
function maybeRedirect() {
    return __awaiter(this, void 0, void 0, function* () {
        const identityId = yield getIdentityId();
        console.log('retrieved identity: ' + identityId);
        yield localstorage_1.default.setItem('bbb-id', identityId);
        window.location.replace(getOauthUrl(identityId));
    });
}
exports.maybeRedirect = maybeRedirect;
//# sourceMappingURL=redirect.js.map