"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var network_1 = require("@harmony-js/network");
var decoder_1 = require("../utils/decoder");
var formatter_1 = require("../utils/formatter");
var EventMethod = /** @class */ (function () {
    function EventMethod(methodKey, params, abiItem, contract) {
        this.methodKey = methodKey;
        this.contract = contract;
        this.messenger = contract.wallet.messenger;
        this.params = params;
        this.abiItem = abiItem;
    }
    EventMethod.prototype.send = function () { };
    EventMethod.prototype.call = function () { };
    EventMethod.prototype.estimateGas = function () { };
    EventMethod.prototype.encodeABI = function () { };
    EventMethod.prototype.subscribe = function (options) {
        var _this = this;
        if (options &&
            typeof options.filter !== 'undefined' &&
            typeof options.topics !== 'undefined') {
            throw new Error('Invalid subscription options: Only filter or topics are allowed and not both');
        }
        if (this.emitter && this.messenger) {
            var messenger_1 = this.messenger;
            var emitter_1 = this.emitter;
            var inputOptions_1 = formatter_1.inputLogFormatter(options);
            // 1. getLog
            // 2. subscribe pastlog
            // 3. emit data
            messenger_1
                .send("hmy_getLogs" /* GetPastLogs */, [inputOptions_1])
                .then(function (logs) {
                logs.forEach(function (log) {
                    var formattedLog = _this.onNewSubscriptionItem(log);
                    emitter_1.emit('data', formattedLog);
                });
                messenger_1.subscribe('logs', [inputOptions_1] || []);
            })
                .catch(function (error) {
                emitter_1.emit('error', error);
            });
        }
        return this.contract;
        // return this.eventSubscriptionFactory
        //   .createEventLogSubscription(
        //     this.eventLogDecoder,
        //     this.contract,
        //     this.eventOptionsMapper.map(abiItemModel, this.contract, options),
        //     abiItemModel,
        //   )
        //   .subscribe(callback);
        // this.messenger.subscribe()
    };
    EventMethod.prototype.onNewSubscriptionItem = function (subscriptionItem) {
        // const log = outputLogFormatter(subscriptionItem);
        var log = decoder_1.decode(this.contract.abiCoder, this.abiItem, formatter_1.outputLogFormatter(subscriptionItem));
        if (log.removed && this.emitter) {
            this.emitter.emit('changed', log);
        }
        return log;
    };
    Object.defineProperty(EventMethod.prototype, "emitter", {
        get: function () {
            if (this.messenger && this.messenger.provider instanceof network_1.WSProvider) {
                return this.messenger.provider.emitter;
            }
            else {
                return undefined;
            }
        },
        enumerable: true,
        configurable: true
    });
    return EventMethod;
}());
exports.EventMethod = EventMethod;
//# sourceMappingURL=event.js.map