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 { elements } from "../../../types";
import { triggerContractByContractName, viewContractByContractName, } from "../../../utils/ethers/contractHelper";
import { BigNumber, ethers } from "ethers";
const ElementRaffleMap = {
    gold: "goldElementRaffle",
    wood: "woodElementRaffle",
    water: "waterElementRaffle",
    fire: "fireElementRaffle",
    soil: "soilElementRaffle",
};
export const elementRaffleBalance = (landId, provider, element, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const [lands, apostles] = yield viewContractByContractName(landId, provider, ElementRaffleMap[element], "balanceOfEVO", [], callback);
    return {
        lands: BigNumber.from(lands).toString(),
        apostles: BigNumber.from(apostles).toString(),
    };
});
export const elementRaffleBalanceOfBatch = (landId, provider, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const results = yield Promise.all(elements.map((element) => elementRaffleBalance(landId, provider, element, callback)));
    return elements.reduce((result, element, index) => (Object.assign(Object.assign({}, result), { [element]: results[index] })), {});
});
export const elementRaffleElementTokenAddress = (landId, provider, element, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const [address] = yield viewContractByContractName(landId, provider, ElementRaffleMap[element], "element", [], callback);
    return address;
});
export const elementRaffleElementTokenAddressOfBatch = (landId, provider, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const addressBatch = yield Promise.all(elements.map((element) => elementRaffleElementTokenAddress(landId, provider, element, callback)));
    const result = {};
    elements.forEach((element, index) => {
        result[element] = addressBatch[index];
    });
    return result;
});
export const elementRaffleSmallDrawFee = (landId, provider, element, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const [smallDrawFee] = yield viewContractByContractName(landId, provider, ElementRaffleMap[element], "smallDrawFee", [], callback);
    return BigNumber.from(smallDrawFee).toString();
});
export const elementRaffleLargeDrawFee = (landId, provider, element, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const [largeDrawFee] = yield viewContractByContractName(landId, provider, ElementRaffleMap[element], "largeDrawFee", [], callback);
    return BigNumber.from(largeDrawFee).toString();
});
export const elementRaffleDrawFeeOfBatch = (landId, provider, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const drawFeeBatch = yield Promise.all(elements.map((element) => Promise.all([
        elementRaffleSmallDrawFee(landId, provider, element, callback),
        elementRaffleLargeDrawFee(landId, provider, element, callback),
    ])));
    const result = {};
    elements.forEach((element, index) => {
        const [smallDrawFee, largeDrawFee] = drawFeeBatch[index];
        result[element] = {
            smallDrawFee,
            largeDrawFee,
        };
    });
    return result;
});
export const elementRaffleSmallDraw = (landId, signer, element, callback) => {
    return triggerContractByContractName(landId, signer, ElementRaffleMap[element], "smallDraw", [], callback, {
        gasLimit: ethers.BigNumber.from("500000"),
    });
};
export const elementRaffleLargeDraw = (landId, signer, element, callback) => {
    return triggerContractByContractName(landId, signer, ElementRaffleMap[element], "largeDraw", [], callback, {
        gasLimit: ethers.BigNumber.from("500000"),
    });
};
