import { h } from "@stencil/core";
import { REGEX_HYPERLINK } from "../../utils/regex";
export const ZTextWithLink = ({ text, mention }) => {
    if (!text)
        return null;
    const segments = [];
    if (mention && mention.mentionees && mention.mentionees.length > 0) {
        let currentIndex = 0;
        const sorted = [...mention.mentionees].sort((a, b) => a.index - b.index);
        for (const m of sorted) {
            if (m.index > currentIndex) {
                segments.push({ type: 'text', content: text.substring(currentIndex, m.index) });
            }
            segments.push({ type: 'mention', content: text.substring(m.index, m.index + m.length) });
            currentIndex = m.index + m.length;
        }
        if (currentIndex < text.length) {
            segments.push({ type: 'text', content: text.substring(currentIndex) });
        }
    }
    else {
        segments.push({ type: 'text', content: text });
    }
    return (h("span", null, segments.map((segment, segIndex) => {
        if (segment.type === 'mention') {
            return (h("span", { key: `mention-${segIndex}`, class: "z-text-with-link cursor-default" }, segment.content));
        }
        const linkParts = segment.content.split(REGEX_HYPERLINK);
        return (h("span", { key: `text-${segIndex}` }, linkParts.map((part, index) => {
            if (REGEX_HYPERLINK.test(part)) {
                return (h("a", { key: index, href: part, target: "_blank", rel: "noopener noreferrer", class: "z-text-with-link" }, part));
            }
            return (h("span", { key: index }, part));
        })));
    })));
};
