import { resolveMindSpaceHomeUrl } from './publicUrl';
import type { MindSpacePage } from '../types';

export type SharePayload = {
  title: string;
  url: string;
  description?: string;
};

export type ShareChannel = {
  id: string;
  label: string;
  hint: string;
  buildText: (payload: SharePayload) => string;
};

export const SHARE_CHANNELS: ShareChannel[] = [
  {
    id: 'wechat_moments',
    label: '微信朋友圈',
    hint: '文案已复制，请打开微信粘贴到朋友圈',
    buildText: ({ title, url, description }) =>
      [title, description, url].filter(Boolean).join('\n'),
  },
  {
    id: 'xiaohongshu',
    label: '小红书',
    hint: '文案已复制，请打开小红书发布笔记并粘贴',
    buildText: ({ title, url, description }) =>
      [`${title}`, description ?? '分享自我的 MindSpace', url, '#TKMind #创作分享'].join('\n'),
  },
  {
    id: 'toutiao',
    label: '今日头条',
    hint: '文案已复制，请打开今日头条发布并粘贴',
    buildText: ({ title, url, description }) =>
      [`【${title}】`, description, url].filter(Boolean).join('\n'),
  },
  {
    id: 'weibo',
    label: '微博',
    hint: '文案已复制，请打开微博发布并粘贴',
    buildText: ({ title, url }) => `${title} ${url} #TKMind#`,
  },
  {
    id: 'douyin',
    label: '抖音',
    hint: '链接已复制，可粘贴到抖音个人简介或私信',
    buildText: ({ title, url }) => `${title}\n${url}`,
  },
  {
    id: 'copy_link',
    label: '复制链接',
    hint: '链接已复制',
    buildText: ({ url }) => url,
  },
];

export function buildPageSharePayload(page: MindSpacePage, pagePublicUrl?: string): SharePayload {
  const publicUrl = pagePublicUrl
    ? new URL(pagePublicUrl, window.location.origin).toString()
    : window.location.href;
  return {
    title: page.title || '我的 MindSpace 页面',
    url: publicUrl,
    description: page.summary || `查看我在 TKMind 的页面：${page.title || page.id}`,
  };
}

export async function copyShareText(text: string) {
  if (navigator.clipboard?.writeText) {
    await navigator.clipboard.writeText(text);
    return;
  }
  const textarea = document.createElement('textarea');
  textarea.value = text;
  textarea.style.position = 'fixed';
  textarea.style.opacity = '0';
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand('copy');
  document.body.removeChild(textarea);
}

export function canUseNativeShare() {
  return typeof navigator.share === 'function';
}

export async function nativeShare(payload: SharePayload) {
  await navigator.share({
    title: payload.title,
    text: payload.description ?? payload.title,
    url: payload.url,
  });
}

export function buildAssetSharePayload(
  asset: { displayName: string; publicUrl?: string | null },
  categoryCode?: string,
): SharePayload {
  if (asset.publicUrl) {
    const publicUrl = new URL(asset.publicUrl, window.location.origin).toString();
    return {
      title: asset.displayName,
      url: publicUrl,
      description: `看看我在 TKMind 空间里的图片「${asset.displayName}」`,
    };
  }
  const url = new URL(resolveMindSpaceHomeUrl());
  if (categoryCode) {
    url.searchParams.set('category', categoryCode);
  }
  return {
    title: asset.displayName,
    url: url.toString(),
    description: `看看我在 TKMind 空间里的作品「${asset.displayName}」`,
  };
}
