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 { BigNumber, ethers } from "ethers";
import { triggerContractByContractName, viewContractByContractName, } from "../../../utils/ethers/contractHelper";
import { pad0xBegin } from "../../../utils/common/utils";
/**
 * Covert material Id to token Id.
 * @param landId Land Id
 * @param provider Provider
 * @param id Material Id
 * @param callback Callback
 * @returns any
 */
export const materialIdEncode = (landId, provider, id, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const results = yield viewContractByContractName(landId, provider, "material", "encode", [id], callback);
    return results.length ? results[0].toHexString() : ethers.utils.hexValue(0);
});
/**
 * Batch get material balance
 * @param landId Land Id
 * @param provider Provider
 * @param accounts Array of address
 * @param materialIdList Material token ids
 * @param callback Callback
 * @returns any
 */
export const materialBalanceOfBatch = (landId, provider, accounts, materialIdList, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const [result] = (yield viewContractByContractName(landId, provider, "material", "balanceOfBatch", [accounts, materialIdList.map((materialId) => pad0xBegin(materialId))], callback)) || [];
    if (Array.isArray(result)) {
        return result.map((item) => (BigNumber.isBigNumber(item) ? BigNumber.from(item).toString() : "0"));
    }
    return new Array(materialIdList.length).fill("0");
});
/**
 * Batch transfer material
 * @param landId Land Id
 * @param signer Signer
 * @param from Material owner
 * @param to Recipient
 * @param materialIdList Array of material token Ids
 * @param amounts Array of transfer amounts
 * @param data Additional data
 * @param callback Callback
 * @returns any
 */
export const materialBatchTransfer = (landId, signer, from, to, materialIdList, amounts, data = "0x", callback) => {
    return triggerContractByContractName(landId, signer, "material", "safeBatchTransferFrom", [from, to, materialIdList.map((materialId) => pad0xBegin(materialId)), amounts, data], callback);
};
