"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.CustomContractPositionTemplatePositionFetcher = void 0;
const common_1 = require("@nestjs/common");
const lodash_1 = require("lodash");
const app_toolkit_interface_1 = require("../../app-toolkit/app-toolkit.interface");
const display_item_present_1 = require("../../app-toolkit/helpers/presentation/display-item.present");
const image_present_1 = require("../../app-toolkit/helpers/presentation/image.present");
const contract_interface_1 = require("../../position/contract.interface");
const position_utils_1 = require("../../position/position.utils");
let CustomContractPositionTemplatePositionFetcher = class CustomContractPositionTemplatePositionFetcher {
    constructor(appToolkit) {
        this.appToolkit = appToolkit;
        this.isExcludedFromBalances = false;
        this.isExcludedFromExplore = false;
        this.isExcludedFromTvl = false;
    }
    getDataProps(_params) {
        return __awaiter(this, void 0, void 0, function* () {
            return {};
        });
    }
    getLabelDetailed(_params) {
        return __awaiter(this, void 0, void 0, function* () {
            return undefined;
        });
    }
    getSecondaryLabel(_params) {
        return __awaiter(this, void 0, void 0, function* () {
            return undefined;
        });
    }
    getTertiaryLabel({ contractPosition }) {
        return __awaiter(this, void 0, void 0, function* () {
            if (typeof contractPosition.dataProps.apy === 'number')
                return `${contractPosition.dataProps.apy.toFixed(3)}% APY`;
            return undefined;
        });
    }
    getImages({ contractPosition }) {
        return __awaiter(this, void 0, void 0, function* () {
            return contractPosition.tokens.flatMap(v => (0, image_present_1.getImagesFromToken)(v));
        });
    }
    getStatsItems({ contractPosition }) {
        return __awaiter(this, void 0, void 0, function* () {
            const statsItems = [];
            if (typeof contractPosition.dataProps.liquidity === 'number' && Math.abs(contractPosition.dataProps.liquidity) > 0)
                statsItems.push({ label: 'Liquidity', value: (0, display_item_present_1.buildDollarDisplayItem)(contractPosition.dataProps.liquidity) });
            if (typeof contractPosition.dataProps.apy === 'number' && contractPosition.dataProps.apy > 0)
                statsItems.push({ label: 'APY', value: (0, display_item_present_1.buildPercentageDisplayItem)(contractPosition.dataProps.apy) });
            return statsItems;
        });
    }
    getPositions() {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(this.network);
            const tokenLoader = this.appToolkit.getTokenDependencySelector({
                tags: { network: this.network, context: `${this.appId}__template` },
            });
            const definitions = yield this.getDefinitions({ multicall, tokenLoader });
            const skeletons = yield Promise.all(definitions.map((definition) => __awaiter(this, void 0, void 0, function* () {
                const address = definition.address.toLowerCase();
                const contract = multicall.wrap(this.getContract(definition.address));
                const context = { address, contract, definition, multicall, tokenLoader };
                const maybeTokenDefinitions = yield this.getTokenDefinitions(context);
                if (!maybeTokenDefinitions)
                    return null;
                const tokenDefinitionsArr = Array.isArray(maybeTokenDefinitions)
                    ? maybeTokenDefinitions
                    : [maybeTokenDefinitions];
                const tokenDefinitions = tokenDefinitionsArr.map(t => (Object.assign(Object.assign({}, t), { address: t.address.toLowerCase() })));
                return { address, definition, tokenDefinitions };
            })));
            const underlyingTokenRequests = (0, lodash_1.compact)(skeletons)
                .flatMap(v => v.tokenDefinitions)
                .map(v => ({ address: v.address, network: v.network, tokenId: v.tokenId }));
            const tokenDependencies = yield tokenLoader.getMany(underlyingTokenRequests).then(tokenDeps => (0, lodash_1.compact)(tokenDeps));
            const skeletonsWithResolvedTokens = yield Promise.all((0, lodash_1.compact)(skeletons).map(({ address, tokenDefinitions, definition }) => __awaiter(this, void 0, void 0, function* () {
                const maybeTokens = tokenDefinitions.map(v => {
                    const match = tokenDependencies.find(t => t.address === v.address);
                    return match ? (0, position_utils_1.metatyped)(match, v.metaType) : null;
                });
                const tokens = (0, lodash_1.compact)(maybeTokens);
                if (maybeTokens.length !== tokens.length)
                    return null;
                return { address, tokens, definition };
            })));
            const tokens = yield Promise.all((0, lodash_1.compact)(skeletonsWithResolvedTokens).map(({ address, tokens, definition }) => __awaiter(this, void 0, void 0, function* () {
                const contract = multicall.wrap(this.getContract(address));
                const baseContext = { address, contract, multicall, tokenLoader, definition };
                const baseFragment = {
                    type: contract_interface_1.ContractType.POSITION,
                    appId: this.appId,
                    groupId: this.groupId,
                    network: this.network,
                    address,
                    tokens,
                };
                const dataPropsStageParams = Object.assign(Object.assign({}, baseContext), { contractPosition: baseFragment });
                const dataProps = yield this.getDataProps(dataPropsStageParams);
                const displayPropsStageFragment = Object.assign(Object.assign({}, baseFragment), { dataProps });
                const displayPropsStageParams = Object.assign(Object.assign({}, dataPropsStageParams), { contractPosition: displayPropsStageFragment });
                const displayProps = {
                    label: yield this.getLabel(displayPropsStageParams),
                    labelDetailed: yield this.getLabelDetailed(displayPropsStageParams),
                    secondaryLabel: yield this.getSecondaryLabel(displayPropsStageParams),
                    tertiaryLabel: yield this.getTertiaryLabel(displayPropsStageParams),
                    images: yield this.getImages(displayPropsStageParams),
                    statsItems: yield this.getStatsItems(displayPropsStageParams),
                };
                const contractPosition = Object.assign(Object.assign({}, baseFragment), { dataProps, displayProps });
                const key = this.appToolkit.getPositionKey(contractPosition);
                return Object.assign({ key }, contractPosition);
            })));
            return tokens;
        });
    }
};
CustomContractPositionTemplatePositionFetcher = __decorate([
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __metadata("design:paramtypes", [Object])
], CustomContractPositionTemplatePositionFetcher);
exports.CustomContractPositionTemplatePositionFetcher = CustomContractPositionTemplatePositionFetcher;
//# sourceMappingURL=custom-contract-position.template.position-fetcher.js.map