"use strict";
/**
 * UserSession class
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @todo this is very tightly coupled with LoVullo's system
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserSession = void 0;
var events_1 = require("events");
var php_serialize_1 = require("php-serialize");
/**
 * Session management
 */
var UserSession = /** @class */ (function (_super) {
    __extends(UserSession, _super);
    /**
     * Initializes a session from an existing PHP session
     *
     * @param _id       - session id
     * @param _memcache - memcache client used to access session
     * @param _data     - session data
     *
     * @return undefined
     */
    function UserSession(_id, _memcache) {
        if (_id === void 0) { _id = ''; }
        var _this = _super.call(this) || this;
        _this._id = _id;
        _this._memcache = _memcache;
        /** Session Data */
        _this._data = {};
        // parse the session data
        _this._getSessionData(function (data) {
            _this._data = data === null ? {} : data;
            // session data is now available
            _this.emit('ready', data);
        });
        return _this;
    }
    /**
     * Returns the session data
     *
     * @return session data
     */
    UserSession.prototype.getData = function () {
        return this._data;
    };
    /**
     * Returns whether the user is currently logged in
     *
     * This is determined simply by whether the agent id is available.
     *
     * @return whether the user is logged in or not
     */
    UserSession.prototype.isLoggedIn = function () {
        return this._data.agentID !== undefined ? true : false;
    };
    /**
     * Gets the agent id, if available
     *
     * @return agent id or undefined if unavailable
     */
    UserSession.prototype.agentId = function () {
        return this._data.agentID !== undefined ? +this._data.agentID : undefined;
    };
    /**
     * Gets the agent name, if available
     *
     * @return agent name or undefined if unavailable
     */
    UserSession.prototype.agentName = function () {
        return this._data.agentNAME || undefined;
    };
    /**
     * Gets the agency number, if available
     *
     * @return agency number or undefined if unavailable
     */
    UserSession.prototype.agencyNumber = function () {
        return this._data.retail_agency_num || undefined;
    };
    /**
     * Gets the user name, if available
     *
     * @return user name or undefined if unavailable
     */
    UserSession.prototype.userName = function () {
        return this._data.user_name || undefined;
    };
    /**
     * Whether the user is logged in as an internal user rather than a broker
     *
     * @return true if internal user, otherwise false
     */
    UserSession.prototype.isInternal = function () {
        return this.agentId() === 900000 ? true : false;
    };
    /**
     * Sets the agent id
     *
     * @param id - the agent id
     */
    UserSession.prototype.setAgentId = function (id) {
        this._data.agentID = id;
        return this;
    };
    /**
     * Gets the broker entity id, if available
     *
     * @return agent entity id or undefined if unavailable
     */
    UserSession.prototype.agentEntityId = function () {
        return this._data.broker_entity_id || undefined;
    };
    /**
     * Parses PHP session data from memcache and returns an object with the data
     *
     * @param callback - function to call with parsed data
     *
     * @return Object parsed session data
     */
    UserSession.prototype._getSessionData = function (callback) {
        this._memcache.get(this._id, function (err, data) {
            if (err || data === null) {
                // failure
                callback(null);
                return;
            }
            var session_data = {};
            if (data) {
                // Due to limitations of Javascript's regex engine, we need to do
                // this in a series of steps. First, split the string to find the
                // keys and their serialized values.
                var splits = data.split(/(\w+?)\|/);
                var len = splits.length;
                // associate the keys with their values
                for (var i = 1; i < len; i++) {
                    var key = splits[i];
                    var val = splits[++i];
                    // the values are serialized PHP data; unserialize them
                    val = php_serialize_1.unserialize(val);
                    // add to the session data
                    session_data[key] = val;
                }
            }
            // return the parsed session data
            callback(session_data);
        });
    };
    return UserSession;
}(events_1.EventEmitter));
exports.UserSession = UserSession;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiVXNlclNlc3Npb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJVc2VyU2Vzc2lvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQXFCRzs7Ozs7Ozs7Ozs7Ozs7OztBQUVILGlDQUFvQztBQUNwQywrQ0FBMEM7QUFJMUM7O0dBRUc7QUFDSDtJQUFpQywrQkFBWTtJQUkzQzs7Ozs7Ozs7T0FRRztJQUNILHFCQUNtQixHQUFnQixFQUNoQixTQUFjO1FBRGQsb0JBQUEsRUFBQSxRQUFnQjtRQURuQyxZQUlFLGlCQUFPLFNBUVI7UUFYa0IsU0FBRyxHQUFILEdBQUcsQ0FBYTtRQUNoQixlQUFTLEdBQVQsU0FBUyxDQUFLO1FBZGpDLG1CQUFtQjtRQUNYLFdBQUssR0FBd0IsRUFBRSxDQUFDO1FBZ0J0Qyx5QkFBeUI7UUFDekIsS0FBSSxDQUFDLGVBQWUsQ0FBQyxVQUFBLElBQUk7WUFDdkIsS0FBSSxDQUFDLEtBQUssR0FBRyxJQUFJLEtBQUssSUFBSSxDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsQ0FBQyxDQUFDLElBQUksQ0FBQztZQUV2QyxnQ0FBZ0M7WUFDaEMsS0FBSSxDQUFDLElBQUksQ0FBQyxPQUFPLEVBQUUsSUFBSSxDQUFDLENBQUM7UUFDM0IsQ0FBQyxDQUFDLENBQUM7O0lBQ0wsQ0FBQztJQUVEOzs7O09BSUc7SUFDSCw2QkFBTyxHQUFQO1FBQ0UsT0FBTyxJQUFJLENBQUMsS0FBSyxDQUFDO0lBQ3BCLENBQUM7SUFFRDs7Ozs7O09BTUc7SUFD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