"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValerisPlatform = exports.valerisNetworkSelectionMenu = exports.signalsMenu = exports.countryCodeToName = exports.stocksScopeMenu = exports.stocksCountryMenu = exports.parseStocksScopeCallback = exports.stocksScopeCallback = exports.parseStocksCountryCallback = exports.stocksCountryCallback = exports.forexActionMenu = exports.forexTypeMenu = exports.parseForexSelectCallback = exports.forexSelectCallback = exports.parseForexRandomCallback = exports.forexRandomCallback = exports.parseForexTypeCallback = exports.forexTypeCallback = exports.parseMarketCallback = exports.marketCallback = exports.mainMenu = exports.platformsKeyboard = exports.parseCategoryCallback = exports.categoryCallback = exports.parseValerisNetworkCallback = exports.valerisNetworkCallback = exports.mySubscriptionCallback = exports.subscribePlatformCallback = exports.platformCallback = exports.supportButton = exports.SUPPORT_CALLBACK = exports.CALLBACKS = void 0;
const telegraf_1 = require("telegraf");
const types_js_1 = require("../domain/subscriptions/types.js");
const ValerisNetwork_js_1 = require("../domain/value-objects/ValerisNetwork.js");
exports.CALLBACKS = {
    tradingOptions: "signal:trading-options",
    margin: "signal:margin",
    aiStrategy: "signal:ai-strategy",
    arbitrage: "signal:arbitrage",
    farmLiquidity: "signal:farm-liquidity"
};
exports.SUPPORT_CALLBACK = "support:contact";
const supportButton = (supportUrl) => supportUrl
    ? telegraf_1.Markup.button.url("🆘 Support", supportUrl)
    : telegraf_1.Markup.button.callback("🆘 Support", exports.SUPPORT_CALLBACK);
exports.supportButton = supportButton;
const platformCallback = (code) => `platform:${code}`;
exports.platformCallback = platformCallback;
const subscribePlatformCallback = (code) => `subscription:subscribe-platform:${code}`;
exports.subscribePlatformCallback = subscribePlatformCallback;
const mySubscriptionCallback = (code) => `subscription:status:${code}`;
exports.mySubscriptionCallback = mySubscriptionCallback;
const valerisNetworkCallback = (networkCode) => `lychee-finance:${networkCode}`;
exports.valerisNetworkCallback = valerisNetworkCallback;
const parseValerisNetworkCallback = (data) => {
    if (!data.startsWith("lychee-finance:"))
        return undefined;
    return data.replace("lychee-finance:", "");
};
exports.parseValerisNetworkCallback = parseValerisNetworkCallback;
const categoryCallback = (category, platformCode, networkCode) => networkCode ? `signal:${category}:${platformCode}:${networkCode}` : `signal:${category}:${platformCode}`;
exports.categoryCallback = categoryCallback;
const parseCategoryCallback = (data) => {
    const parts = data.split(":");
    if (parts.length < 3 || parts[0] !== "signal")
        return undefined;
    if (parts.length === 3) {
        return { category: parts[1], platformCode: parts[2] };
    }
    return { category: parts[1], platformCode: parts[2], networkCode: parts[3] };
};
exports.parseCategoryCallback = parseCategoryCallback;
const platformsKeyboard = (supportUrl) => telegraf_1.Markup.inlineKeyboard([
    ...types_js_1.PLATFORM_OPTIONS.filter((p) => p.code !== "forex" && p.code !== "stocks").map((platform) => [
        telegraf_1.Markup.button.callback(platform.name, (0, exports.platformCallback)(platform.code))
    ]),
    [(0, exports.supportButton)(supportUrl)],
    [telegraf_1.Markup.button.callback("🔙 Back to Markets", (0, exports.marketCallback)("back"))]
]);
exports.platformsKeyboard = platformsKeyboard;
const mainMenu = (supportUrl) => telegraf_1.Markup.inlineKeyboard([
    [telegraf_1.Markup.button.callback("💱 Forex", "market:forex")],
    [telegraf_1.Markup.button.callback("₿ Crypto", "market:crypto")],
    [telegraf_1.Markup.button.callback("📊 Stocks", "market:stocks")],
    [(0, exports.supportButton)(supportUrl)]
]);
exports.mainMenu = mainMenu;
const marketCallback = (market) => `market:${market}`;
exports.marketCallback = marketCallback;
const parseMarketCallback = (data) => {
    if (!data.startsWith("market:"))
        return undefined;
    return data.replace("market:", "");
};
exports.parseMarketCallback = parseMarketCallback;
const forexTypeCallback = (type) => `forex:type:${type}`;
exports.forexTypeCallback = forexTypeCallback;
const parseForexTypeCallback = (data) => {
    // Legacy callbacks from old Forex menu messages (e.g. "forex:fiat").
    if (data === "forex:fiat")
        return "fiat";
    if (data === "forex:crypto")
        return "crypto";
    if (!data.startsWith("forex:type:"))
        return undefined;
    const type = data.replace("forex:type:", "");
    return type === "fiat" || type === "crypto" || type === "back" ? type : undefined;
};
exports.parseForexTypeCallback = parseForexTypeCallback;
const forexRandomCallback = (type) => `forex:random:${type}`;
exports.forexRandomCallback = forexRandomCallback;
const parseForexRandomCallback = (data) => {
    if (!data.startsWith("forex:random:"))
        return undefined;
    const type = data.replace("forex:random:", "");
    return type === "fiat" || type === "crypto" ? type : undefined;
};
exports.parseForexRandomCallback = parseForexRandomCallback;
const forexSelectCallback = (type) => `forex:select:${type}`;
exports.forexSelectCallback = forexSelectCallback;
const parseForexSelectCallback = (data) => {
    if (!data.startsWith("forex:select:"))
        return undefined;
    const type = data.replace("forex:select:", "");
    return type === "fiat" || type === "crypto" ? type : undefined;
};
exports.parseForexSelectCallback = parseForexSelectCallback;
const forexTypeMenu = (supportUrl) => telegraf_1.Markup.inlineKeyboard([
    [telegraf_1.Markup.button.callback("💵 Fiat", (0, exports.forexTypeCallback)("fiat"))],
    [telegraf_1.Markup.button.callback("₿ Crypto", (0, exports.forexTypeCallback)("crypto"))],
    [(0, exports.supportButton)(supportUrl)],
    [telegraf_1.Markup.button.callback("🔙 Back to Markets", (0, exports.marketCallback)("back"))]
]);
exports.forexTypeMenu = forexTypeMenu;
const forexActionMenu = (type, supportUrl) => telegraf_1.Markup.inlineKeyboard([
    [telegraf_1.Markup.button.callback("🎲 Skip pair selection", (0, exports.forexRandomCallback)(type))],
    [telegraf_1.Markup.button.callback("🔍 Select pair", (0, exports.forexSelectCallback)(type))],
    [(0, exports.supportButton)(supportUrl)],
    [telegraf_1.Markup.button.callback("🔙 Back to Forex", (0, exports.forexTypeCallback)("back"))]
]);
exports.forexActionMenu = forexActionMenu;
const STOCK_COUNTRIES = [
    { code: "US", name: "🇺🇸 United States" },
    { code: "UK", name: "🇬🇧 United Kingdom" },
    { code: "DE", name: "🇩🇪 Germany" },
    { code: "FR", name: "🇫🇷 France" },
    { code: "JP", name: "🇯🇵 Japan" },
    { code: "CA", name: "🇨🇦 Canada" },
    { code: "AU", name: "🇦🇺 Australia" },
    { code: "HK", name: "🇭🇰 Hong Kong" },
    { code: "IN", name: "🇮🇳 India" },
    { code: "BR", name: "🇧🇷 Brazil" },
    { code: "ZA", name: "🇿🇦 South Africa" },
    { code: "MX", name: "🇲🇽 Mexico" },
    { code: "SG", name: "🇸🇬 Singapore" },
    { code: "CH", name: "🇨🇭 Switzerland" },
    { code: "OTHER", name: "🌍 Other" }
];
const COUNTRY_CODE_TO_NAME = {
    US: "United States",
    UK: "United Kingdom",
    DE: "Germany",
    FR: "France",
    JP: "Japan",
    CA: "Canada",
    AU: "Australia",
    HK: "Hong Kong",
    IN: "India",
    BR: "Brazil",
    ZA: "South Africa",
    MX: "Mexico",
    SG: "Singapore",
    CH: "Switzerland",
    OTHER: "Other"
};
const stocksCountryCallback = (countryCode) => `stocks:country:${countryCode}`;
exports.stocksCountryCallback = stocksCountryCallback;
const parseStocksCountryCallback = (data) => {
    if (!data.startsWith("stocks:country:"))
        return undefined;
    return data.replace("stocks:country:", "");
};
exports.parseStocksCountryCallback = parseStocksCountryCallback;
const stocksScopeCallback = (countryCode, scope) => `stocks:scope:${countryCode}:${scope}`;
exports.stocksScopeCallback = stocksScopeCallback;
const parseStocksScopeCallback = (data) => {
    if (!data.startsWith("stocks:scope:"))
        return undefined;
    const parts = data.split(":");
    if (parts.length !== 4)
        return undefined;
    return { countryCode: parts[2], scope: parts[3] };
};
exports.parseStocksScopeCallback = parseStocksScopeCallback;
const stocksCountryMenu = (supportUrl) => telegraf_1.Markup.inlineKeyboard([
    ...STOCK_COUNTRIES.map((country) => [telegraf_1.Markup.button.callback(country.name, (0, exports.stocksCountryCallback)(country.code))]),
    [(0, exports.supportButton)(supportUrl)],
    [telegraf_1.Markup.button.callback("🔙 Back to Markets", (0, exports.marketCallback)("back"))]
]);
exports.stocksCountryMenu = stocksCountryMenu;
const stocksScopeMenu = (countryCode, customName, supportUrl) => {
    const countryName = customName ?? COUNTRY_CODE_TO_NAME[countryCode] ?? countryCode;
    return telegraf_1.Markup.inlineKeyboard([
        [telegraf_1.Markup.button.callback(`🏠 Local (${countryName})`, (0, exports.stocksScopeCallback)(countryCode, "local"))],
        [telegraf_1.Markup.button.callback("🌍 International", (0, exports.stocksScopeCallback)(countryCode, "international"))],
        [telegraf_1.Markup.button.callback("🔙 Back to Countries", "stocks:back:countries")],
        [(0, exports.supportButton)(supportUrl)],
        [telegraf_1.Markup.button.callback("🔙 Back to Markets", (0, exports.marketCallback)("back"))]
    ]);
};
exports.stocksScopeMenu = stocksScopeMenu;
const countryCodeToName = (code) => COUNTRY_CODE_TO_NAME[code] ?? code;
exports.countryCodeToName = countryCodeToName;
const signalsMenu = (platformCode, networkCode, supportUrl) => (0, exports.isValerisPlatform)(platformCode)
    ? valerisSignalsMenu(platformCode, networkCode, supportUrl)
    : genericSignalsMenu(platformCode, supportUrl);
exports.signalsMenu = signalsMenu;
const valerisNetworkSelectionMenu = (supportUrl) => telegraf_1.Markup.inlineKeyboard([
    ...ValerisNetwork_js_1.VALERIS_ALLOWED_NETWORKS.map((network) => [
        telegraf_1.Markup.button.callback(networkEmoji(network) + " " + network, (0, exports.valerisNetworkCallback)(ValerisNetwork_js_1.NETWORK_CODES[network]))
    ]),
    [telegraf_1.Markup.button.callback("🎲 Any Network (Random)", (0, exports.valerisNetworkCallback)(ValerisNetwork_js_1.ANY_NETWORK_CODE))],
    [(0, exports.supportButton)(supportUrl)]
]);
exports.valerisNetworkSelectionMenu = valerisNetworkSelectionMenu;
const valerisSignalsMenu = (platformCode, networkCode, supportUrl) => telegraf_1.Markup.inlineKeyboard([
    [telegraf_1.Markup.button.callback("🌾 Farm Liquidity", (0, exports.categoryCallback)("farm-liquidity", platformCode, networkCode))],
    [telegraf_1.Markup.button.callback("📈 Options", (0, exports.categoryCallback)("trading-options", platformCode, networkCode))],
    [
        telegraf_1.Markup.button.callback("⚖️ Margin", (0, exports.categoryCallback)("margin", platformCode, networkCode)),
        telegraf_1.Markup.button.callback("🤖 AI Strategies", (0, exports.categoryCallback)("ai-strategy", platformCode, networkCode))
    ],
    [telegraf_1.Markup.button.callback("🦅 Arbitrage", (0, exports.categoryCallback)("arbitrage", platformCode, networkCode))],
    [telegraf_1.Markup.button.callback("🌐 Change Network", (0, exports.valerisNetworkCallback)(ValerisNetwork_js_1.ANY_NETWORK_CODE))],
    [telegraf_1.Markup.button.callback("📋 My Subscription", (0, exports.mySubscriptionCallback)(platformCode))],
    [(0, exports.supportButton)(supportUrl)],
    [telegraf_1.Markup.button.callback("🔙 Back to Platforms", (0, exports.platformCallback)("back"))]
]);
const genericSignalsMenu = (platformCode, supportUrl) => telegraf_1.Markup.inlineKeyboard([
    [telegraf_1.Markup.button.callback("📈 Trading Options", (0, exports.categoryCallback)("trading-options", platformCode))],
    [
        telegraf_1.Markup.button.callback("⚖️ Margin", (0, exports.categoryCallback)("margin", platformCode)),
        telegraf_1.Markup.button.callback("🤖 AI Strategy", (0, exports.categoryCallback)("ai-strategy", platformCode))
    ],
    [telegraf_1.Markup.button.callback("🦅 Arbitrage Opportunities", (0, exports.categoryCallback)("arbitrage", platformCode))],
    [telegraf_1.Markup.button.callback("📋 My Subscription", (0, exports.mySubscriptionCallback)(platformCode))],
    [(0, exports.supportButton)(supportUrl)],
    [telegraf_1.Markup.button.callback("🔙 Back to Platforms", (0, exports.platformCallback)("back"))]
]);
const isValerisPlatform = (platformCode) => platformCode === "lychee";
exports.isValerisPlatform = isValerisPlatform;
const networkEmoji = (network) => {
    switch (network) {
        case "Ethereum":
            return "⬜";
        case "BNB Smart Chain":
            return "🟡";
        case "Polygon":
            return "🟣";
        case "Avalanche C-Chain":
            return "🔺";
        case "Base":
            return "🔵";
        default:
            return "🌐";
    }
};
