"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCoreSDK = void 0;
exports.applyMixins = applyMixins;
const biconomy_1 = require("../meta-tx/biconomy");
class BaseCoreSDK {
    /**
     * Creates an instance of `BaseCoreSDK`
     * @param args - Constructor args
     */
    constructor(opts) {
        this._errorsMap = new Map();
        this._web3Lib = opts.web3Lib;
        this._subgraphUrl = opts.subgraphUrl;
        this._protocolDiamond = opts.protocolDiamond;
        this._metadataStorage = opts.metadataStorage;
        this._theGraphStorage = opts.theGraphStorage;
        this._chainId = opts.chainId;
        this._metaTxConfig = opts.metaTx;
        this._lens = opts.lens;
        this._contracts = opts.contracts;
        this._uuid = crypto.randomUUID();
        this._getTxExplorerUrl = opts.getTxExplorerUrl;
    }
    assertAndGetMetaTxConfig(overrides = {}) {
        var _a, _b, _c, _d, _e, _f;
        const contractAddress = overrides.contractAddress || this._protocolDiamond;
        const metaTransactionMethod = overrides.metaTransactionMethod || "executeMetaTransaction";
        const metaTxRelayerUrl = ((_a = overrides.metaTxConfig) === null || _a === void 0 ? void 0 : _a.relayerUrl) || ((_b = this._metaTxConfig) === null || _b === void 0 ? void 0 : _b.relayerUrl);
        const metaTxApiKey = ((_c = overrides.metaTxConfig) === null || _c === void 0 ? void 0 : _c.apiKey) || ((_d = this._metaTxConfig) === null || _d === void 0 ? void 0 : _d.apiKey);
        // metaTxApiId is depending on the contract/method(=executeMetaTransaction) to be called with Biconomy
        const apiIds = (_e = this._metaTxConfig) === null || _e === void 0 ? void 0 : _e.apiIds[contractAddress.toLowerCase()];
        const metaTxApiId = ((_f = overrides.metaTxConfig) === null || _f === void 0 ? void 0 : _f.apiId) ||
            (apiIds && apiIds[metaTransactionMethod]);
        if (!(metaTxRelayerUrl && metaTxApiKey && metaTxApiId)) {
            throw new Error("CoreSDK not configured to relay meta transactions. Either pass in 'relayerUrl', 'apiKey' and 'apiId' during initialization OR as overrides arguments.");
        }
        return {
            metaTxRelayerUrl,
            metaTxApiId,
            metaTxApiKey,
            contractAddress
        };
    }
    assertAndGetMetaTxConfig2() {
        return __awaiter(this, arguments, void 0, function* (overrides = {}) {
            const ret = this.assertAndGetMetaTxConfig(overrides);
            // Check the meta-tx gateway is ready and accepts relaying transaction to the targeted contract
            const biconomy = new biconomy_1.Biconomy(ret.metaTxRelayerUrl, ret.metaTxApiKey, ret.metaTxApiId);
            const ready = yield biconomy.check({
                contract: ret.contractAddress
            });
            return ready ? ret : undefined;
        });
    }
}
exports.BaseCoreSDK = BaseCoreSDK;
// Doc: https://www.typescriptlang.org/docs/handbook/mixins.html#alternative-pattern
function applyMixins(derivedCtor, constructors) {
    constructors.forEach((baseCtor) => {
        Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
            Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
                Object.create(null));
        });
    });
}
//# sourceMappingURL=base-core-sdk.js.map