"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var util_1 = require("../../../core/util");
var logger_1 = require("../../../logger");
var logger = logger_1.default('VariableMonitor');
var DefaultVariableMonitor = /** @class */ (function () {
    function DefaultVariableMonitor(updater) {
        this._options = {};
        this._updater = updater;
    }
    DefaultVariableMonitor.fromVariable = function (variable) {
        return new DefaultVariableMonitor({
            update: function (totalMs) {
                logger.debug("Request monitor update on " + variable.identifier());
                return variable.read();
            }
        });
    };
    DefaultVariableMonitor.prototype.start = function (options) {
        var _this = this;
        if (this._interval) {
            logger.warn("Variable monitor is already started");
            return this;
        }
        var period = options ? options.period || DefaultVariableMonitor.DEFAULT_PERIOD : DefaultVariableMonitor.DEFAULT_PERIOD;
        this._interval = rxjs_1.interval(period);
        if (!this._values) {
            this._values = new rxjs_1.Subject();
        }
        this._intervalSubscription = this._interval
            .subscribe(function (x) {
            _this.fetchNewValue(x);
        });
        this._emit('START');
        return this;
    };
    DefaultVariableMonitor.prototype.isStarted = function () {
        return !!this._intervalSubscription;
    };
    DefaultVariableMonitor.prototype.pause = function () {
        if (!this._intervalSubscription) {
            return this;
        }
        this._intervalSubscription.unsubscribe();
        this._interval = undefined;
        this._emit('PAUSE');
        return this;
    };
    DefaultVariableMonitor.prototype.stop = function () {
        this.pause();
        this._emit('STOP');
        if (this._values) {
            this._values.complete();
            this._values = undefined;
        }
        if (this._events) {
            this._events.complete();
            this._events = undefined;
        }
        this._previousValue = undefined;
        return this;
    };
    DefaultVariableMonitor.prototype.values = function () {
        if (!this._values) {
            this._values = new rxjs_1.Subject();
        }
        return this._values;
    };
    DefaultVariableMonitor.prototype.events = function () {
        if (!this._events) {
            this._events = new rxjs_1.Subject();
        }
        return this._events;
    };
    DefaultVariableMonitor.prototype.notifyNewValue = function (data) {
        if (!this.isStarted()) {
            logger.debug("notifyNewValue skipped has monitor is stopped");
            return;
        }
        if (this._options.forceChange || this.valueHasChanged(data)) {
            logger.debug('valueHasChanged', data, this._previousValue);
            this._previousValue = data;
            this._values.next(data);
            if (this._events) {
                this._events.next({
                    type: 'READ_SUCCESS',
                    payload: data
                });
            }
        }
        else {
            logger.debug('Value did not changed');
        }
    };
    DefaultVariableMonitor.prototype.fetchNewValue = function (count) {
        var _this = this;
        logger.debug('DefaultVariableMonitor', 'fetchNewValue', "Count=" + count); //. Updated fct=${this._updater.update}`)
        var promise = this
            ._updater.update(count);
        return promise
            .then(function (newValue) {
            _this.notifyNewValue(newValue);
        })
            .catch(function (error) {
            _this.notifyValueError(error);
        });
    };
    DefaultVariableMonitor.prototype.notifyValueError = function (error) {
        logger.debug('notifyValueError', error);
        if (!this.isStarted()) {
            return;
        }
        //this._values!.error(error);
        if (this._events) {
            this._events.next({
                type: 'READ_ERROR',
                payload: error
            });
        }
    };
    DefaultVariableMonitor.prototype.valueHasChanged = function (newValue) {
        return this._previousValue === undefined || !util_1.Util.deepEqual(this._previousValue, newValue);
    };
    DefaultVariableMonitor.prototype._emit = function (type) {
        if (!this._events) {
            this._events = new rxjs_1.Subject();
        }
        this._events.next({
            type: type,
            payload: undefined
        });
    };
    DefaultVariableMonitor.DEFAULT_PERIOD = 300;
    return DefaultVariableMonitor;
}());
exports.DefaultVariableMonitor = DefaultVariableMonitor;
