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: 'Www Next',
    description: 'My awesome app using docz',
  },
  plugins: [
    {
      resolve: 'gatsby-theme-docz',
      options: {
        themeConfig: {},
        src: './',
        gatsbyRoot: null,
        themesDir: 'src',
        mdxExtensions: ['.md', '.mdx'],
        docgenConfig: {},
        menu: [],
        mdPlugins: [],
        hastPlugins: [],
        ignore: [],
        typescript: false,
        ts: false,
        propsParser: true,
        'props-parser': true,
        debug: false,
        native: false,
        openBrowser: null,
        o: null,
        open: null,
        'open-browser': null,
        root: '/home/patrick/gitlab/www-next/.docz',
        base: '/',
        source: './',
        'gatsby-root': null,
        files: '**/*.{md,markdown,mdx}',
        public: '/public',
        dest: '.docz/dist',
        d: '.docz/dist',
        editBranch: 'master',
        eb: 'master',
        'edit-branch': 'master',
        config: '',
        title: 'Www Next',
        description: 'My awesome app using docz',
        host: 'localhost',
        port: 3001,
        p: 3000,
        separator: '-',
        paths: {
          root: '/home/patrick/gitlab/www-next',
          templates:
            '/home/patrick/gitlab/www-next/node_modules/docz-core/dist/templates',
          docz: '/home/patrick/gitlab/www-next/.docz',
          cache: '/home/patrick/gitlab/www-next/.docz/.cache',
          app: '/home/patrick/gitlab/www-next/.docz/app',
          appPackageJson: '/home/patrick/gitlab/www-next/package.json',
          appTsConfig: '/home/patrick/gitlab/www-next/tsconfig.json',
          gatsbyConfig: '/home/patrick/gitlab/www-next/gatsby-config.js',
          gatsbyBrowser: '/home/patrick/gitlab/www-next/gatsby-browser.js',
          gatsbyNode: '/home/patrick/gitlab/www-next/gatsby-node.js',
          gatsbySSR: '/home/patrick/gitlab/www-next/gatsby-ssr.js',
          importsJs: '/home/patrick/gitlab/www-next/.docz/app/imports.js',
          rootJs: '/home/patrick/gitlab/www-next/.docz/app/root.jsx',
          indexJs: '/home/patrick/gitlab/www-next/.docz/app/index.jsx',
          indexHtml: '/home/patrick/gitlab/www-next/.docz/app/index.html',
          db: '/home/patrick/gitlab/www-next/.docz/app/db.json',
        },
      },
    },
    {
      resolve: 'gatsby-plugin-eslint',
      options: {
        test: /\.js$|\.jsx$/,
        exclude: /.*/,
        stages: ['develop'],
        options: {
          emitWarning: false,
          failOnError: false,
        },
      },
    },
    {
      resolve: 'gatsby-plugin-compile-es6-packages',
      options: {
        modules: ['docz', 'gatsby-theme-docz'],
      },
    },
  ],
}

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

module.exports = merge(config, custom)
