import { __assign, __awaiter, __extends, __generator } from "tslib";
import { SvmSpokeClient } from "@across-protocol/contracts";
import { getCurrentTime, bnZero, toAddressType, isDefined, } from "../../utils";
import { SVMSpokePoolClient } from "../SpokePoolClient";
import { MockSvmCpiEventsClient } from "./MockSvmCpiEventsClient";
import { unwrapEventData } from "../../arch/svm";
// This class replaces internal SpokePoolClient functionality, enabling
// the user to bypass on-chain queries and inject events directly.
var MockSvmSpokePoolClient = /** @class */ (function (_super) {
    __extends(MockSvmSpokePoolClient, _super);
    function MockSvmSpokePoolClient(logger, chainId, programId, deploymentBlock, eventSearchConfig, opts) {
        if (programId === void 0) { programId = SvmSpokeClient.SVM_SPOKE_PROGRAM_ADDRESS; }
        if (deploymentBlock === void 0) { deploymentBlock = 1; }
        if (eventSearchConfig === void 0) { eventSearchConfig = { from: 0, maxLookBack: 0 }; }
        if (opts === void 0) { opts = { hubPoolClient: null }; }
        var _this = _super.call(this, logger, opts.hubPoolClient, chainId, BigInt(deploymentBlock), eventSearchConfig, null, programId, null) || this;
        _this.destinationTokenForChainOverride = {};
        _this.mockEventsClient = new MockSvmCpiEventsClient();
        _this.latestHeightSearched = deploymentBlock;
        return _this;
    }
    MockSvmSpokePoolClient.prototype.setConfigStoreClient = function (configStore) {
        this.configStoreClient = configStore;
    };
    MockSvmSpokePoolClient.prototype.setDestinationTokenForChain = function (chainId, token) {
        this.destinationTokenForChainOverride[chainId] = token;
    };
    MockSvmSpokePoolClient.prototype.getDestinationTokenForDeposit = function (deposit) {
        var override = this.destinationTokenForChainOverride[deposit.originChainId];
        return isDefined(override) ? toAddressType(override, this.chainId) : _super.prototype.getDestinationTokenForDeposit.call(this, deposit);
    };
    MockSvmSpokePoolClient.prototype.setLatestBlockNumber = function (blockNumber) {
        this.latestHeightSearched = blockNumber;
    };
    MockSvmSpokePoolClient.prototype._update = function (eventsToQuery) {
        var _a;
        return __awaiter(this, void 0, void 0, function () {
            var from, to, events, eventsWithBlockNumber;
            var _this = this;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        from = (_a = this.eventSearchConfig.from) !== null && _a !== void 0 ? _a : this.deploymentBlock;
                        to = this.eventSearchConfig.to ? BigInt(this.eventSearchConfig.to) : undefined;
                        return [4 /*yield*/, Promise.all(eventsToQuery.map(function (eventName) { return _this.mockEventsClient.queryEvents(eventName, BigInt(from), to); }))];
                    case 1:
                        events = _b.sent();
                        eventsWithBlockNumber = events.map(function (eventList) {
                            return eventList.map(function (event) {
                                return __assign({ txnRef: event.signature, blockNumber: Number(event.slot), txnIndex: 0, logIndex: 0 }, unwrapEventData(event.data));
                            });
                        });
                        return [2 /*return*/, Promise.resolve({
                                success: true,
                                firstDepositId: bnZero,
                                currentTime: getCurrentTime(),
                                events: eventsWithBlockNumber,
                                searchEndBlock: this.eventSearchConfig.to || this.latestHeightSearched,
                            })];
                }
            });
        });
    };
    MockSvmSpokePoolClient.prototype.deposit = function (deposit) {
        return this.mockEventsClient.deposit(deposit);
    };
    MockSvmSpokePoolClient.prototype.fillRelay = function (fill) {
        return this.mockEventsClient.fillRelay(fill);
    };
    MockSvmSpokePoolClient.prototype.requestSlowFill = function (request) {
        return this.mockEventsClient.requestSlowFill(request);
    };
    MockSvmSpokePoolClient.prototype.setTokensBridged = function (_tokensBridged) {
        throw new Error("MockSvmSpokePoolClient#setTokensBridged not implemented");
    };
    MockSvmSpokePoolClient.prototype.executeSlowRelayLeaf = function (_leaf) {
        throw new Error("MockSvmSpokePoolClient#executeV3SlowRelayLeaf not implemented");
    };
    MockSvmSpokePoolClient.prototype.executeRelayerRefundLeaf = function (_refund) {
        throw new Error("MockSvmSpokePoolClient#executeRelayerRefundLeaf not implemented");
    };
    MockSvmSpokePoolClient.prototype.setEnableRoute = function (_originToken, _destinationChainId, _enabled, _overrides) {
        if (_overrides === void 0) { _overrides = {}; }
        throw new Error("MockSvmSpokePoolClient#setEnableRoute not implemented");
    };
    return MockSvmSpokePoolClient;
}(SVMSpokePoolClient));
export { MockSvmSpokePoolClient };
//# sourceMappingURL=MockSvmSpokePoolClient.js.map