"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.blockExplorerLinks = exports.resolveBlockExplorerDomain = exports.blockExplorerLink = void 0;
var ethers_1 = require("ethers");
var constants_1 = require("../constants");
var FormattingUtils_1 = require("./FormattingUtils");
var TypeGuards_1 = require("./TypeGuards");
function blockExplorerLink(txHashOrAddress, chainId) {
    var _a;
    return (_a = _createBlockExplorerLinkMarkdown(txHashOrAddress, Number(chainId))) !== null && _a !== void 0 ? _a : "<>";
}
exports.blockExplorerLink = blockExplorerLink;
function resolveBlockExplorerDomain(networkId) {
    var _a;
    return (_a = constants_1.PUBLIC_NETWORKS[networkId]) === null || _a === void 0 ? void 0 : _a.blockExplorer;
}
exports.resolveBlockExplorerDomain = resolveBlockExplorerDomain;
function constructURL(domain, parts) {
    parts = parts.flatMap(function (p) { return p.split("/"); });
    parts = parts.filter(function (p) { return p !== ""; });
    var path = parts.join("/");
    return "".concat(domain, "/").concat(path).replace(/\/+$/, "");
}
function _createBlockExplorerLinkMarkdown(hex, chainId) {
    if (chainId === void 0) { chainId = 1; }
    var explorerDomain = resolveBlockExplorerDomain(chainId);
    if (!(0, TypeGuards_1.isDefined)(explorerDomain)) {
        return "<unsupported chain/hash ".concat(chainId, ":").concat(hex, ">}");
    }
    if (hex.substring(0, 2) !== "0x") {
        hex = "0x".concat(hex);
    }
    if (!ethers_1.ethers.utils.isHexString(hex)) {
        return null;
    }
    var shortURLString = (0, FormattingUtils_1.createShortHexString)(hex);
    for (var _i = 0, _a = [
        [66, "tx"],
        [42, "address"],
    ]; _i < _a.length; _i++) {
        var _b = _a[_i], length_1 = _b[0], route = _b[1];
        if (hex.length === length_1) {
            return "<".concat(constructURL(explorerDomain, [route, hex]), "|").concat(shortURLString, ">");
        }
    }
    return null;
}
function blockExplorerLinks(txHashesOrAddresses, chainId) {
    return txHashesOrAddresses.map(function (hash) { return "".concat(blockExplorerLink(hash, chainId), "\n"); }).join("");
}
exports.blockExplorerLinks = blockExplorerLinks;
//# sourceMappingURL=BlockExplorerUtils.js.map