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());
    });
};
import { convert0xto41, getProvider } from "./utils";
import { getContractAddressAndAbiByContractName } from "../ethers/contractHelper";
export { getContractAddressAndAbiByContractName } from "../ethers/contractHelper";
export const getContract = (signerOrProvider, contractAddress, contractInterface) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const provider = getProvider(signerOrProvider);
        const contract = contractInterface
            ? yield provider.contract(contractInterface, convert0xto41(contractAddress))
            : yield provider.contract().at(convert0xto41(contractAddress));
        return contract;
    }
    catch (err) {
        console.error("failed to get contract. error:", err);
    }
});
export const getContractByContractName = (landId, signerOrProvider, contractName) => __awaiter(void 0, void 0, void 0, function* () {
    const contractAddrAndAbi = yield getContractAddressAndAbiByContractName(landId, signerOrProvider, contractName);
    const provider = getProvider(signerOrProvider);
    // const contract = contractAddrAndAbi.interface
    //   ? await provider.contract(contractAddrAndAbi.interface, contractAddrAndAbi.address)
    //   : await provider.contract().at(contractAddrAndAbi.address);
    const contract = provider.contract().at(contractAddrAndAbi.address);
    return contract;
});
const defaultCallback = {
    errorCallback: ({ error }) => {
        void error;
    },
    // responseCallback: ({ transactionHash }) => { console.info(transactionHash) },
    // successCallback: ({ transactionHash }) => { console.info(transactionHash) },
};
export const triggerContract = (contract, methodName, contractArgs = [], callback = defaultCallback, overrides) => __awaiter(void 0, void 0, void 0, function* () {
    const { errorCallback, responseCallback, successCallback } = callback;
    const hash = yield contract[methodName](...contractArgs).send(Object.assign({}, overrides));
    responseCallback && responseCallback({ transactionHash: hash });
    void successCallback;
    return { hash };
});
export const triggerContractByContractName = (landId, signerOrProvider, contractName, methodName, contractArgs = [], callback = defaultCallback, overrides) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContractByContractName(landId, signerOrProvider, contractName);
    return yield triggerContract(contract, methodName, contractArgs, callback, overrides);
    // if (contract) {
    //   await triggerContract(contract, methodName, contractArgs, callback, overrides);
    // } else {
    //   callback &&
    //     callback.errorCallback({
    //       error: new Error(
    //         `failed to trigger contract by contract name. contract undefined, contract name is ${contractName}`
    //       ),
    //     });
    // }
});
export const viewContract = (contract, methodName, contractArgs = [], callback = defaultCallback) => __awaiter(void 0, void 0, void 0, function* () {
    const { errorCallback } = callback;
    try {
        return yield contract[methodName](...contractArgs).call();
    }
    catch (err) {
        console.error(err);
        errorCallback({ error: err });
    }
    return [];
});
export const viewContractByContractName = (landId, tronWeb, contractName, methodName, contractArgs = [], callback = defaultCallback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContractByContractName(landId, tronWeb, contractName);
    if (contract) {
        return yield viewContract(contract, methodName, contractArgs, callback);
    }
    else {
        callback &&
            callback.errorCallback({
                error: new Error(`failed to view contract by contract name. contract undefined, contract name is ${contractName}`),
            });
        return [];
    }
});
