import {discordLink} from "common/constants";

export const SITE_ORDER = [
  'site', // personal site
  'x', // twitter
  'discord',
  'bluesky',
  'mastodon',
  'substack',
  'paypal',
  'instagram',
  'github',
  'linkedin',
  'facebook',
  'patreon',
  'okcupid',
  'calendly',
  'datingdoc',
  'friendshipdoc',
  'connectiondoc',
  'workdoc',
  'spotify',
] as const

export type Site = (typeof SITE_ORDER)[number]

// this is a lie, actually people can have anything in their links
export type Socials = { [key in Site]?: string }

export const strip = (site: Site, input: string) =>
  stripper[site]?.(input) ?? input

const stripper: { [key in Site]: (input: string) => string } = {
  site: (s) => s.replace(/^(https?:\/\/)/, ''),
  x: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?(twitter|x)(\.com\/)/, '')
      .replace(/^@/, '')
      .replace(/\/$/, ''),
  discord: (s) => s,
  paypal: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?(\w+\.)?paypal\.com\/paypalme\//, '')
      .replace(/\/$/, ''),
  patreon: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?(\w+\.)?patreon\.com\//, '')
      .replace(/\/$/, ''),
  okcupid: (s) => s.replace(/^(https?:\/\/)/, ''),
  calendly: (s) => s,
  datingdoc: (s) => s,
  friendshipdoc: (s) => s,
  workdoc: (s) => s,
  connectiondoc: (s) => s,
  bluesky: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?bsky\.app\/profile\//, '')
      .replace(/^@/, '')
      .replace(/\/$/, ''),
  mastodon: (s) => s.replace(/^@/, ''),
  substack: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?(\w+\.)?substack\.com\//, '')
      .replace(/\/$/, ''),
  instagram: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?instagram\.com\//, '')
      .replace(/^@/, '')
      .replace(/\/$/, ''),
  github: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?github\.com\//, '')
      .replace(/^@/, '')
      .replace(/\/$/, ''),
  linkedin: (s) =>
    s
      .replace(/^(https?:\/\/)?(www\.)?linkedin\.com\/(in|company)\//, '')
      .replace(/\/$/, ''),
  facebook: (s) =>
    s.replace(/^(https?:\/\/)?(www\.)?facebook\.com\//, '').replace(/\/$/, ''),
  spotify: (s) =>
    s
      .replace(/^(https?:\/\/)?(open\.)?spotify\.com\/(artist|user)\//, '')
      .replace(/\/$/, ''),
}

export const getSocialUrl = (site: Site, handle: string) =>
  urler[site]?.(handle) ?? urler.site(handle)

const urler: { [key in Site]: (handle: string) => string } = {
  site: (s) => (s.startsWith('http') ? s : `https://${s}`),
  okcupid: (s) => (s.startsWith('http') ? s : `https://${s}`),
  x: (s) => s.startsWith('http') ? s : `https://x.com/${s}`,
  discord: (s) =>
    (s.length === 17 || s.length === 18) && !isNaN(parseInt(s, 10))
      ? `https://discord.com/users/${s}` // discord user id
      : discordLink, // our server
  bluesky: (s) => `https://bsky.app/profile/${s}`,
  mastodon: (s) =>
    s.includes('@') ? `https://${s.split('@')[1]}/@${s.split('@')[0]}` : s,
  substack: (s) => s.startsWith('http') ? s : `https://${s}.substack.com`,
  instagram: (s) => s.startsWith('http') ? s : `https://instagram.com/${s}`,
  github: (s) => s.startsWith('http') ? s : `https://github.com/${s}`,
  linkedin: (s) => s.startsWith('http') ? s : `https://linkedin.com/in/${s}`,
  facebook: (s) => s.startsWith('http') ? s : `https://facebook.com/${s}`,
  spotify: (s) => s.startsWith('http') ? s : `https://open.spotify.com/user/${s}`,
  paypal: (s) => s.startsWith('http') ? s : `https://paypal.com/paypalme/${s}`,
  patreon: (s) => s.startsWith('http') ? s : `https://patreon.com/${s}`,
  calendly: (s) => (s.startsWith('http') ? s : `https://${s}`),
  datingdoc: (s) => (s.startsWith('http') ? s : `https://${s}`),
  friendshipdoc: (s) => (s.startsWith('http') ? s : `https://${s}`),
  workdoc: (s) => (s.startsWith('http') ? s : `https://${s}`),
  connectiondoc: (s) => (s.startsWith('http') ? s : `https://${s}`),
}

export const PLATFORM_LABELS: { [key in Site]: string } = {
  site: 'Website',
  x: 'Twitter/X',
  discord: 'Discord',
  bluesky: 'Bluesky',
  mastodon: 'Mastodon',
  substack: 'Substack',
  instagram: 'Instagram',
  github: 'GitHub',
  linkedin: 'LinkedIn',
  facebook: 'Facebook',
  spotify: 'Spotify',
  paypal: 'Paypal',
  patreon: 'Patreon',
  okcupid: 'OkCupid',
  calendly: 'Calendly',
  datingdoc: 'Dating Doc',
  friendshipdoc: 'Friendship Doc',
  workdoc: 'Work Doc',
  connectiondoc: 'Connection Doc',
}
