import { cleanUrl } from "@/lib/utils";
import Link from "next/link";
import ProfileMention from "./ProfileMention";
import EventMention from "./EventMention";
const RenderText = ({ text }: { text?: string }) => {
if (!text) return null;
const Elements: JSX.Element[] = [];
const urlRegex =
/(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))/g;
const hashtagRegex = /#\b\w+\b/g;
const nostrPrefixRegex = /nostr:[a-z0-9]+/g;
// const usernameRegex = /(?:^|\s)\@(\w+)\b/g;
const combinedRegex = new RegExp(
`(${urlRegex.source}|${hashtagRegex.source}|${nostrPrefixRegex.source})`,
"g",
);
// Get Array of URLs
const specialValuesArray = text.match(combinedRegex);
const formattedText = text.replace(combinedRegex, "##[link]##");
const cleanTextArray = formattedText.split("##[link]##");
cleanTextArray.forEach((string, index) => {
const jsxElement = {string};
Elements.push(jsxElement);
let specialElement;
if (specialValuesArray?.length && specialValuesArray.length > index) {
if (specialValuesArray[index]?.match(urlRegex)) {
specialElement = (
{cleanUrl(specialValuesArray[index])}
);
// specialElement = ;
// specialElement = {cleanUrl(specialValuesArray[index])};
} else if (specialValuesArray[index]?.match(hashtagRegex)) {
specialElement = (
{specialValuesArray[index]}
);
// specialElement = {specialValuesArray[index]};
} else if (specialValuesArray[index]?.match(nostrPrefixRegex)) {
const mention = specialValuesArray[index]?.split(":")[1];
if (mention.startsWith("nprofile") || mention.startsWith("npub")) {
specialElement = ;
} else if (
mention.startsWith("nevent") ||
mention.startsWith("note") ||
mention.startsWith("naddr")
) {
specialElement = ;
}
}
if (specialElement) {
Elements.push(specialElement);
}
}
});
return (
<>
{Elements.map((el, index) => (
{el}
))}
>
);
};
export { RenderText };