"use strict";
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var storable_realtime_subscriptions_1 = __importDefault(require("../../Data/storable-realtime-subscriptions"));
var Tasks_consts_1 = require("../Tasks.consts");
var TaskRealtimeSubscriptions = /** @class */ (function () {
    function TaskRealtimeSubscriptions(session, tasksStore, storableDataService) {
        this.tasksStore = tasksStore;
        this.storableRealtimeSubscriptions = new storable_realtime_subscriptions_1.default(session, tasksStore, storableDataService);
    }
    TaskRealtimeSubscriptions.prototype.updateStoreOnEvents = function () {
        var _this = this;
        var events = [
            {
                name: Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE,
                isDelete: false,
                extraDataFunc: function (task) {
                    var isPartialUpdate = !Object.prototype.hasOwnProperty.call(task, 'status');
                    return { ignore: isPartialUpdate && !_this.tasksStore.get(task.id) };
                }
            },
            {
                name: Tasks_consts_1.TaskRealTimeEvents.NEW_TASK,
                isDelete: false
            },
            { name: Tasks_consts_1.TaskRealTimeEvents.TASK_DELETED, isDelete: true }
        ];
        var subEntityEvents = [
            {
                name: Tasks_consts_1.TaskRealTimeEvents.NEW_SCAN,
                eventToRaise: Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE,
                parentField: 'task_id',
                field: 'scans',
                ignoreFields: ['way_points'],
                isDelete: false
            },
            {
                name: Tasks_consts_1.TaskRealTimeEvents.NEW_NOTE,
                eventToRaise: Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE,
                parentField: 'task_id',
                field: 'task_notes',
                ignoreFields: ['way_points'],
                isDelete: false
            },
            {
                name: Tasks_consts_1.TaskRealTimeEvents.WAYPOINT_ADDED,
                eventToRaise: Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE,
                parentField: 'task_id',
                field: 'way_points',
                ignoreFields: ['way_points'],
                isDelete: false
            },
            {
                name: Tasks_consts_1.TaskRealTimeEvents.WAYPOINT_UPDATED,
                eventToRaise: Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE,
                parentField: 'task_id',
                field: 'way_points',
                ignoreFields: ['way_points'],
                isDelete: false
            },
            {
                name: Tasks_consts_1.TaskRealTimeEvents.WAYPOINT_DELETED,
                eventToRaise: Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE,
                idField: 'way_point_id',
                parentField: 'task_id',
                field: 'way_points',
                ignoreFields: ['way_points'],
                isDelete: true
            }
        ];
        this.storableRealtimeSubscriptions.updateStoreOnEvents(events, subEntityEvents);
    };
    TaskRealtimeSubscriptions.prototype.onCreate = function (callback) {
        return this.storableRealtimeSubscriptions.onEvent(Tasks_consts_1.TaskRealTimeEvents.NEW_TASK, callback);
    };
    TaskRealtimeSubscriptions.prototype.onDelete = function (callback) {
        return this.storableRealtimeSubscriptions.onEvent(Tasks_consts_1.TaskRealTimeEvents.TASK_DELETED, callback);
    };
    TaskRealtimeSubscriptions.prototype.onWayPointUpdate = function (callback) {
        return this.storableRealtimeSubscriptions.onEvent(Tasks_consts_1.TaskRealTimeEvents.WAYPOINT_UPDATED, callback);
    };
    TaskRealtimeSubscriptions.prototype.onWayPointDelete = function (callback) {
        return this.storableRealtimeSubscriptions.onEvent(Tasks_consts_1.TaskRealTimeEvents.WAYPOINT_DELETED, callback);
    };
    TaskRealtimeSubscriptions.prototype.onUpdate = function (callback) {
        return this.storableRealtimeSubscriptions.onEvent(Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE, callback);
    };
    TaskRealtimeSubscriptions.prototype.callRealTimeEvent = function (modifiedItem) {
        var realTimeEvent = modifiedItem.diff ? Tasks_consts_1.TaskRealTimeEvents.TASK_UPDATE : Tasks_consts_1.TaskRealTimeEvents.NEW_TASK;
        this.storableRealtimeSubscriptions.runEventCallbacks(realTimeEvent, modifiedItem);
    };
    TaskRealtimeSubscriptions.prototype.callUpdateTaskRealTimeEvents = function (modifiedItems) {
        var e_1, _a;
        try {
            for (var modifiedItems_1 = __values(modifiedItems), modifiedItems_1_1 = modifiedItems_1.next(); !modifiedItems_1_1.done; modifiedItems_1_1 = modifiedItems_1.next()) {
                var modifiedItem = modifiedItems_1_1.value;
                this.callRealTimeEvent(modifiedItem);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (modifiedItems_1_1 && !modifiedItems_1_1.done && (_a = modifiedItems_1.return)) _a.call(modifiedItems_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    };
    return TaskRealtimeSubscriptions;
}());
exports.default = TaskRealtimeSubscriptions;
//# sourceMappingURL=task-realtime-subscriptions.js.map