"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefiPositionTool = void 0;
const ApiHandler_1 = require("../helper/ApiHandler");
const defi_providers_json_1 = __importDefault(require("../static/defi_providers.json"));
const chain_IDs_json_1 = __importDefault(require("../static/chain_IDs.json"));
class DefiPositionTool {
    constructor(baseUrl = "https://zap.dzap.io/v1") {
        var _a;
        this.apiHandler = new ApiHandler_1.ApiHandler(baseUrl, {}, 10000, 2);
        const parsedProviders = defi_providers_json_1.default;
        this.providers = (_a = parsedProviders.defiProviders) !== null && _a !== void 0 ? _a : {};
        this.validProviderIds = new Set(Object.keys(this.providers));
        this.chainIdMap = chain_IDs_json_1.default;
        this.validChainIds = new Set(Object.values(this.chainIdMap));
    }
    assertValidProvider(providerId) {
        if (!this.validProviderIds.has(providerId)) {
            const sample = Array.from(this.validProviderIds).slice(0, 10).join(", ");
            throw new Error(`Invalid provider "${providerId}". Must be one of: ${sample}${this.validProviderIds.size > 10 ? ", ..." : ""}`);
        }
    }
    assertValidChainId(chainId) {
        if (!Number.isInteger(chainId)) {
            throw new Error(`Invalid chain "${chainId}". Chain ID must be an integer.`);
        }
        if (!this.validChainIds.has(chainId)) {
            const sample = Array.from(this.validChainIds).slice(0, 10).join(", ");
            throw new Error(`Invalid chain ID "${chainId}". Must be one of: ${sample}${this.validChainIds.size > 10 ? ", ..." : ""}`);
        }
    }
    assertValidAddress(address) {
        // Minimal sanity check. If you want checksum validation, use ethers.js.
        if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
            throw new Error(`Invalid address "${address}". Expected a 0x-prefixed 40-hex string.`);
        }
    }
    getAllProviders() {
        const providers = [];
        for (const providerId in this.providers) {
            providers.push(providerId);
        }
        return providers;
    }
    async getDefiPositions(address, providerId, chainId) {
        var _a;
        this.assertValidAddress(address);
        this.assertValidProvider(providerId);
        this.assertValidChainId(chainId);
        const provider = this.providers[providerId];
        const supported = (_a = provider.supportedChainIds) !== null && _a !== void 0 ? _a : [];
        // If the provider declares supported chains, enforce it.
        if (supported.length > 0 && !supported.includes(chainId)) {
            throw new Error(`Provider "${providerId}" does not support chain ID ${chainId}.`);
        }
        const qs = new URLSearchParams({
            provider: providerId,
            chainId: String(chainId),
            account: address,
        });
        const endpoint = `user/positions?${qs.toString()}`;
        return this.apiHandler.get(endpoint, {}, {});
    }
}
exports.DefiPositionTool = DefiPositionTool;
