"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.blockExplorerLink = blockExplorerLink;
exports.resolveBlockExplorerDomain = resolveBlockExplorerDomain;
exports.blockExplorerLinks = blockExplorerLinks;
const tslib_1 = require("tslib");
const ethers_1 = require("ethers");
const constants_1 = require("../constants");
const FormattingUtils_1 = require("./FormattingUtils");
const NetworkUtils_1 = require("./NetworkUtils");
const TypeGuards_1 = require("./TypeGuards");
const bs58_1 = tslib_1.__importDefault(require("bs58"));
function blockExplorerLink(txHashOrAddress, chainId) {
    return _createBlockExplorerLinkMarkdown(txHashOrAddress, Number(chainId)) ?? "<>";
}
function resolveBlockExplorerDomain(networkId) {
    return constants_1.PUBLIC_NETWORKS[networkId]?.blockExplorer;
}
function constructURL(domain, parts) {
    parts = parts.flatMap((p) => p.split("/"));
    parts = parts.filter((p) => p !== "");
    const path = parts.join("/");
    return `${domain}/${path}`.replace(/\/+$/, "");
}
function _createBlockExplorerLinkMarkdown(addr, chainId = 1) {
    const explorerDomain = resolveBlockExplorerDomain(chainId);
    if (!(0, TypeGuards_1.isDefined)(explorerDomain)) {
        return `<unsupported chain/hash ${chainId}:${addr}>}`;
    }
    if (addr.substring(0, 2) !== "0x" && (0, NetworkUtils_1.chainIsEvm)(chainId) && !(0, NetworkUtils_1.chainIsTvm)(chainId)) {
        addr = `0x${addr}`;
        if (!ethers_1.ethers.utils.isHexString(addr)) {
            return null;
        }
    }
    const shortURLString = (0, FormattingUtils_1.createShortenedString)(addr);
    if ((0, NetworkUtils_1.chainIsTvm)(chainId)) {
        const txHexNoPrefix = addr.startsWith("0x") ? addr.slice(2) : addr;
        if (txHexNoPrefix.length === 64 && ethers_1.ethers.utils.isHexString(`0x${txHexNoPrefix}`)) {
            const txIdForUrl = txHexNoPrefix.toLowerCase();
            return `<${constructURL(explorerDomain, ["#", "transaction", txIdForUrl])} | ${shortURLString}>`;
        }
        if (/^T[1-9A-HJ-NP-Za-km-z]{33}$/.test(addr)) {
            return `<${constructURL(explorerDomain, ["#", "address", addr])} | ${shortURLString}>`;
        }
        return null;
    }
    else if ((0, NetworkUtils_1.chainIsEvm)(chainId)) {
        for (const [length, route] of [
            [66, "tx"],
            [42, "address"],
        ]) {
            if (addr.length === length) {
                return `<${constructURL(explorerDomain, [route, addr])} | ${shortURLString}>`;
            }
        }
    }
    else {
        const addrLength = bs58_1.default.decode(addr).length;
        const route = addrLength === 32 ? "account" : "tx";
        return `<${constructURL(explorerDomain, [route, addr])} | ${shortURLString}>`;
    }
    return null;
}
function blockExplorerLinks(txHashesOrAddresses, chainId) {
    return txHashesOrAddresses.map((hash) => `${blockExplorerLink(hash, chainId)}\n`).join("");
}
//# sourceMappingURL=BlockExplorerUtils.js.map