"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toClickableSpan = void 0;
const explorer_url_1 = __importDefault(require("./explorer-url"));
const func_1 = require("./func");
class Link {
    constructor({ type, did, chainId, text }) {
        this.text = text;
        this.did = did;
        this.type = type;
        this.chainId = chainId;
    }
}
const toClickableSpan = (str, isHighLight = true) => {
    const textList = toTextList(str);
    return textList
        .map((item) => {
        if (item instanceof Link) {
            if (isHighLight) {
                let url;
                const { type, chainId, did } = item;
                if ((0, func_1.isSameAddr)(type, 'link')) {
                    url = `${chainId}:${did}`;
                }
                else if ((0, func_1.isSameAddr)(type, 'tx')) {
                    url = (0, explorer_url_1.default)({ hash: did, chainId });
                }
                else if ((0, func_1.isSameAddr)(type, 'stake')) {
                    url = (0, explorer_url_1.default)({ stake: did, chainId });
                }
                else if ((0, func_1.isSameAddr)(type, 'nft')) {
                    url = (0, explorer_url_1.default)({ asset: did, chainId });
                }
                else if ((0, func_1.isSameAddr)(type, 'token')) {
                    url = (0, explorer_url_1.default)({ token: did, chainId });
                }
                else if ((0, func_1.isSameAddr)(type, 'did')) {
                    url = (0, explorer_url_1.default)({ address: did, chainId });
                }
                // HACK: 邮件中无法支持 dapp 的展示，缺少 dapp 链接，只能作为加粗展示
                if ((0, func_1.isSameAddr)(type, 'dapp')) {
                    return `<em style="font-weight:bold;" data-type="${type}" data-chain-id="${chainId}" data-did="${did}">${item.text}</em>`;
                }
                if (url) {
                    return `<a target="_blank" style="color:#4598fa;font-weight:bold;" href="${url}">${item.text}</a>`;
                }
                // 默认展示为加粗
                return `<em style="font-weight:bold;" data-type="${type}" data-chain-id="${chainId}" data-did="${did}">${item.text}</em>`;
            }
            return item.text;
        }
        return item;
    })
        .join('');
};
exports.toClickableSpan = toClickableSpan;
function toTextList(str) {
    var _a;
    const arr = [];
    try {
        let startPoint = 0;
        const pattern = /<(.+?)\(?[tx|nft|token|stake|did|dapp|link]+[:](.+?)\)?>/gi; // 匹配 <asdad(did:abt:xxx)>
        const matches = str.matchAll(pattern);
        for (const match of matches) {
            const didPattern = /\([tx|nft|token|stake|did|dapp|link]+[:](.+?)\)/gi; // 匹配 (did:abt:xxx)
            const oriHit = match[0];
            const matchIndex = match.index || startPoint;
            const endPoint = matchIndex + oriHit.length;
            const didMatcherLength = oriHit.matchAll(didPattern);
            if ([...didMatcherLength].length > 1) {
                return [str];
            }
            let showName = '';
            const didMatcher = didPattern.exec(oriHit);
            let hit = '';
            if (didMatcher) {
                const didGroup = didMatcher[0];
                showName = oriHit.replace(didGroup, '').replace(/^</, '').replace(/(>)$/, '');
                hit = didGroup.replace(/\)$/, '').replace(/^\(/, '');
            }
            else {
                hit = oriHit;
            }
            const address = hit.split(':');
            const didType = (_a = (address[0] || '').replace(/^</, '').replace(/^\(/, '')) === null || _a === void 0 ? void 0 : _a.toUpperCase();
            const chainId = (address[1] || '').replace(/>$/, '').replace(/\)$/, '');
            const did = (address[address.length - 1] || '').replace(/>$/, '').replace(/\)$/, '');
            arr.push(str.substring(startPoint, matchIndex));
            arr.push(new Link({
                type: didType,
                chainId,
                did,
                text: showName || didType,
            }));
            startPoint = endPoint;
        }
        if (startPoint < str.length) {
            arr.push(str.substring(startPoint));
        }
        return arr;
    }
    catch (error) {
        console.error(error === null || error === void 0 ? void 0 : error.message);
        return [str];
    }
}
