import { hashText } from "./mvp-db";
import {
  extractXhsPublishUrl,
  safeHttpUrl,
} from "./publish-url";

export { extractXhsPublishUrl } from "./publish-url";

export function accountFromPublishLink(input: string) {
  const url = safeHttpUrl(extractXhsPublishUrl(input));
  if (!url) return null;
  const platform =
    url.hostname.includes("xiaohongshu") || url.hostname.includes("xhslink")
      ? "小红书"
      : "其他平台";
  const noteId =
    url.pathname.match(
      /\/(?:discovery\/item|explore)\/([A-Za-z0-9_-]{8,80})/,
    )?.[1] ?? "";
  const platformUid = `pending-${hashText(
    noteId || `${url.origin}${url.pathname}`,
  )}`;
  return {
    platform,
    platformUid,
    nickname: "待识别账号",
    profileUrl: "",
  };
}
