"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatSignal = void 0;
const money = (value, currency = "USD") => {
    if (value === undefined)
        return "Unavailable";
    return new Intl.NumberFormat("en-US", {
        style: "currency",
        currency,
        maximumFractionDigits: value >= 1 ? 4 : 8
    }).format(value);
};
const escapeHtml = (value) => value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
const confidenceStars = (score) => {
    if (!score)
        return "";
    const full = Math.round(score);
    return "★".repeat(full) + "☆".repeat(Math.max(0, 10 - full));
};
const formatSignal = (signal) => {
    switch (signal.category) {
        case "Arbitrage Opportunities":
            return formatArbitrageSignal(signal);
        case "Farm Liquidity":
            return formatFarmLiquiditySignal(signal);
        case "Trading Options":
            return formatOptionsSignal(signal);
        case "Margin":
            return formatMarginSignal(signal);
        case "AI Strategy":
            return formatAIStrategySignal(signal);
        case "Forex":
            return formatForexSignal(signal);
        case "Stocks":
            return formatStockSignal(signal);
        default:
            return formatDefaultSignal(signal);
    }
};
exports.formatSignal = formatSignal;
const formatArbitrageSignal = (signal) => {
    const action = signal.side === "BUY" ? "Buy external → Sell on Lychee" : "Buy on Lychee → Sell external";
    return [
        "🦅 <b>Arbitrage Signal</b>",
        "",
        `🔹 Token: <b>${escapeHtml(signal.tokenName)} (${escapeHtml(signal.symbol)})</b>`,
        `🌐 Network: ${escapeHtml(signal.network ?? "Unknown")}`,
        `📊 Action: <b>${action}</b>`,
        `💵 Market price: <b>${money(signal.actualMarketPrice)}</b>`,
        `🏦 Lychee price: <b>${money(signal.platformPrice)}</b>`,
        `📈 Spread: <b>${signal.spreadPercentage?.toFixed(2)}%</b>`,
        `💰 Est. profit: <b>${signal.estimatedProfitPercentage?.toFixed(2)}%</b>`,
        `⏱ Window: ${escapeHtml(signal.executionWindow)}`,
        `💵 Suggested capital: <b>${money(signal.suggestedAmountUsd)}</b>`,
        signal.confidenceScore ? `🎯 Confidence: ${confidenceStars(signal.confidenceScore)} (${signal.confidenceScore}/10)` : undefined,
        "",
        signal.reasoning ? `📝 <i>${escapeHtml(signal.reasoning)}</i>` : undefined,
    ]
        .filter(Boolean)
        .join("\n");
};
const formatFarmLiquiditySignal = (signal) => {
    return [
        "🌾 <b>Farm Liquidity Signal</b>",
        "",
        `🔹 Pool: <b>${escapeHtml(signal.tokenName)} (${escapeHtml(signal.symbol)})</b>`,
        `🌐 Network: ${escapeHtml(signal.network ?? "Unknown")}`,
        `📊 Action: <b>${signal.side}</b>`,
        signal.apr !== undefined ? `📈 APR: <b>${signal.apr.toFixed(2)}%</b>` : undefined,
        signal.tvl !== undefined ? `💧 TVL: <b>$${signal.tvl.toLocaleString()}</b>` : undefined,
        signal.volume24h !== undefined ? `📊 24h Volume: <b>$${signal.volume24h.toLocaleString()}</b>` : undefined,
        signal.fee !== undefined ? `💸 Pool fee: <b>${signal.fee}%</b>` : undefined,
        `⚠️ IL risk: <b>${signal.impermanentLossRisk ?? signal.riskLevel}</b>`,
        `⏱ Expected duration: ${escapeHtml(signal.executionWindow)}`,
        `💵 Suggested capital: <b>${money(signal.suggestedAmountUsd)}</b>`,
        signal.confidenceScore ? `🎯 Confidence: ${confidenceStars(signal.confidenceScore)} (${signal.confidenceScore}/10)` : undefined,
        "",
        signal.reasoning ? `📝 <i>${escapeHtml(signal.reasoning)}</i>` : undefined,
    ]
        .filter(Boolean)
        .join("\n");
};
const formatOptionsSignal = (signal) => {
    return [
        "📈 <b>Options Signal</b>",
        "",
        `🔹 Asset: <b>${escapeHtml(signal.tokenName)} (${escapeHtml(signal.symbol)})</b>`,
        `🌐 Network: ${escapeHtml(signal.network ?? "Unknown")}`,
        `📊 Signal: <b>${signal.side} ${signal.side === "CALL" ? "📈" : "📉"}</b>`,
        `🎯 Strategy: <b>${escapeHtml(signal.strategyName ?? "Options")}</b>`,
        `💵 Spot price: <b>${money(signal.entryPrice)}</b>`,
        signal.strikePrice ? `⚡ Strike: <b>${money(signal.strikePrice)}</b>` : undefined,
        signal.breakEvenPrice ? `⚖️ Break-even: <b>${money(signal.breakEvenPrice)}</b>` : undefined,
        signal.premiumEstimate ? `💰 Premium estimate: <b>${money(signal.premiumEstimate)}</b>` : undefined,
        signal.optionExpiry ? `⏱ Expiry: ${escapeHtml(signal.optionExpiry)}` : undefined,
        `⚠️ Risk level: <b>${signal.riskLevel}</b>`,
        `💵 Suggested capital: <b>${money(signal.suggestedAmountUsd)}</b>`,
        signal.confidenceScore ? `🎯 Confidence: ${confidenceStars(signal.confidenceScore)} (${signal.confidenceScore}/10)` : undefined,
        "",
        signal.reasoning ? `📝 <i>${escapeHtml(signal.reasoning)}</i>` : undefined,
    ]
        .filter(Boolean)
        .join("\n");
};
const formatMarginSignal = (signal) => {
    return [
        "⚖️ <b>Margin Signal</b>",
        "",
        `🔹 Asset: <b>${escapeHtml(signal.tokenName)} (${escapeHtml(signal.symbol)})</b>`,
        `🌐 Network: ${escapeHtml(signal.network ?? "Unknown")}`,
        `📊 Position: <b>${signal.side} ${signal.side === "LONG" ? "🚀" : "🔻"}</b>`,
        `🎯 Strategy: <b>${escapeHtml(signal.strategyName ?? "Margin")}</b>`,
        `💵 Entry: <b>${money(signal.entryPrice)}</b>`,
        `🎯 Target: <b>${money(signal.targetPrice)}</b>`,
        signal.leverage ? `⚡ Leverage: <b>${signal.leverage}x</b>` : undefined,
        signal.marginRequired ? `💰 Margin required: <b>${money(signal.marginRequired)}</b>` : undefined,
        signal.liquidationEstimate ? `🚨 Liquidation est.: <b>${money(signal.liquidationEstimate)}</b>` : undefined,
        `⏱ Window: ${escapeHtml(signal.executionWindow)}`,
        `⚠️ Risk level: <b>${signal.riskLevel}</b>`,
        `💵 Position size: <b>${money(signal.suggestedAmountUsd)}</b>`,
        signal.confidenceScore ? `🎯 Confidence: ${confidenceStars(signal.confidenceScore)} (${signal.confidenceScore}/10)` : undefined,
        "",
        signal.reasoning ? `📝 <i>${escapeHtml(signal.reasoning)}</i>` : undefined,
    ]
        .filter(Boolean)
        .join("\n");
};
const formatAIStrategySignal = (signal) => {
    const pairs = signal.pairs?.length ? signal.pairs : buildAssetPairs(signal.symbol);
    return [
        "🤖 <b>AI Strategy Signal</b>",
        "",
        `🔹 Asset: <b>${escapeHtml(signal.tokenName)} (${escapeHtml(signal.symbol)})</b>`,
        `🧠 AI Strategy: <b>${escapeHtml(signal.aiStrategyName ?? signal.strategyName ?? "AI Strategy")}</b>`,
        pairs.length ? `🔗 Pairs: <b>${escapeHtml(pairs.join(", "))}</b>` : undefined,
        `⏱ Strategy window: ${escapeHtml(signal.executionWindow)}`,
        `⚠️ Risk level: <b>${signal.riskLevel}</b>`,
        `💵 Suggested capital: <b>${money(signal.suggestedAmountUsd)}</b>`,
        signal.confidenceScore ? `🎯 Confidence: ${confidenceStars(signal.confidenceScore)} (${signal.confidenceScore}/10)` : undefined,
        "",
        signal.reasoning ? `📝 <i>${escapeHtml(signal.reasoning)}</i>` : undefined,
    ]
        .filter(Boolean)
        .join("\n");
};
const buildAssetPairs = (symbol) => {
    const normalized = symbol.toUpperCase();
    const commonQuotes = ["USDT", "USDC", "BTC", "ETH", "BNB", "FDUSD"];
    const pairs = [];
    for (const quote of commonQuotes) {
        if (pairs.length >= 5)
            break;
        if (quote === normalized)
            continue;
        pairs.push(`${normalized}/${quote}`);
    }
    return pairs;
};
const formatDefaultSignal = (signal) => {
    return [
        `📡 <b>${escapeHtml(signal.category)} Signal</b>`,
        "",
        `Token: <b>${escapeHtml(signal.tokenName)} (${escapeHtml(signal.symbol)})</b>`,
        `Network: ${escapeHtml(signal.network ?? "Unknown")}`,
        `Strategy: <b>${escapeHtml(signal.strategyName ?? signal.category)}</b>`,
        `Entry price: <b>${money(signal.entryPrice)}</b>`,
        `Target price: <b>${money(signal.targetPrice)}</b>`,
        `Risk level: <b>${signal.riskLevel}</b>`,
        `Suggested amount: <b>${money(signal.suggestedAmountUsd)}</b>`,
        `Execution window: ${escapeHtml(signal.executionWindow)}`,
    ]
        .filter(Boolean)
        .join("\n");
};
const formatForexSignal = (signal) => {
    return [
        `💱 <b>Forex Signal — ${escapeHtml(signal.tokenName)}</b>`,
        "",
        `🔹 Pair: <b>${escapeHtml(signal.symbol)}</b>`,
        `📊 Direction: <b>${signal.side} ${signal.side === "BUY" ? "📈" : "📉"}</b>`,
        `🎯 Strategy: <b>${escapeHtml(signal.strategyName ?? "Forex")}</b>`,
        `💵 Current price: <b>${signal.entryPrice.toFixed(5)}</b>`,
        signal.predictedPrice ? `🔮 Predicted price: <b>${signal.predictedPrice.toFixed(5)}</b>` : undefined,
        `🚪 Entry: <b>${signal.entryPrice.toFixed(5)}</b>`,
        `🏁 Exit / Take Profit: <b>${signal.takeProfit?.toFixed(5) ?? signal.targetPrice.toFixed(5)}</b>`,
        signal.stopLoss ? `🛑 Stop Loss: <b>${signal.stopLoss.toFixed(5)}</b>` : undefined,
        signal.lotSize ? `📏 Lot size: <b>${signal.lotSize}</b>` : undefined,
        signal.pipValue ? `📍 Pip value: <b>${signal.pipValue}</b>` : undefined,
        `⏱ Expected duration: ${escapeHtml(signal.executionWindow)}`,
        `⚠️ Risk level: <b>${signal.riskLevel}</b>`,
        `💵 Suggested capital: <b>${money(signal.suggestedAmountUsd)}</b>`,
        signal.confidenceScore ? `🎯 Confidence: ${confidenceStars(signal.confidenceScore)} (${signal.confidenceScore}/10)` : undefined,
        "",
        signal.reasoning ? `📝 <i>${escapeHtml(signal.reasoning)}</i>` : undefined,
    ]
        .filter(Boolean)
        .join("\n");
};
const formatStockSignal = (signal) => {
    const currency = signal.currency ?? "USD";
    return [
        "📊 <b>Stock Signal</b>",
        "",
        `🔹 Company: <b>${escapeHtml(signal.tokenName)} (${escapeHtml(signal.symbol)})</b>`,
        signal.exchange ? `🏛 Exchange: <b>${escapeHtml(signal.exchange)}</b>` : undefined,
        signal.sector ? `🏭 Sector: <b>${escapeHtml(signal.sector)}</b>` : undefined,
        `📊 Direction: <b>${signal.side} ${signal.side === "BUY" ? "📈" : "📉"}</b>`,
        `🎯 Strategy: <b>${escapeHtml(signal.strategyName ?? "Stocks")}</b>`,
        `💵 Entry: <b>${money(signal.entryPrice, currency)}</b>`,
        `🎯 Target: <b>${money(signal.targetPrice, currency)}</b>`,
        signal.marketCap ? `💰 Market cap: <b>${money(signal.marketCap, currency)}</b>` : undefined,
        `⏱ Window: ${escapeHtml(signal.executionWindow)}`,
        `⚠️ Risk level: <b>${signal.riskLevel}</b>`,
        `💵 Suggested capital: <b>${money(signal.suggestedAmountUsd)}</b>`,
        signal.confidenceScore ? `🎯 Confidence: ${confidenceStars(signal.confidenceScore)} (${signal.confidenceScore}/10)` : undefined,
        "",
        signal.isSimulated ? "⚠️ <i>Live quote unavailable for this market; prices shown are estimated reference values.</i>" : undefined,
        signal.reasoning ? `📝 <i>${escapeHtml(signal.reasoning)}</i>` : undefined,
    ]
        .filter(Boolean)
        .join("\n");
};
