"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(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 (_) 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 };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var tlv_converter_1 = require("../../client/impl/converter/tlv-converter");
var TlvBundleConverter_1 = require("../extra-converter/TlvBundleConverter");
var converter_1 = require("../converter");
var logger_1 = require("../../logger");
var debug = logger_1.default("DataLogger").debug;
/**
 * Experimental data logger class
 *
 * @experimental
 */
var DataLogger = /** @class */ (function () {
    function DataLogger(tap, converter, options) {
        if (converter === void 0) { converter = new DataLogPacketConverter(); }
        this.tap = tap;
        this.converter = converter;
        this.options = options;
        this.source = rxjs_1.interval(this.options.period);
    }
    DataLogger.fromTap = function (tap, options) {
        var converter = DataLogPacketConverter.createFromManager(tap.bundles, tap.variables);
        return new DataLogger(tap, converter, options);
    };
    DataLogger.prototype.dequeue = function () {
        return __awaiter(this, void 0, void 0, function () {
            var singlePacketResponse, singlePacket;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.tap.service.datalog.dequeueOnePacket()];
                    case 1:
                        singlePacketResponse = (_a.sent());
                        singlePacketResponse.successful();
                        singlePacket = singlePacketResponse.body();
                        try {
                            return [2 /*return*/, this.converter.decode({
                                    raw: singlePacketResponse.rawBody(),
                                    singlePacket: singlePacket
                                })];
                        }
                        catch (err) {
                            throw new DataLogger.CannotDecodePacketError(singlePacket, err);
                        }
                        return [2 /*return*/];
                }
            });
        });
    };
    DataLogger.prototype.getPacketCount = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.tap.service.datalog.getPacketCount()];
                    case 1: return [2 /*return*/, (_a.sent()).body()];
                }
            });
        });
    };
    DataLogger.prototype.download = function () {
        var _this = this;
        return this.source.pipe(operators_1.flatMap(function () { return rxjs_1.from(_this.dequeue()); })).pipe(operators_1.map(function (datalogPacket, index) {
            try {
                return {
                    type: 'progress',
                    payload: {
                        index: index,
                        limit: undefined,
                        current: datalogPacket
                    }
                };
            }
            catch (err) {
                return {
                    type: 'error',
                    error: err
                };
            }
        }), operators_1.share());
    };
    return DataLogger;
}());
exports.DataLogger = DataLogger;
(function (DataLogger) {
    var CannotDecodePacketError = /** @class */ (function (_super) {
        __extends(CannotDecodePacketError, _super);
        function CannotDecodePacketError(singlePacket, cause) {
            var _this = _super.call(this, "Cannot parse data log packet. Error: " + cause.message) || this;
            _this.singlePacket = singlePacket;
            _this.cause = cause;
            return _this;
        }
        return CannotDecodePacketError;
    }(Error));
    DataLogger.CannotDecodePacketError = CannotDecodePacketError;
})(DataLogger = exports.DataLogger || (exports.DataLogger = {}));
exports.DataLogger = DataLogger;
/**
 *
 * @experimental
 */
var RawPacketConverter = /** @class */ (function () {
    function RawPacketConverter(tlvConverter, bundleConverter, singlePacketConverter) {
        if (tlvConverter === void 0) { tlvConverter = new tlv_converter_1.TLVConverter(); }
        if (bundleConverter === void 0) { bundleConverter = new TlvBundleConverter_1.TlvBundleConverter(); }
        if (singlePacketConverter === void 0) { singlePacketConverter = new converter_1.SinglePacketConverter(); }
        this.tlvConverter = tlvConverter;
        this.bundleConverter = bundleConverter;
        this.singlePacketConverter = singlePacketConverter;
    }
    RawPacketConverter.prototype.encode = function (data) {
        throw new Error("Method not implemented.");
    };
    RawPacketConverter.prototype.decode = function (data) {
        var tlvTree = this.tlvConverter.decode(data);
        var bundleId = tlvTree.header.id;
        // console.log(tlvTree);
        var variables = tlvTree.children ? this.bundleConverter.decode(tlvTree) : {};
        return {
            _raw: data,
            bundle: {
                id: bundleId
            },
            rawVariables: variables,
        };
    };
    return RawPacketConverter;
}());
exports.RawPacketConverter = RawPacketConverter;
/**
 *
 * @experimental
 */
var DataLogPacketConverter = /** @class */ (function () {
    function DataLogPacketConverter(meta, rawPacketConverter) {
        if (rawPacketConverter === void 0) { rawPacketConverter = new RawPacketConverter(); }
        this.meta = meta;
        this.rawPacketConverter = rawPacketConverter;
    }
    DataLogPacketConverter.createFromManager = function (bundleManager, variableManager) {
        var meta = [];
        var variableConfig = variableManager.variableConfig;
        for (var bundleId in bundleManager.bundles) {
            var bundle = bundleManager.get(bundleId);
            meta.push({
                id: bundle.bundleId,
                name: bundle.identifier,
                variables: variableConfig
            });
        }
        // debug('Created with meta', meta );
        return new DataLogPacketConverter(meta);
    };
    DataLogPacketConverter.createFromMeta = function (meta, rawPacketConverter) {
        if (rawPacketConverter === void 0) { rawPacketConverter = new RawPacketConverter(); }
        return new DataLogPacketConverter(meta, rawPacketConverter);
    };
    DataLogPacketConverter.prototype.encode = function (type) {
        throw new Error("Method not implemented.");
    };
    DataLogPacketConverter.prototype.decode = function (data) {
        var singlePacket = data.singlePacket;
        var dataLogPacket = this.rawPacketConverter.decode(singlePacket.payload.data);
        var timeOffset = Date.now() - (singlePacket.sendTime * 1000);
        dataLogPacket.bundle.logTime = new Date((singlePacket.payload.logTime * 1000) + timeOffset);
        var packet = populateDataLogPacket(dataLogPacket, this.meta || []);
        return {
            packet: packet,
            rawSinglePacket: data.raw,
            singlePacket: singlePacket
        };
    };
    return DataLogPacketConverter;
}());
exports.DataLogPacketConverter = DataLogPacketConverter;
/**
 *
 * @experimental
 */
function populateDataLogPacket(rawDataLogPacket, meta) {
    // console.log('decodeDataLogPacket', meta)
    var result = rawDataLogPacket;
    result.variables = {};
    var bundleMeta = meta.find(function (item) { return item.id == rawDataLogPacket.bundle.id; });
    if (bundleMeta) {
        var variableMeta = bundleMeta.variables;
        result.bundle.name = bundleMeta.name;
        var id_1;
        for (id_1 in rawDataLogPacket.rawVariables) {
            var data = rawDataLogPacket.rawVariables[id_1];
            var variableConfig = variableMeta.find(function (v) { return v.id == parseInt(id_1); });
            if (variableConfig) {
                var key = variableConfig.name || variableConfig.id.toString();
                debug('Converting key', id_1, key, data, variableConfig);
                result.variables[key] = variableConfig.converter ? variableConfig.converter.decode(data) : data; // Converts Buffer to a value according to variableConfig
            }
            else {
                debug('No converter available', id_1, data, variableConfig);
                result.variables[id_1] = data;
            }
        }
    }
    else {
        debug("NO BUNDLE META FOR VARIABLE ID \"" + rawDataLogPacket.bundle.id + "\". Available:", meta.map(function (item) { return item.id; }).join(','));
    }
    if (Object.keys(result.variables).length === 0) {
        result.variables = result.rawVariables;
    }
    return result;
}
exports.populateDataLogPacket = populateDataLogPacket;
