"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_1 = require("lodash");
var BringgException_1 = require("../Core/BringgException");
var ExceptionReason_1 = require("../Core/ExceptionReason");
var keyset_pagination_consts_1 = require("../utils/consts/keyset-pagination.consts");
var UsersStore_1 = __importDefault(require("./Store/UsersStore"));
var User_consts_1 = require("./User.consts");
var Users = /** @class */ (function () {
    function Users(session) {
        this.usersStore = new UsersStore_1.default(session);
        //keep previous names for backward compatibility
        this.onListUpdate = this.usersStore.onListUpdate;
        this.onUpdate = this.usersStore.onItemUpdate;
        this.onDelete = this.usersStore.onItemRemoved;
        this.onCreate = this.usersStore.onItemCreated;
        this.unsubscribe = this.usersStore.unsubscribe;
    }
    Users.prototype.create = function (user) {
        if ((0, lodash_1.isNil)(user.email) && (0, lodash_1.isNil)(user.phone)) {
            throw new BringgException_1.BringgException(ExceptionReason_1.ExceptionReason.INVALID_ARGUMENTS, 'Phone or Email is required');
        }
        return this.usersStore.create(user);
    };
    Users.prototype.getAllAdminsAndDispatchers = function (_a) {
        var _b = _a.limit, limit = _b === void 0 ? 25 : _b, _c = _a.page_action, page_action = _c === void 0 ? keyset_pagination_consts_1.KeysetPaginationPageAction.NEXT : _c, cursor = _a.cursor;
        return this.usersStore.getAllAdminsAndDispatchers({ limit: limit, page_action: page_action, cursor: cursor });
    };
    Users.prototype.getAllDrivers = function (_a) {
        var _b = _a.limit, limit = _b === void 0 ? 25 : _b, _c = _a.page_action, page_action = _c === void 0 ? keyset_pagination_consts_1.KeysetPaginationPageAction.NEXT : _c, cursor = _a.cursor, options = _a.options;
        var optionsWithDefaults = (0, lodash_1.defaults)(options, { onlyOnlineDriver: false, onlyCustomers: false });
        return this.usersStore.getAllDrivers({ limit: limit, page_action: page_action, cursor: cursor, options: optionsWithDefaults });
    };
    Users.prototype.byUserType = function (userTypeId) {
        return this.usersStore.byUserType(userTypeId);
    };
    Users.prototype.makeAdmin = function (userId) {
        return this.usersStore.makeAdmin(userId);
    };
    Users.prototype.totalEmployeesCount = function () {
        return this.usersStore.totalEmployeesCount();
    };
    Users.prototype.totalDriversCount = function () {
        return this.usersStore.totalDriversCount();
    };
    Users.prototype.getAdmin = function (adminId) {
        return this.usersStore.get(adminId);
    };
    Users.prototype.getDriver = function (driverId) {
        return this.usersStore.get(driverId);
    };
    Users.prototype.getDispatcher = function (dispatcherId) {
        return this.usersStore.get(dispatcherId);
    };
    Users.prototype.getDriversByTeam = function (teamId) {
        return this.usersStore.getDriversByTeam(teamId);
    };
    Users.prototype.getAll = function (page, limit) {
        if (page === void 0) { page = 1; }
        if (limit === void 0) { limit = 50; }
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.usersStore.getAll(page, limit)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    Users.prototype.getAllKeysetPagination = function (_a) {
        var _b = _a.limit, limit = _b === void 0 ? 2500 : _b, _c = _a.page_action, page_action = _c === void 0 ? keyset_pagination_consts_1.KeysetPaginationPageAction.NEXT : _c, cursor = _a.cursor;
        return this.usersStore.getAllKeysetPagination({ limit: limit, page_action: page_action, cursor: cursor });
    };
    Users.prototype.get = function (userId) {
        return __awaiter(this, void 0, void 0, function () {
            var user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.usersStore.get(userId)];
                    case 1:
                        user = _a.sent();
                        this.usersStore.addToStore(user);
                        return [2 /*return*/, user];
                }
            });
        });
    };
    Users.prototype.getMany = function (usersIds, commonOptions) {
        return this.usersStore.getMany(usersIds, commonOptions);
    };
    Users.prototype.getLocal = function (userId) {
        return this.usersStore.getItem(userId);
    };
    Users.prototype.invite = function (userId) {
        return this.usersStore.invite(userId);
    };
    Users.prototype.unlock = function (userId) {
        return this.usersStore.unlock(userId);
    };
    Users.prototype.assignedTasksCount = function (userId) {
        return this.usersStore.assignedTasksCount(userId);
    };
    Users.prototype.inviteByEmail = function (userId) {
        return this.usersStore.inviteByEmail(userId);
    };
    Users.prototype.validatePhone = function (phone) {
        return this.usersStore.validatePhone(phone);
    };
    Users.prototype.closedTasks = function (userId, page) {
        return this.usersStore.closedTasks(userId, page);
    };
    Users.prototype.update = function (userId, userDelta) {
        var _this = this;
        return this.usersStore.update(userId, userDelta).then(function (user) { return _this.updateCurrentUserIfNeeded(user); });
    };
    Users.prototype.updateCurrentUserIfNeeded = function (user) {
        var currentUser = this.usersStore.usersService.session.user;
        if (currentUser.id === user.id) {
            this.usersStore.usersService.session.user = (0, lodash_1.merge)(currentUser, user);
            this.usersStore.usersService.session.sendDataEvent(User_consts_1.UserEvents.CURRENT_USER_UPDATED, this.usersStore.usersService.session.user);
        }
        return user;
    };
    Users.prototype.delete = function (userId) {
        return this.usersStore.delete(userId);
    };
    Users.prototype.getProfileImage = function (userId) {
        return this.usersStore.getProfileImage(userId);
    };
    Users.prototype.subscribeToGroupUpdates = function (fn) {
        this.usersStore.subscribeToGroupUpdates(fn);
    };
    Users.prototype.getOfflineDrivers = function () {
        return this.usersStore.getOfflineDrivers();
    };
    Users.prototype.getOnlineDrivers = function () {
        return this.usersStore.getOnlineDrivers();
    };
    Users.prototype.getAllLocalDrivers = function () {
        return this.usersStore.getAllLocalDrivers();
    };
    Users.prototype.getOnShiftDrivers = function () {
        return this.usersStore.getOnShiftDrivers();
    };
    Users.prototype.getAdmins = function () {
        return this.usersStore.getAdmins();
    };
    Users.prototype.getDispatchers = function () {
        return this.usersStore.getDispatchers();
    };
    Users.prototype.messageDriver = function (userId, message) {
        return this.usersStore.messageDriver(userId, message);
    };
    Users.prototype.getDriverCost = function (userId) {
        return this.usersStore.usersService.getDriverCost(userId);
    };
    Users.prototype.generateResetPasswordToken = function (email) {
        return this.usersStore.usersService.generateResetPasswordToken(email);
    };
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "create", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getAllAdminsAndDispatchers", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getAllDrivers", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "byUserType", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "makeAdmin", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "totalEmployeesCount", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "totalDriversCount", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getAdmin", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getDriver", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getDispatcher", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getDriversByTeam", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getAll", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getAllKeysetPagination", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "get", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getMany", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "invite", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "unlock", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "assignedTasksCount", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "inviteByEmail", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "validatePhone", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "closedTasks", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "update", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "delete", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getProfileImage", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "subscribeToGroupUpdates", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getOfflineDrivers", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getOnlineDrivers", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getAllLocalDrivers", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getOnShiftDrivers", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getAdmins", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getDispatchers", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "messageDriver", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "getDriverCost", null);
    __decorate([
        BringgException_1.logOnException
    ], Users.prototype, "generateResetPasswordToken", null);
    return Users;
}());
exports.default = Users;
//# sourceMappingURL=Users.js.map