"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MasterChefV2TemplateContractPositionFetcher = void 0;
const common_1 = require("@nestjs/common");
const app_toolkit_interface_1 = require("../../app-toolkit/app-toolkit.interface");
const address_1 = require("../../app-toolkit/constants/address");
const position_interface_1 = require("../../position/position.interface");
const master_chef_template_contract_position_fetcher_1 = require("./master-chef.template.contract-position-fetcher");
let MasterChefV2TemplateContractPositionFetcher = class MasterChefV2TemplateContractPositionFetcher extends master_chef_template_contract_position_fetcher_1.MasterChefTemplateContractPositionFetcher {
    constructor(appToolkit) {
        super(appToolkit);
        this.appToolkit = appToolkit;
    }
    getTokenDefinitions(params) {
        const _super = Object.create(null, {
            getTokenDefinitions: { get: () => super.getTokenDefinitions }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const { multicall, definition, contract } = params;
            const tokenDefinitions = yield _super.getTokenDefinitions.call(this, params);
            if (!tokenDefinitions)
                return null;
            const extraRewarderAddress = yield this.getExtraRewarder(contract, definition.poolIndex);
            if (extraRewarderAddress === address_1.ZERO_ADDRESS)
                return tokenDefinitions;
            const rewarderContract = multicall.wrap(this.getExtraRewarderContract(extraRewarderAddress));
            const extraRewardTokenAddresses = yield this.getExtraRewardTokenAddresses(rewarderContract, definition.poolIndex);
            tokenDefinitions.push(...extraRewardTokenAddresses.map(v => ({ metaType: position_interface_1.MetaType.CLAIMABLE, address: v, network: this.network })));
            return tokenDefinitions;
        });
    }
    getRewardRates(params) {
        const _super = Object.create(null, {
            getRewardRates: { get: () => super.getRewardRates }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const rewardRates = yield _super.getRewardRates.call(this, params);
            const { contract, definition, multicall } = params;
            const extraRewarderAddress = yield this.getExtraRewarder(contract, definition.poolIndex);
            const rewarderContract = multicall.wrap(this.getExtraRewarderContract(extraRewarderAddress));
            if (extraRewarderAddress === address_1.ZERO_ADDRESS)
                return rewardRates;
            const extraRewardRates = yield this.getExtraRewardTokenRewardRates(Object.assign(Object.assign({}, params), { rewarderContract }));
            return [...rewardRates, ...(Array.isArray(extraRewardRates) ? extraRewardRates : [extraRewardRates])];
        });
    }
    getDataProps(params) {
        const _super = Object.create(null, {
            getDataProps: { get: () => super.getDataProps }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const { definition, contract } = params;
            const dataProps = yield _super.getDataProps.call(this, params);
            const extraRewarderAddress = yield this.getExtraRewarder(contract, definition.poolIndex);
            return Object.assign(Object.assign({}, dataProps), { extraRewarderAddress: extraRewarderAddress.toLowerCase() });
        });
    }
    getTokenBalancesPerPosition(params) {
        const _super = Object.create(null, {
            getTokenBalancesPerPosition: { get: () => super.getTokenBalancesPerPosition }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const { contractPosition, multicall } = params;
            const { extraRewarderAddress } = contractPosition.dataProps;
            const tokenBalancesForPosition = yield _super.getTokenBalancesPerPosition.call(this, params);
            if (extraRewarderAddress === address_1.ZERO_ADDRESS)
                return tokenBalancesForPosition;
            const rewarderContract = multicall.wrap(this.getExtraRewarderContract(extraRewarderAddress));
            const extraResult = yield this.getExtraRewardTokenBalances(Object.assign(Object.assign({}, params), { rewarderContract }));
            const extraRewardBalances = Array.isArray(extraResult) ? extraResult : [extraResult];
            return [...tokenBalancesForPosition, ...extraRewardBalances];
        });
    }
};
MasterChefV2TemplateContractPositionFetcher = __decorate([
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __metadata("design:paramtypes", [Object])
], MasterChefV2TemplateContractPositionFetcher);
exports.MasterChefV2TemplateContractPositionFetcher = MasterChefV2TemplateContractPositionFetcher;
//# sourceMappingURL=master-chef-v2.template.contract-position-fetcher.js.map