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 { ethers } from "ethers";
import { triggerContract, viewContract, getContract } from "../../../utils/ethers/contractHelper";
import ERC1155ABI from "../../../config/abi/common/abi-erc1155.json";
/**
 * Query if an address is an authorized operator for another address
 * @param provider Provider
 * @param owner The address that owns the NFTs
 * @param spender The address that acts on behalf of the owner
 * @param tokenContractAddress ERC1155 contract address
 * @param callback Callback
 * @returns any
 */
export const erc1155IsApprovedForAll = (provider, owner, spender, tokenContractAddress, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenContractAddress, ERC1155ABI);
    const results = yield viewContract(contract, "isApprovedForAll", [owner, spender]);
    return results.length ? results[0] : false;
});
/**
 * Get the balance of an account's tokens.
 * @param provider Provider
 * @param owner The address of the token holder
 * @param tokenId ID of the token
 * @param tokenContractAddress ERC1155 contract address
 * @param callback Callback
 * @returns The _owner's balance of the token type requested
 */
export const erc1155BalanceOf = (provider, owner, tokenId, tokenContractAddress, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenContractAddress, ERC1155ABI);
    const results = yield viewContract(contract, "balanceOf", [owner, tokenId]);
    return results.length ? results[0].toHexString() : ethers.utils.hexValue(0);
});
/**
 * Get the balance of multiple account/token pairs
 * @param provider Provider
 * @param owners The addresses of the token holders
 * @param tokenIds ID of the tokens
 * @param tokenContractAddress ERC1155 contract address
 * @param callback Callback
 * @returns The _owner's balance of the token types requested (i.e. balance for each (owner, id) pair)
 */
export const erc1155BalanceOfBatch = (provider, owners, tokenIds, tokenContractAddress, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenContractAddress, ERC1155ABI);
    const results = yield viewContract(contract, "balanceOfBatch", [owners, tokenIds]);
    return results.length
        ? results[0].map((item) => {
            return item.toHexString();
        })
        : [ethers.utils.hexValue(0)];
});
/**
 * Enable or disable approval for a third party ("operator") to manage all of the caller's tokens.
 * @param signer Signer
 * @param contractAddress ERC1155 contract address
 * @param spender Address of authorized operator
 * @param approved Approved or Cancel Approved
 * @param callback Callback
 * @returns any
 */
export const erc1155SetApprovalForAll = (signer, contractAddress, spender, approved, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(signer, contractAddress, ERC1155ABI);
    return triggerContract(contract, "setApprovalForAll", [spender, approved], callback);
});
/**
 * Transfers `_values` amount(s) of `_ids` from the `_from` address to the `_to` address specified (with safety call).
 * @param signer Signer
 * @param contractAddress ERC1155 contract address
 * @param spender Address of authorized operator
 * @param approved Approved or Cancel Approved
 * @param callback Callback
 * @returns any
 */
export const erc1155SafeBatchTransferFrom = (signer, contractAddress, spender, approved, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(signer, contractAddress, ERC1155ABI);
    return triggerContract(contract, "safeBatchTransferFrom", [spender, approved], callback);
});
