/* eslint-disable filenames/match-regex */
const path = require('path')
const fs = require('fs')
const CopyPlugin = require('copy-webpack-plugin')
const vueConfig = require('./vue.config')

const proxyInConfig = vueConfig.devServer.proxy
const proxy = {}
for (const path in proxyInConfig) {
  const p = proxyInConfig[path]
  delete p.pathRewrite
  proxy[path.replace('/-app-', '')] = p
}

module.exports = {
  // set your styleguidist configuration here
  title: '信创OA开发手册',
  sections: [
    {
      name: '介绍',
      content: 'docs/introduction.md',
    },
    {
      name: '快速上手',
      sections: [
        {
          name: '搭建开发环境',
          content: 'docs/setup.md',
        },
        {
          name: '代码规范',
          content: 'docs/style-guide.md',
        },
        {
          name: '新建Vue文件',
          content: 'docs/create-vue-file.md',
        },
        {
          name: '新增非流程页面',
          content: 'docs/new-page.md',
        },
        {
          name: '开发配置页面详情表单',
          components: 'src/webform/sd-webform.vue',
        },
        {
          name: '开发流程审批单',
          components: 'src/webflow/sd-webflow.vue',
        },
        {
          name: '第三方单点',
          content: 'docs/sso.md',
        },
        {
          name: '学习资源',
          content: 'docs/resources.md',
        },
      ],
    },
    {
      name: '定制主题',
      content: 'docs/custom-theme.md',
    },
    {
      name: '扩展点',
      sections: [
        {
          name: '介绍',
          content: 'docs/extension-points.md',
        },
        ...fs
          .readdirSync('./src/_extension-points')
          .filter((dir) => dir !== 'include.md')
          .map((dir) => ({
            name: dir,
            components: `src/_extension-points/${dir}/*.vue`,
          })),
      ],
    },
    {
      name: '关于组件库',
      content: 'docs/components.md',
    },
    {
      name: '组件库',
      components: fs
        .readdirSync('./src/common/components')
        .filter((file) => {
          if (!file.endsWith('.vue')) return false
          // 不显示在文档中的组件，例如：'sd-attachment'
          const excludeComponents = ['sd-data-table-sys']
          return !excludeComponents.includes(file.slice(0, -4))
        })
        .map((file) => {
          if (fs.existsSync(`./src/common/components/deprecated/${file}`))
            return `src/common/components/deprecated/${file}`
          else return `src/common/components/${file}`
        }),
    },
    {
      name: '非组件样例',
      sections: [
        ...fs.readdirSync('./docs/examples').map((file) => {
          return {
            name: file.replace('.md', ''),
            content: 'docs/examples/' + file,
          }
        }),
      ],
    },
    {
      name: 'Demo环境',
      content: 'docs/demo-server.md',
    },
    {
      name: '依赖的开源项目',
      content: 'docs/dependencies.md',
    },
  ],
  require: [
    path.join(__dirname, 'docs/styleguide.js'),
    path.join(__dirname, 'docs/styleguide.css'),
  ],
  getComponentPathLine(componentPath) {
    if (componentPath.startsWith('src\\_extension-points'))
      return componentPath.replace(/^src/, 'src_custom')
    return ''
  },
  webpackConfig: {
    devServer: {
      proxy,
      // 允许通过域名访问
      disableHostCheck: true,
    },
    plugins: [new CopyPlugin([{ from: 'docs/assets', to: 'assets' }])],
  },
  usageMode: 'expand',
  exampleMode: 'expand',
  renderRootJsx: path.join(__dirname, 'docs/styleguide-root.js'),
}
