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 { getAbiObjectOwnership, getAbiERC721, getAbiERC721Collection } from "../../../utils/ethers/abiHelper";
import { pad0xBegin } from "../../../utils/common/utils";
import { triggerContract, viewContract, getContract } from "../../../utils/ethers/contractHelper";
/**
 * ERC721 Owner of
 * @param provider Provider
 * @param tokenAddress ERC721 contract address
 * @param tokenId Token Id
 * @param callback Callback
 * @returns The address of owner
 */
export const erc721OwnerOf = (provider, tokenAddress, tokenId, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenAddress, yield getAbiObjectOwnership());
    const results = yield viewContract(contract, "ownerOf", [tokenId], callback);
    return results.length ? results[0] : "";
});
/**
 * 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 ERC721 contract address
 * @param callback Callback
 * @returns any
 */
export const erc721IsApprovedForAll = (provider, owner, spender, tokenContractAddress, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenContractAddress, yield getAbiERC721Collection());
    const results = yield viewContract(contract, "isApprovedForAll", [owner, spender]);
    return results.length ? results[0] : false;
});
/**
 * Returns whether `spender` is allowed to manage `tokenId`.
 * @param provider Provider
 * @param spender The address that acts on behalf of the owner
 * @param tokenContractAddress The factory of tokenId.
 * @param tokenId ERC721 token Id;
 * @param callback Callback
 * @returns any
 */
export const erc721IsApprovedOrOwner = (provider, spender, tokenContractAddress, tokenId, callback) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    const contract = yield getContract(provider, tokenContractAddress, yield getAbiERC721());
    const owner = yield viewContract(contract, "ownerOf", [pad0xBegin(tokenId)]);
    if (!owner._owner) {
        throw new Error("erc721IsApprovedOrOwner::Token Id does not exist!");
    }
    const approvedAddress = yield viewContract(contract, "getApproved", [pad0xBegin(tokenId)]);
    const isApprovedForAll = yield erc721IsApprovedForAll(provider, owner._owner, spender, tokenContractAddress);
    return (((_a = owner._owner) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === spender.toLowerCase() ||
        ((_b = approvedAddress._approved) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === spender.toLowerCase() ||
        isApprovedForAll);
});
/**
 * Change or reaffirm the approved address for all of NFT
 * @param signer Signer
 * @param contractAddress The factory of tokenId.
 * @param spender The address that acts on behalf of the owner
 * @param approved Approved or Cancel Approved
 * @param callback Callback
 * @returns any
 */
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);
});
