"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SentioProvider = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
class SentioProvider {
    constructor(host, options) {
        this.host = host;
    }
    init(txnHash, chainId) {
        return __awaiter(this, void 0, void 0, function* () {
            this.chainId = chainId;
            const url = new URL("solidity/transaction_info", this.host);
            url.searchParams.append("txHash", txnHash);
            const resp = yield (0, cross_fetch_1.default)(url);
            this.transactionInfo = yield resp.json();
        });
    }
    send(payload, callback) {
        let response = {
            id: payload.id,
            jsonrpc: payload.jsonrpc,
        };
        switch (payload.method) {
            case "debug_traceTransaction":
                response.result = this.transactionInfo.trace;
                break;
            case "eth_getTransactionByHash":
                response.result = this.transactionInfo.transaction;
                break;
            case "eth_getTransactionReceipt":
                response.result = this.transactionInfo.transactionReceipt;
                break;
            case "eth_getBlockByNumber":
                response.result = this.transactionInfo.block;
                break;
            case "eth_chainId":
                response.result = this.chainId;
                break;
            case "eth_getCode":
                if (payload.params != null) {
                    const contract = payload.params[0];
                    const matchedKey = Object.keys(this.transactionInfo.code).find(c => c.toLowerCase() === contract);
                    if (matchedKey) {
                        response.result = this.transactionInfo.code[matchedKey];
                        break;
                    }
                }
                callback(new Error("Get code request param is null or matched key is null."));
                return;
            case "debug_storageRangeAt":
                try {
                    const url = new URL("solidity/storage_info", this.host);
                    url.searchParams.append("blockHash", payload.params[0]);
                    url.searchParams.append("txIdx", payload.params[1]);
                    url.searchParams.append("contractAddress", payload.params[2]);
                    url.searchParams.append("keyStart", payload.params[3]);
                    url.searchParams.append("maxResult", payload.params[4]);
                    (0, cross_fetch_1.default)(url).then(resp => resp.json().then(result => callback(null, result))).catch(r => callback(new Error(r), undefined));
                }
                catch (e) {
                    callback(e, undefined);
                    return;
                }
                break;
            default:
                callback(new Error(`${payload.method} is not supported.`), undefined);
                return;
        }
        callback(null, response);
    }
    supportsSubscriptions() {
        return false;
    }
    disconnect() { }
}
exports.SentioProvider = SentioProvider;
//# sourceMappingURL=sentio-provider.js.map