'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.LaunchDarklyUtilsMembers = undefined;

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Class representing Team member functionality
var LaunchDarklyUtilsMembers = exports.LaunchDarklyUtilsMembers = function () {
    /**
     * Team member specific api functions attached as 'LaunchDarklyUtils.members'
     * @constructor LaunchDarklyUtilsMembers
     * @param { Swagger } apiClient - generated launchdarkly apiClient
     * @param { Object } log - logger implementation, or 'console'
     * @param { LaunchDarklyUtils } ldUtils - primary utils class
     * @returns { LaunchDarklyUtilsMembers } team member api functions
     */
    function LaunchDarklyUtilsMembers(apiClient, log, ldUtils) {
        (0, _classCallCheck3.default)(this, LaunchDarklyUtilsMembers);

        this.log = log;
        this.apiClient = apiClient;
        this.ldUtils = ldUtils;
        if (!this.ldUtils) {
            throw {
                message: 'LaunchDarklyUtilsRoles constructor requires ldUtils parameter'
            };
        }
    }

    /**
     * Api group object key in LD api
     * @returns {string}
     */


    (0, _createClass3.default)(LaunchDarklyUtilsMembers, [{
        key: 'getTeamMembers',


        /**
         * Get all team members in account
         * @returns {Promise}
         * @fulfil {Object} team member list json
         * @reject {Error} object with message
         */
        value: function () {
            var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
                return _regenerator2.default.wrap(function _callee$(_context) {
                    while (1) {
                        switch (_context.prev = _context.next) {
                            case 0:
                                _context.prev = 0;
                                return _context.abrupt('return', this.apiClient.apis[this.API_GROUP].getMembers().then(function (response) {
                                    return response.body;
                                }));

                            case 4:
                                _context.prev = 4;
                                _context.t0 = _context['catch'](0);
                                throw {
                                    api: 'getMembers',
                                    message: _context.t0.message,
                                    docs: 'https://apidocs.launchdarkly.com/docs/list-team-members'
                                };

                            case 7:
                            case 'end':
                                return _context.stop();
                        }
                    }
                }, _callee, this, [[0, 4]]);
            }));

            function getTeamMembers() {
                return _ref.apply(this, arguments);
            }

            return getTeamMembers;
        }()

        /**
         * get a single team member by id
         * @param memberId - _id field of team member
         * @returns {Promise}
         * @fulfil {Object} team member object json
         * @reject {Error} object with message
         */

    }, {
        key: 'getTeamMember',
        value: function () {
            var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(memberId) {
                return _regenerator2.default.wrap(function _callee2$(_context2) {
                    while (1) {
                        switch (_context2.prev = _context2.next) {
                            case 0:
                                _context2.prev = 0;
                                return _context2.abrupt('return', this.apiClient.apis[this.API_GROUP].getMember({ memberId: memberId }).then(function (response) {
                                    return response.body;
                                }));

                            case 4:
                                _context2.prev = 4;
                                _context2.t0 = _context2['catch'](0);
                                throw {
                                    api: 'getMember',
                                    message: _context2.t0.message,
                                    docs: 'https://apidocs.launchdarkly.com/docs/get-team-member'
                                };

                            case 7:
                            case 'end':
                                return _context2.stop();
                        }
                    }
                }, _callee2, this, [[0, 4]]);
            }));

            function getTeamMember(_x) {
                return _ref2.apply(this, arguments);
            }

            return getTeamMember;
        }()

        /**
         * Get a team member using a supplied email address
         * @param {string} emailAddress - email address of member to locate
         * @returns {Promise}
         * @fulfil {Object} team member json
         * @reject {Error} object with message
         */

    }, {
        key: 'getTeamMemberByEmail',
        value: function () {
            var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(emailAddress) {
                return _regenerator2.default.wrap(function _callee3$(_context3) {
                    while (1) {
                        switch (_context3.prev = _context3.next) {
                            case 0:
                                return _context3.abrupt('return', this.getTeamMembers().then(function (memberList) {
                                    var members = _lodash2.default.filter(memberList.items, { email: emailAddress });

                                    if (members.length !== 1) {
                                        throw {
                                            api: 'getTeamMembers',
                                            message: 'member not found for email ' + emailAddress,
                                            docs: 'https://apidocs.launchdarkly.com/docs/list-team-members'
                                        };
                                    }

                                    return members[0];
                                }));

                            case 1:
                            case 'end':
                                return _context3.stop();
                        }
                    }
                }, _callee3, this);
            }));

            function getTeamMemberByEmail(_x2) {
                return _ref3.apply(this, arguments);
            }

            return getTeamMemberByEmail;
        }()

        /**
         * Get a team member including customRoleKeys translated from customRoles array
         * @param {string} emailAddress - email address of member to locate
         * @returns {Promise}
         * @fulfil {Object} team member json
         * @reject {Error} object with message
         */

    }, {
        key: 'getTeamMemberCustomRoles',
        value: function () {
            var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(emailAddress) {
                var teamMember;
                return _regenerator2.default.wrap(function _callee4$(_context4) {
                    while (1) {
                        switch (_context4.prev = _context4.next) {
                            case 0:
                                _context4.next = 2;
                                return this.getTeamMemberByEmail(emailAddress);

                            case 2:
                                teamMember = _context4.sent;

                                teamMember.customRoleKeys = [];
                                return _context4.abrupt('return', this.ldUtils.roles.getCustomRoles().then(function (roles) {
                                    teamMember.customRoles.forEach(function (memberRoleId) {
                                        try {
                                            teamMember.customRoleKeys.push(_lodash2.default.filter(roles.items, { _id: memberRoleId })[0]['key']);
                                        } catch (e) {
                                            throw {
                                                api: 'getTeamMemberCustomRoles',
                                                message: 'role not found for _id ' + memberRoleId,
                                                docs: 'https://apidocs.launchdarkly.com/docs/list-team-members'
                                            };
                                        }
                                    });
                                    return teamMember;
                                }));

                            case 5:
                            case 'end':
                                return _context4.stop();
                        }
                    }
                }, _callee4, this);
            }));

            function getTeamMemberCustomRoles(_x3) {
                return _ref4.apply(this, arguments);
            }

            return getTeamMemberCustomRoles;
        }()
    }, {
        key: 'API_GROUP',
        get: function get() {
            return 'Team members';
        }
    }]);
    return LaunchDarklyUtilsMembers;
}();