const { mergeWith } = require('docz-utils')
const fs = require('fs-extra')

let custom = {}
const hasGatsbyConfig = fs.existsSync('./gatsby-config.custom.js')

if (hasGatsbyConfig) {
  try {
    custom = require('./gatsby-config.custom')
  } catch (err) {
    console.error(
      `Failed to load your gatsby-config.js file : `,
      JSON.stringify(err),
    )
  }
}

const config = {
  pathPrefix: '/',

  siteMetadata: {
    title: '☁️ React Wordcloud',
    description:
      'Simple React + D3 wordcloud component with powerful features.',
  },
  plugins: [
    {
      resolve: 'gatsby-plugin-typescript',
      options: {
        isTSX: true,
        allExtensions: true,
      },
    },
    {
      resolve: 'gatsby-theme-docz',
      options: {
        themeConfig: {
          styles: {
            root: {
              fontFamily:
                '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
            },
          },
        },
        src: './',
        gatsbyRoot: null,
        themesDir: 'src',
        mdxExtensions: ['.md', '.mdx'],
        docgenConfig: {},
        menu: [
          'Home',
          'Readme',
          'Props',
          {
            name: 'Usage',
            menu: [
              'Basic',
              'Size',
              'Callbacks',
              'Transitions',
              'Options',
              'Optimizations',
            ],
          },
          'Common Issues',
          'Changelog',
        ],
        mdPlugins: [],
        hastPlugins: [],
        ignore: [],
        typescript: true,
        ts: false,
        propsParser: true,
        'props-parser': true,
        debug: false,
        native: false,
        openBrowser: null,
        o: null,
        open: null,
        'open-browser': null,
        root: '/Users/chrisrzhou/Github/react-wordcloud/.docz',
        base: '/',
        source: './',
        'gatsby-root': null,
        files: '**/*.{md,markdown,mdx}',
        public: 'public',
        dest: 'dist/docs',
        d: '.docz/dist',
        editBranch: 'master',
        eb: 'master',
        'edit-branch': 'master',
        config: '',
        title: '☁️ React Wordcloud',
        description:
          'Simple React + D3 wordcloud component with powerful features.',
        host: 'localhost',
        port: 3000,
        p: 3000,
        separator: '-',
        paths: {
          root: '/Users/chrisrzhou/Github/react-wordcloud',
          templates:
            '/Users/chrisrzhou/Github/react-wordcloud/node_modules/docz-core/dist/templates',
          docz: '/Users/chrisrzhou/Github/react-wordcloud/.docz',
          cache: '/Users/chrisrzhou/Github/react-wordcloud/.docz/.cache',
          app: '/Users/chrisrzhou/Github/react-wordcloud/.docz/app',
          appPackageJson:
            '/Users/chrisrzhou/Github/react-wordcloud/package.json',
          appTsConfig: '/Users/chrisrzhou/Github/react-wordcloud/tsconfig.json',
          gatsbyConfig:
            '/Users/chrisrzhou/Github/react-wordcloud/gatsby-config.js',
          gatsbyBrowser:
            '/Users/chrisrzhou/Github/react-wordcloud/gatsby-browser.js',
          gatsbyNode: '/Users/chrisrzhou/Github/react-wordcloud/gatsby-node.js',
          gatsbySSR: '/Users/chrisrzhou/Github/react-wordcloud/gatsby-ssr.js',
          importsJs:
            '/Users/chrisrzhou/Github/react-wordcloud/.docz/app/imports.js',
          rootJs: '/Users/chrisrzhou/Github/react-wordcloud/.docz/app/root.jsx',
          indexJs:
            '/Users/chrisrzhou/Github/react-wordcloud/.docz/app/index.jsx',
          indexHtml:
            '/Users/chrisrzhou/Github/react-wordcloud/.docz/app/index.html',
          db: '/Users/chrisrzhou/Github/react-wordcloud/.docz/app/db.json',
        },
      },
    },
  ],
}

const merge = mergeWith((objValue, srcValue) => {
  if (Array.isArray(objValue)) {
    return objValue.concat(srcValue)
  }
})

module.exports = merge(config, custom)
