import type { Markdown } from 'vuepress/markdown'
import type { MarkdownPowerPluginOptions } from '../../shared/index.js'
import { audioReaderPlugin } from './audio/reader.js'
import { caniusePlugin, legacyCaniuse } from './caniuse.js'
import { codepenPlugin } from './code/codepen.js'
import { codeSandboxPlugin } from './code/codeSandbox.js'
import { jsfiddlePlugin } from './code/jsfiddle.js'
import { replitPlugin } from './code/replit.js'
import { pdfPlugin } from './pdf.js'
import { qrcodePlugin } from './qrcode.js'
import { acfunPlugin } from './video/acfun.js'
import { artPlayerPlugin } from './video/artPlayer.js'
import { bilibiliPlugin } from './video/bilibili.js'
import { youtubePlugin } from './video/youtube.js'

/**
 * Embed syntax plugin - Register all embed syntax plugins
 *
 * 嵌入语法插件 - 注册所有嵌入语法插件
 *
 * @param md - Markdown instance / Markdown 实例
 * @param options - Plugin options / 插件选项
 */
export function embedSyntaxPlugin(md: Markdown, options: MarkdownPowerPluginOptions): void {
  if (options.caniuse) {
    const caniuse = options.caniuse === true ? {} : options.caniuse
    // @[caniuse](feature_name)
    md.use(caniusePlugin, caniuse)
    // 兼容旧语法
    legacyCaniuse(md, caniuse)
  }

  if (options.pdf) {
    // @[pdf](url)
    md.use(pdfPlugin)
  }

  if (options.acfun) {
    // @[acfun](id)
    md.use(acfunPlugin)
  }

  if (options.bilibili) {
    // @[bilibili](bvid aid cid)
    md.use(bilibiliPlugin)
  }

  if (options.youtube) {
    // @[youtube](id)
    md.use(youtubePlugin)
  }

  if (options.artPlayer) {
    // @[artPlayer](url)
    md.use(artPlayerPlugin)
  }

  if (options.audioReader) {
    // @[audioReader](url)
    md.use(audioReaderPlugin)
  }

  if (options.codepen) {
    // @[codepen](user/slash)
    md.use(codepenPlugin)
  }

  if (options.replit) {
    // @[replit](user/repl-name)
    md.use(replitPlugin)
  }

  if (options.codeSandbox) {
    // @[codesandbox](id)
    md.use(codeSandboxPlugin)
  }

  if (options.jsfiddle) {
    // @[jsfiddle](user/id)
    md.use(jsfiddlePlugin)
  }

  if (options.qrcode) {
    // @[qrcode svg card title="xxx"](text)
    md.use(qrcodePlugin)
  }
}
