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 { getAddressesByLandId } from "../../../utils/ethers/addressHelper";
import { pad0xBegin } from "../../../utils/common/utils";
import { toHexAndPadLeft, padLeft } from "../../../utils/ethers/utils";
import { getContractAddressAndAbiByContractName } from "../../../utils/tronweb/contractHelper";
import { triggerContractByContractName, viewContractByContractName } from "../../../utils/tronweb/contractHelper";
import { LandId } from "../../../config/constants";
import { convert41to0x, resultToHexNumber } from "../../../utils/tronweb/utils";
/**
 * Land Bid With Token
 * @param signer Ethers signer
 * @param landTokenId Land token id
 * @param referrer Referrer
 * @param amount Amount of price in RING token
 * @param callback Callback
 */
export const landBidWithToken = (landId, signer, landTokenId, referrer, amount, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const finalReferrer = referrer && ethers.utils.isAddress(referrer) ? referrer : "0x0000000000000000000000000000000000000000";
    if (landId === LandId.TRON || landId === LandId._TRON) {
        const data = finalReferrer
            ? `${pad0xBegin(landTokenId)}${ethers.utils.hexZeroPad(finalReferrer, 32).slice(2)}`
            : pad0xBegin(landTokenId);
        const addresses = getAddressesByLandId(landId);
        if (!addresses || !addresses.LAND_CLOCK_AUCTION) {
            throw "address error";
        }
        return triggerContractByContractName(landId, signer, "ring", "transferAndFallback(address,uint256,bytes)", [addresses.LAND_CLOCK_AUCTION, amount, data], callback);
    }
    return triggerContractByContractName(landId, signer, "landClockAuctionV3", "bidWithToken", [pad0xBegin(landTokenId), finalReferrer, amount], callback);
});
/**
 * Land Available Resources
 * @param provider Ethers provider
 * @param tokenId Token id
 * @param resources Resources
 * @param callback Callback
 * @returns String[]
 */
export const landGetAvailableResources = (landId, provider, tokenId, resources, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const addresses = getAddressesByLandId(landId);
    if (!addresses) {
        return [];
    }
    const finalRes = resources.length
        ? [...resources]
        : [
            addresses.ELEMENT_GOLD,
            addresses.ELEMENT_WOOD,
            addresses.ELEMENT_WATER,
            addresses.ELEMENT_FIRE,
            addresses.ELEMENT_SOIL,
        ];
    const results = yield viewContractByContractName(landId, provider, "apostleLandResource", "availableResources", [pad0xBegin(tokenId), finalRes.map((address) => convert41to0x(address))], callback);
    return results.map((res) => {
        return res.toString();
    });
});
/**
 * Land Batch Claim Land Resource
 * @param signer Ethers signer
 * @param tokenIds Token ids
 * @param callback Callback
 */
export const landBatchClaimLandResource = (landId, signer, tokenIds, callback) => {
    return triggerContractByContractName(landId, signer, "apostleLandResource", "batchClaimLandResource", [tokenIds.map((tokenId) => pad0xBegin(tokenId))], callback);
};
/**
 * Land Batch Claim Land Resource
 * @param signer Ethers signer
 * @param tokenIds Token ids
 * @param callback Callback
 */
export const landClaimLandResource = (landId, signer, tokenId, callback) => {
    return triggerContractByContractName(landId, signer, "apostleLandResource", "claimAllResource", [pad0xBegin(tokenId)], callback);
};
/**
 * Returns the current price of an auction.
 * @param provider Ethers signer
 * @param tokenId Token Id
 * @param callback Callback
 * @returns any
 */
export const landGetCurrentPriceByTokenId = (landId, provider, tokenId, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const results = yield viewContractByContractName(landId, provider, "landClockAuctionV3", "getCurrentPriceInToken", [pad0xBegin(tokenId)], callback);
    return resultToHexNumber(results);
});
/**
 * Sell land asset
 * @param signer Ethers signer
 * @param from
 * @param tokenId - Land tokenId
 * @param start - start price
 * @param end - end price
 * @param duration - bid duration time in second
 * @param callback Callback
 * @returns {Promise<void>}
 */
export const landAskWithToken = (landId, signer, from, tokenId, startPrice, endPrice, duration, callback) => {
    const _from = padLeft(from.slice(2), 64, "0");
    const _start = toHexAndPadLeft(startPrice).slice(2);
    const _end = toHexAndPadLeft(endPrice).slice(2);
    const _duration = toHexAndPadLeft(duration).slice(2);
    const data = `0x${_start}${_end}${_duration}${_from}`;
    const landClockAuctionContract = getContractAddressAndAbiByContractName(landId, signer, "landClockAuctionV3");
    return triggerContractByContractName(landId, signer, "objectOwnership", "approveAndCall", [landClockAuctionContract.address, pad0xBegin(tokenId), data], callback);
};
export const landCancelAskWithToken = (landId, signer, tokenId, callback) => {
    return triggerContractByContractName(landId, signer, "landClockAuctionV3", "cancelAuction", [pad0xBegin(tokenId)], callback);
};
export const landGetAvailableItemResources = (landId, provider, porpsContractAddress, tokenId, resourcesContractAddress, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const results = yield viewContractByContractName(landId, provider, "apostleLandResource", "availableItemResources", [porpsContractAddress, pad0xBegin(tokenId), resourcesContractAddress], callback);
    return results.map((res) => res.toString());
});
export const landBatchClaimItemResource = (landId, signer, drillContractAddress, tokenIds, callback) => {
    return triggerContractByContractName(landId, signer, "apostleLandResourceV2", "batchClaimItemResource", [drillContractAddress, tokenIds.map((tokenId) => pad0xBegin(tokenId))], callback);
};
export const landClaimItemResource = (landId, signer, drillContractAddress, tokenId, callback) => {
    return triggerContractByContractName(landId, signer, "apostleLandResource", "claimItemResource", [drillContractAddress, pad0xBegin(tokenId)], callback);
};
export const landTransfer = (landId, signer, from, to, tokenId, callback) => {
    return triggerContractByContractName(landId, signer, "objectOwnership", "transferFrom", [from, to, pad0xBegin(tokenId)], callback);
};
export const landClaimLand = (landId, signer, landTokenId, callback) => {
    return triggerContractByContractName(landId, signer, "landClockAuctionV3", "claimLandAsset", [pad0xBegin(landTokenId)], callback);
};
