import { v4 as uuid } from '@lukeed/uuid';
import dset from 'dset';
import Logger from '../logger';
import Stats from '../stats';
import { RemoteMetrics } from '../stats/remote-metrics';
var ContextCancelation = /** @class */ (function () {
    function ContextCancelation(options) {
        var _a, _b, _c;
        this.retry = (_a = options.retry) !== null && _a !== void 0 ? _a : true;
        this.type = (_b = options.type) !== null && _b !== void 0 ? _b : 'plugin Error';
        this.reason = (_c = options.reason) !== null && _c !== void 0 ? _c : '';
    }
    return ContextCancelation;
}());
export { ContextCancelation };
var remoteMetrics;
var Context = /** @class */ (function () {
    function Context(event, id) {
        this.logger = new Logger();
        this.cancel = function (error) {
            if (error) {
                throw error;
            }
            throw new ContextCancelation({ reason: 'Context Cancel' });
        };
        this._attempts = 0;
        this._event = event;
        this._id = id !== null && id !== void 0 ? id : uuid();
        this.stats = new Stats(remoteMetrics);
    }
    Context.initMetrics = function (options) {
        remoteMetrics = new RemoteMetrics(options);
    };
    Context.system = function () {
        return new Context({ type: 'track', event: 'system' });
    };
    Context.prototype.isSame = function (other) {
        return other._id === this._id;
    };
    Context.prototype.log = function (level, message, extras) {
        this.logger.log(level, message, extras);
    };
    Object.defineProperty(Context.prototype, "id", {
        get: function () {
            return this._id;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Context.prototype, "event", {
        get: function () {
            return this._event;
        },
        set: function (evt) {
            this._event = evt;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Context.prototype, "attempts", {
        get: function () {
            return this._attempts;
        },
        set: function (attempts) {
            this._attempts = attempts;
        },
        enumerable: false,
        configurable: true
    });
    Context.prototype.updateEvent = function (path, val) {
        dset(this._event, path, val);
        return this._event;
    };
    Context.prototype.logs = function () {
        return this.logger.logs;
    };
    Context.prototype.flush = function () {
        this.logger.flush();
        this.stats.flush();
    };
    Context.prototype.toJSON = function () {
        return {
            id: this._id,
            event: this._event,
            logs: this.logger.logs,
            metrics: this.stats.metrics,
        };
    };
    return Context;
}());
export { Context };
//# sourceMappingURL=index.js.map