'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var core = require('@al/core');

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

function __awaiter(thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
}

class AlTestExecutionContext extends core.AlExecutionContext {
    constructor() {
        super();
        this.openRequests = [];
        this.setOption(core.ConfigOption.DisableEndpointsResolution, true);
        this.setOption(core.ConfigOption.ResolveAccountMetadata, false);
        this.setOption(core.ConfigOption.LocalManagedContent, true);
    }
    base64Encode(data) {
        return data;
    }
    base64Decode(data) {
        return data;
    }
    handleRequest(request) {
        /*
        return new Promise<AlNetworkResponse<T>>( ( resolve, reject ) => {
            this.openRequests.push( {
                request: request,
                resolve: resolve,
                reject: reject
            } );
        } );
        */
        return Promise.resolve({
            request: request,
            data: {},
            status: 200,
            statusText: "OK",
            headers: {}
        });
    }
    static clearAuthState() {
        core.AlExecutionContext.session.deactivateSession();
        return null;
    }
    static setAuthState(state, actingAccountId = "default", userId = '1001-ABCDEF00-10105060-1234') {
        return __awaiter(this, void 0, void 0, function* () {
            if (!state) {
                return AlTestExecutionContext.clearAuthState();
            }
            if (typeof (state) === 'string') {
                state = {
                    accountId: state
                };
            }
            const nowTS = Date.now() / 1000;
            const sessionData = {
                authentication: {
                    user: {
                        id: state.userId || '1001-ABCDEF00-10105060-1234',
                        name: state.userName || 'Mister McNielsen',
                        email: 'mcnielsen@alertlogic.com',
                        active: true,
                        locked: false,
                        version: 1002,
                        linked_users: [],
                        created: {
                            at: 123456789,
                            by: 'McNielsen',
                        },
                        modified: {
                            at: 123456790,
                            by: 'Warsaw',
                        }
                    },
                    account: {
                        id: state.accountId,
                        name: "Kevin's Fast Company",
                        active: true,
                        accessible_locations: state.accessibleLocationIds || [
                            "defender-us-denver",
                            "insight-us-virginia"
                        ],
                        default_location: state.locationId || 'defender-us-denver',
                        created: {
                            at: 123456789,
                            by: 'McNielsen',
                        },
                        modified: {
                            at: 123456790,
                            by: 'Warsaw',
                        }
                    },
                    token: 'BigFatFakeToken',
                    token_expiration: nowTS + 86400,
                }
            };
            if (state.actingAccountId && state.actingAccountId !== 'default' && state.actingAccountId !== state.accountId) {
                sessionData.acting = Object.assign({}, sessionData.authentication.account);
                sessionData.acting.id = state.actingAccountId;
                sessionData.acting.name = "Kevin's Slow Company";
            }
            core.AlExecutionContext.setOption(core.ConfigOption.ResolveAccountMetadata, false);
            let resolved = yield core.AlExecutionContext.session.setAuthentication(sessionData);
            if (state.entitlements) {
                core.AlExecutionContext.session.setEffectiveEntitlements(AlTestExecutionContext.createEntitlementSet(state.entitlements));
            }
            if (state.primaryEntitlements) {
                core.AlExecutionContext.session.setPrimaryEntitlements(AlTestExecutionContext.createEntitlementSet(state.primaryEntitlements));
            }
            return resolved;
        });
    }
    static createEntitlementSet(entitlements = []) {
        return new core.AlEntitlementCollection(entitlements.map(entitlementId => {
            const date = new Date();
            date.setDate(date.getDate() + 1);
            return {
                productId: entitlementId,
                active: true,
                expires: date
            };
        }));
    }
}

exports.AlTestExecutionContext = AlTestExecutionContext;
//# sourceMappingURL=al-core-testing.es5.js.map
