"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ROIStateStorageImpl = exports.TrackingEventQueueImpl = void 0;
const robotics_types_1 = require("./robotics-types");
const trackable_object_1 = require("./trackable-object");
const debug = require('debug')('aidoop:robotics:tracking-storage');
class TrackingEventQueueImpl {
    constructor() {
        this.trackingEvents = [];
        this.registeredTypes = [];
    }
    reset() {
        this.trackingEvents = [];
        this.registeredTypes = [];
    }
    registerType(registeredEvent) {
        this.registeredTypes.push(registeredEvent);
    }
    deregisterType(registeredEvent) {
        let idx = this.registeredTypes.indexOf(registeredEvent);
        if (idx > -1) {
            this.registeredTypes.splice(idx, 1);
        }
    }
    queue(trackingEvent) {
        if (this.registeredTypes.indexOf(trackingEvent.type) >= 0) {
            this.trackingEvents.push(trackingEvent);
        }
    }
    dequeue() {
        return this.trackingEvents.shift();
    }
    count() {
        return this.trackingEvents.length;
    }
    instanceofTrackingEvent(object) {
        return 'type' in object;
    }
}
exports.TrackingEventQueueImpl = TrackingEventQueueImpl;
class ROIStateStorageImpl {
    constructor() {
        this.states = {};
        this.eventQueue = {};
    }
    getObjectState(id) {
        const state = this.states[id];
        if (!state) {
            debug('not found', id, this.states);
        }
        return Object.assign({}, state);
    }
    getROIState(roi) {
        return Object.keys(this.states)
            .map(id => this.states[id])
            .filter(obj => obj.roi == roi)
            .sort((obj1, obj2) => (obj1.id > obj2.id ? 1 : -1))
            .map(obj => {
            return Object.assign({}, obj);
        });
    }
    updateObjectState(id, roi, pose, threshold) {
        if (!this.states[id]) {
            this.states[id] = new trackable_object_1.TrackableObjectImpl(id);
        }
        if (!this.eventQueue[roi]) {
            this.eventQueue[roi] = new TrackingEventQueueImpl();
            this.eventQueue[roi].registerType(robotics_types_1.TRACKING_EVENT_TYPES.IN);
            this.eventQueue[roi].registerType(robotics_types_1.TRACKING_EVENT_TYPES.OUT);
        }
        var changes = this.states[id].update(roi, pose, threshold
            ? Object.assign(Object.assign({}, robotics_types_1.DEFAULT_POSE_THRESHOLD), threshold) : robotics_types_1.DEFAULT_POSE_THRESHOLD);
        //if (changes !== null) changes.forEach(change => this.publish(change))
        if (changes !== null)
            changes.forEach(change => this.setROIEvent(change));
    }
    publish(event) {
        console.log('\n\n\n\nevent', event);
    }
    setROIEvent(event) {
        // which roi(camera) this event happens in??
        if (event.object.roi.toString() !== '') {
            this.eventQueue[event.object.roi.toString()].queue(event);
        }
        else if (event.object.roi.toString() !== '') {
            this.eventQueue[event.from.roi.toString()].queue(event);
        }
        else if (event.object.roi.toString() !== '') {
            this.eventQueue[event.to.roi.toString()].queue(event);
        }
    }
    getROIEvent(roi) {
        var _a;
        return (_a = this.eventQueue[roi]) === null || _a === void 0 ? void 0 : _a.dequeue();
    }
}
exports.ROIStateStorageImpl = ROIStateStorageImpl;
//# sourceMappingURL=tracking-storage.js.map