"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
// Default Install Store. Should only be used for development
class MemoryInstallationStore {
    constructor() {
        this.devDB = {};
    }
    async storeInstallation(installation, logger) {
        // NOTE: installations on a single workspace that happen to be within an enterprise organization are stored by
        // the team ID as the key
        // TODO: what about installations on an enterprise (acting as a single workspace) with `admin` scope, which is not
        // an org install?
        if (logger !== undefined) {
            logger.warn('Storing Access Token. Please use a real Installation Store for production!');
        }
        if (installation.isEnterpriseInstall && installation.enterprise !== undefined) {
            if (logger !== undefined) {
                logger.debug('storing org installation');
            }
            this.devDB[installation.enterprise.id] = installation;
        }
        else if (!installation.isEnterpriseInstall && installation.team !== undefined) {
            if (logger !== undefined) {
                logger.debug('storing single team installation');
            }
            this.devDB[installation.team.id] = installation;
        }
        else {
            throw new Error('Failed saving installation data to installationStore');
        }
    }
    async fetchInstallation(query, logger) {
        if (logger !== undefined) {
            logger.warn('Retrieving Access Token from DB. Please use a real Installation Store for production!');
        }
        if (query.isEnterpriseInstall) {
            if (query.enterpriseId !== undefined) {
                if (logger !== undefined) {
                    logger.debug('fetching org installation');
                }
                return this.devDB[query.enterpriseId];
            }
        }
        if (query.teamId !== undefined) {
            if (logger !== undefined) {
                logger.debug('fetching single team installation');
            }
            return this.devDB[query.teamId];
        }
        throw new Error('Failed fetching installation');
    }
    async deleteInstallation(query, logger) {
        if (logger !== undefined) {
            logger.warn('Deleting Access Token from DB. Please use a real Installation Store for production!');
        }
        if (query.isEnterpriseInstall && query.enterpriseId !== undefined) {
            if (logger !== undefined) {
                logger.debug('deleting org installation');
            }
            // Separate out installation from rest of database
            const _a = this.devDB, _b = query.enterpriseId, _ = _a[_b], devDB = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
            this.devDB = devDB;
        }
        else if (query.teamId !== undefined) {
            if (logger !== undefined) {
                logger.debug('deleting single team installation');
            }
            // Separate out installation from rest of database
            const _c = this.devDB, _d = query.teamId, _ = _c[_d], devDB = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
            this.devDB = devDB;
        }
        else {
            throw new Error('Failed to delete installation');
        }
    }
}
exports.default = MemoryInstallationStore;
//# sourceMappingURL=memory-store.js.map