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 { getAbiERC721Collection } from "../../../utils/ethers/abiHelper";
import { pad0xBegin } from "../../../utils/common/utils";
import { triggerContract, viewContract, getContract } from "../../../utils/tronweb/contractHelper";
import { convert41to0x } from "../../../utils/tronweb/utils";
export const erc721OwnerOf = (provider, tokenAddress, tokenId, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenAddress);
    const results = yield viewContract(contract, "ownerOf", [tokenId], callback);
    return Array.isArray(results) ? "" : results;
});
export const erc721IsApprovedForAll = (provider, owner, spender, tokenContractAddress, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenContractAddress);
    const results = yield viewContract(contract, "isApprovedForAll", [convert41to0x(owner), convert41to0x(spender)]);
    return Array.isArray(results) ? "" : results;
});
export const erc721IsApprovedOrOwner = (provider, spender, tokenContractAddress, tokenId, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenContractAddress);
    const owner = yield viewContract(contract, "ownerOf", [pad0xBegin(tokenId)]);
    if (Array.isArray(owner)) {
        throw new Error("erc721IsApprovedOrOwner::Token Id does not exist!");
    }
    const approvedAddress = yield viewContract(contract, "getApproved", [pad0xBegin(tokenId)]);
    const isApprovedForAll = yield erc721IsApprovedForAll(provider, convert41to0x(owner), convert41to0x(spender), convert41to0x(tokenContractAddress));
    return (convert41to0x(owner).toLowerCase() === convert41to0x(spender).toLowerCase() ||
        convert41to0x(approvedAddress).toLowerCase() === convert41to0x(spender).toLowerCase() ||
        isApprovedForAll);
});
export const erc721SetApprovalForAll = (signer, contractAddress, spender, approved, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(signer, contractAddress, yield getAbiERC721Collection());
    return triggerContract(contract, "setApprovalForAll", [spender, approved], callback);
});
