"use strict";
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 CrudService_1 = __importDefault(require("../../Core/CrudService"));
var Tasks_consts_1 = require("../Tasks.consts");
var TasksService = /** @class */ (function () {
    function TasksService(session) {
        var _this = this;
        this.addTaskNote = function (taskId, wayPointId, note, type, image, hiddenFromDrivers, commonOptions) {
            return _this.service.routeGenerator
                .post(Tasks_consts_1.Routes.CREATE_TASK_NOTE)
                .withRouteParams({ id: taskId, way_point_id: wayPointId })
                .withQueryString({ note: note })
                .withPayload({ type: type, image: image, hidden_from_drivers: hiddenFromDrivers })
                .setException("failed to add note to task : ".concat(taskId))
                .withExtractor(Tasks_consts_1.taskExtractor)
                .withCommonOptions(commonOptions)
                .invoke();
        };
        this.service = new CrudService_1.default(session, 'tasks');
    }
    TasksService.prototype.load = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.get(id)];
            });
        });
    };
    TasksService.prototype.loadAll = function () {
        return __awaiter(this, void 0, void 0, function () {
            var result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.getAll()];
                    case 1:
                        result = _a.sent();
                        return [2 /*return*/, result.vehicles];
                }
            });
        });
    };
    TasksService.prototype.loadPlanningTasks = function (request, commonOptions) {
        return this.service.routeGenerator
            .get(Tasks_consts_1.Routes.GET_PLANNING_TASKS)
            .withQueryString(request)
            .setException("failed to fetch ".concat(Tasks_consts_1.Routes.GET_PLANNING_TASKS, " tasks"))
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.loadOpenTasks = function (request, commonOptions) {
        return this.service.routeGenerator
            .get(Tasks_consts_1.Routes.GET_OPEN_TASKS)
            .withQueryString(request)
            .setException("failed to fetch ".concat(Tasks_consts_1.Routes.GET_OPEN_TASKS, " tasks"))
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.loadInvalid = function (columns, commonOptions) {
        return this.service.routeGenerator
            .get(Tasks_consts_1.Routes.GET_INVALID_TASKS)
            .withQueryString(columns ? { columns: columns } : undefined)
            .setException('failed to fetch invalid tasks')
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.broadcastToDrivers = function (id, userIds, message, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.BROADCAST_TO_DRIVERS)
            .withPayload({ task_id: id, user_ids: userIds, message: message })
            .withCommonOptions(commonOptions)
            .setException("failed to broadcast to drivers task : ".concat(id))
            .invoke();
    };
    TasksService.prototype.acknowledgeTask = function (id, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.ACKNOWLEDGE_TASK)
            .withExtractor(Tasks_consts_1.taskExtractor)
            .setException("failed to acknowledge task - ".concat(id))
            .withPayload({ task_id: id })
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.releaseToDrivers = function (ids, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.RELEASE_TASKS_TO_DRIVERS)
            .setException("failed to release tasks [".concat(ids.join(', '), "] to drivers"))
            .withPayload({ ids: ids })
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.moveToPlanning = function (ids, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.MOVE_TASKS_TO_PLANNING)
            .setException("failed to move tasks [".concat(ids.join(', '), "] to planning"))
            .withPayload({ task_ids: ids })
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.massCancel = function (taskIds, reasonId, text, automaticallyCancelled, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.MASS_TASK_CANCEL)
            .withExtractor(Tasks_consts_1.successExtractor)
            .setException("failed to cancel tasks [".concat(taskIds.join(', '), "]"))
            .withPayload({ task_ids: taskIds, reason: reasonId, text: text, automatically_cancelled: automaticallyCancelled })
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.massAssign = function (user_id, ids, options, targetRun) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.MASS_TASK_ASSIGN)
            .setException("failed to assign tasks ".concat(ids, " to user ").concat(user_id))
            .withPayload({ user_id: user_id, task_ids: ids, options: options, target_run: targetRun })
            .withCommonOptions(options)
            .invoke();
    };
    TasksService.prototype.massUnassign = function (ids, options) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.MASS_TASK_UNASSIGN)
            .setException("failed to unassign tasks ".concat(ids))
            .withPayload({ task_ids: ids, options: options })
            .withCommonOptions(options)
            .invoke();
    };
    TasksService.prototype.cancel = function (id) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.CANCEL_TASK)
            .withExtractor(Tasks_consts_1.defaultExtractor)
            .setException("failed to cancel task - ".concat(id))
            .withRouteParams({ id: id })
            .invoke();
    };
    TasksService.prototype.update = function (task) {
        return __awaiter(this, void 0, void 0, function () {
            var result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.update(task.id, task)];
                    case 1:
                        result = _a.sent();
                        return [2 /*return*/, result.task];
                }
            });
        });
    };
    TasksService.prototype.updatePreparationStatus = function (taskId, preparationStatus, commonOptions) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .post(Tasks_consts_1.Routes.UPDATE_TASK_PREPARATION_STATUS)
                        .withExtractor(Tasks_consts_1.taskExtractor)
                        .setException("failed to update preparation status taskId: ".concat(taskId, ", status: ").concat(preparationStatus))
                        .withPayload({ task_id: taskId, preparation_status: preparationStatus })
                        .withCommonOptions(commonOptions)
                        .invoke()];
            });
        });
    };
    TasksService.prototype.rescheduleVirtualTasks = function (taskIds, noLaterThan, noEarlierThan) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.RESCHEDULE_VIRTUAL_TASKS)
            .withExtractor(Tasks_consts_1.taskIdsExtractor)
            .withPayload({ task_ids: taskIds, no_later_than: noLaterThan, no_earlier_than: noEarlierThan })
            .setException("failed to get reschedule virtual tasks for tasks : [".concat(taskIds.join(', '), "]"))
            .invoke();
    };
    TasksService.prototype.batchLoad = function (ids, fields, options) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .post(Tasks_consts_1.Routes.BATCH_GET_TASK)
                        .withExtractor(Tasks_consts_1.tasksExtractor)
                        .setException("failed to fetch tasks ids - ".concat(ids))
                        .withPayload({ ids: ids, fields: fields })
                        .withCommonOptions(options)
                        .invoke()];
            });
        });
    };
    TasksService.prototype.duplicate = function (taskId, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.DUPLICATE_TASK)
            .withExtractor(Tasks_consts_1.taskExtractor)
            .withPayload({ id: taskId })
            .setException("failed to duplicate task ".concat(taskId))
            .withRouteParams({ id: taskId })
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.getChildTasks = function (taskId, commonOptions) {
        return this.service.routeGenerator
            .get(Tasks_consts_1.Routes.GET_CHILD_TASKS)
            .withExtractor(Tasks_consts_1.childTasksExtractor)
            .withRouteParams({ id: taskId })
            .setException("failed to get child tasks for task ".concat(taskId))
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.unlock = function (taskIds, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.UNLOCK_TASK)
            .withExtractor(Tasks_consts_1.successExtractor)
            .withPayload({ task_ids: taskIds })
            .setException("failed to unlock the tasks [".concat(taskIds.join(', '), "]"))
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.lock = function (taskIds, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.LOCK_TASK)
            .withExtractor(Tasks_consts_1.successExtractor)
            .withPayload({ task_ids: taskIds })
            .setException("failed to lock the tasks [".concat(taskIds.join(', '), "]"))
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.finish = function (id, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.FINISH_TASK)
            .withExtractor(Tasks_consts_1.taskExtractor)
            .setException("failed to finish task - ".concat(id))
            .withRouteParams({ id: id })
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.rescheduleCancelled = function (id, commonOptions) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.RESCHEDULE_CANCELED)
            .withPayload({ id: id })
            .setException("failed to reschedule cancelled task ".concat(id))
            .withCommonOptions(commonOptions)
            .invoke();
    };
    TasksService.prototype.getRescheduleQuote = function (id) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.RESCHEDULE_QUOTE)
            .withPayload({ id: id })
            .withExtractor(Tasks_consts_1.quotesExtractor)
            .setException("failed to get reschedule quote task ".concat(id))
            .invoke();
    };
    TasksService.prototype.getLocations = function (id) {
        return this.service.routeGenerator
            .get(Tasks_consts_1.Routes.GET_TASK_LOCATIONS)
            .withExtractor(Tasks_consts_1.locationExtractor)
            .withRouteParams({ id: id })
            .setException("failed to get location of the task ".concat(id))
            .invoke();
    };
    TasksService.prototype.deleteWayPoint = function (taskId, wayPointId) {
        return this.service.routeGenerator
            .delete(Tasks_consts_1.Routes.DELETE_WAYPOINT)
            .withRouteParams({ id: taskId, way_point_id: wayPointId })
            .setException("failed to delete waypoint : task ".concat(taskId, ", waypoint ").concat(wayPointId))
            .invoke();
    };
    TasksService.prototype.importTasksCsv = function (file) {
        var formData = new FormData();
        formData.append('file', file);
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.IMPORT_TASKS_CSV)
            .withHeaders({ 'Content-Type': 'multipart/form-data' })
            .withPayload(formData)
            .invoke();
    };
    TasksService.prototype.activityLog = function (id) {
        return this.service.routeGenerator
            .get(Tasks_consts_1.Routes.GET_TASK_ACTIVITY_LOG)
            .withExtractor(Tasks_consts_1.defaultExtractor)
            .withRouteParams({ id: id })
            .setException("failed to get activity log for task ".concat(id))
            .invoke();
    };
    TasksService.prototype.massDeleteGroups = function (groupLeaderTaskIds) {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.MASS_DELETE_GROUPS)
            .withExtractor(Tasks_consts_1.successExtractor)
            .withPayload({ group_leader_task_ids: groupLeaderTaskIds })
            .setException("failed to massDeleteGroups [".concat(groupLeaderTaskIds.join(', '), "]"))
            .invoke();
    };
    TasksService.prototype.hasDefectiveGeocode = function () {
        return this.service.routeGenerator
            .post(Tasks_consts_1.Routes.HAS_DEFECTIVE_GEOCODE)
            .withExtractor(Tasks_consts_1.defaultExtractor)
            .setException('failed to get hasDefectiveGeocoded')
            .invoke();
    };
    return TasksService;
}());
exports.default = TasksService;
//# sourceMappingURL=tasks-service.js.map