"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const integration_base_1 = require("@things-factory/integration-base");
const utils_1 = require("@things-factory/utils");
const get_tracking_workspace_1 = require("./get-tracking-workspace");
async function TrackableObjectWaitStable(step, { root, domain }) {
    var { connection, params: { minRetention } } = step;
    var object = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, connection) || {};
    if (!object) {
        throw new Error(`no connection : ${connection}`);
    }
    var { endpoint } = object;
    var retentionCount = 0;
    var targetObjectState = null;
    while (true) {
        let state = root.getState();
        if (state == integration_base_1.ScenarioInstanceStatus.STARTED /* STARTED */) {
            await (0, utils_1.sleep)(1000);
            var workspace = (0, get_tracking_workspace_1.getTrackingWorkspace)(domain);
            var { engine } = workspace;
            var { trackingStorage } = engine;
            targetObjectState = trackingStorage.getObjectState(endpoint);
            retentionCount = (targetObjectState === null || targetObjectState === void 0 ? void 0 : targetObjectState.retention) || 0;
            let targetRoi = (targetObjectState === null || targetObjectState === void 0 ? void 0 : targetObjectState.roi) || '';
            // console.log('retentionCount: ', retentionCount)
            // console.log('ROI: ', targetRoi)
            if (retentionCount > minRetention && targetRoi != '') {
                break;
            }
        }
        else if (state == integration_base_1.ScenarioInstanceStatus.STOPPED /* PAUSED */) {
            await (0, utils_1.sleep)(1000);
        }
        else {
            throw new Error('scenario stopped unexpectedly');
        }
    }
    // console.log('targetObjectState: ', targetObjectState)
    return {
        data: targetObjectState
    };
}
TrackableObjectWaitStable.parameterSpec = [
    {
        type: 'number',
        name: 'minRetention',
        label: 'min-retention'
    }
];
TrackableObjectWaitStable.help = 'robotics/task/trackable-object-wait-stable';
integration_base_1.TaskRegistry.registerTaskHandler('trackable-object-wait-stable', TrackableObjectWaitStable);
//# sourceMappingURL=trackable-object-wait-stable.js.map