import path from 'node:path'
import { searchForWorkspaceRoot } from 'vite'
import { getAllModules } from '../shared/module-utils'

const workspaceRoot = searchForWorkspaceRoot(process.cwd())
const pathFromWorkspaceRoot = (...args) => path.resolve(workspaceRoot, ...args)

const getComponentAlias = (alias = {}) => {
  getAllModules(false).forEach((item) => {
    if (item.type === 'component') alias[item.importName] = pathFromWorkspaceRoot('packages', item.path)
  })
  return alias
}

const getAlias = (vueVersion: string | number, theme = '', design?: string) => {
  const ns = (t: string) => (t ? '-' + t : '')

  const map = {
    // 主模块映射
    // vite 4 不支持 exports 语法自动拼接 index 文件 https://github.com/vitejs/vite/issues/7267
    '@opentiny/vue-icon-saas$': pathFromWorkspaceRoot('packages/vue-icon-saas/index.ts'),
    '@opentiny/vue-icon-saas': pathFromWorkspaceRoot('packages/vue-icon-saas/src'),
    '@opentiny/vue-icon$': pathFromWorkspaceRoot(`packages/vue-icon${ns(design || theme)}/index.ts`),
    '@opentiny/vue-icon-multicolor$': pathFromWorkspaceRoot(`packages/vue-icon-multicolor${ns(theme)}/index.ts`),
    '@opentiny/vue-renderless': pathFromWorkspaceRoot('packages/renderless/src'),
    '@opentiny/vue-theme/dark-theme-index.css': pathFromWorkspaceRoot(
      `packages/theme${ns(design || theme)}/src/base/dark-theme.less`
    ),
    '@opentiny/vue-theme': pathFromWorkspaceRoot(`packages/theme${ns(design || theme)}/src`),
    '@opentiny/vue-theme-saas': pathFromWorkspaceRoot('packages/theme-saas/src'),
    '@opentiny/vue-common': pathFromWorkspaceRoot('packages/vue-common/src/index-dev'),
    '@opentiny/vue-locale': pathFromWorkspaceRoot('packages/vue-locale/src'),

    // 虚拟映射
    'virtual:locale/vue': pathFromWorkspaceRoot(`packages/vue-locale/src/vue${vueVersion}/index`),
    'virtual:common/adapter/vue': pathFromWorkspaceRoot(`packages/vue-common/src/adapter/vue${vueVersion}/index`),
    // examples 映射
    '@opentiny/vue-mobile-first-example/docs': pathFromWorkspaceRoot('examples/docs/components/mobile-first'),
    '@opentiny/vue-mobile-example/docs': pathFromWorkspaceRoot('examples/docs/components/mobile'),
    '@opentiny/vue-example/docs': pathFromWorkspaceRoot('examples/docs/components/pc'),
    ...getComponentAlias()
  }

  if (!design && vueVersion === 3) {
    map['@opentiny/vue-icon'] = pathFromWorkspaceRoot(`packages/vue-icon${ns(design || theme)}/src`)
  }

  return map
}

const createContainer = (klass, defaultTitle) => {
  return [
    klass,
    {
      render(tokens, idx) {
        const token = tokens[idx]
        const info = token.info.trim().slice(klass.length).trim()

        if (token.nesting === 1)
          return `<div class="${klass} custom-block"><p class="custom-block-title">${info || defaultTitle}</p>\n`

        return '</div>\n'
      }
    }
  ]
}

const getOptimizeDeps = (vueVersion: string | number) => {
  return {
    include: [
      'shepherd.js',
      'echarts/core',
      'echarts/components',
      'echarts/charts',
      'echarts/extension/bmap/bmap',
      'echarts/features',
      'echarts/renderers',
      'echarts-wordcloud',
      'echarts',
      'streamsaver',
      vueVersion === 2 ? '@vue/babel-helper-vue-jsx-merge-props' : ''
    ].filter((item) => !!item)
  }
}

export { pathFromWorkspaceRoot, getAlias, createContainer, getOptimizeDeps }
