const path = require('path')
const glob = require('glob')
const extend = require('util')._extend

const REACT_MYPAGES_SRC = 'node_modules/react-mypages/src'
const REACT_MYPAGES_HELPERS = REACT_MYPAGES_SRC + '/helpers'
const REACT_STYLEGUIDE_COMPONENTS = 'styleguide'

const ASSET_PATHS = [REACT_MYPAGES_SRC, REACT_STYLEGUIDE_COMPONENTS]
const dir = ASSET_PATHS.concat([REACT_MYPAGES_HELPERS]).map((_path) => path.resolve(__dirname, _path))

const utils_paths = function() {
  const resolve = path.resolve

  const base = function base() {
    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key]
    }

    return resolve.apply(resolve, [path.resolve(__dirname, '.')].concat(args))
  }

  return {
    base: base,
    client: base.bind(null, REACT_MYPAGES_SRC),
    dist: base.bind(null, 'dist')
  }
}()

function getComponents(directory) {
  return function() {
    return glob.sync(path.resolve(__dirname, directory + '/components/**/*.js')).filter(function(module) {
      return /\/[A-Z]\w*\.js$/.test(module)
    })
  };
}

module.exports = {
  // template: 'templates/index.html',
  title: 'tutti.ch Style Guide',
  defaultExample: false,
  skipComponentsWithoutExample: true,
  styleguideDir: 'docs', // this allow to publish the styleguide in GH-pages
  sections: [
    // {
    //   name: 'Components',
    //   components: getComponents(REACT_MYPAGES_SRC)
    // },
    {
      name: 'Styleguide',
      components: getComponents(REACT_STYLEGUIDE_COMPONENTS)
    }
  ],
  webpackConfig: {
    module: {
      loaders: [
        // Babel loader, will use your project’s .babelrc
        {
          test: /\.js?$/,
          include: dir,
          use: [{
            loader: 'babel-loader',
            query: {
              cacheDirectory: true,
              plugins: [
                'babel-plugin-transform-class-properties',
                'babel-plugin-syntax-dynamic-import',
                [
                  'babel-plugin-transform-runtime',
                  {
                    helpers: true,
                    polyfill: true,
                    regenerator: true
                  }
                ],
                [
                  'babel-plugin-transform-object-rest-spread',
                  {
                    useBuiltIns: false
                  }
                ]
              ],
              presets: [
                'stage-0',
                'babel-preset-react',
                ['babel-preset-env', {
                  targets: {
                    ie9: false,
                    uglify: true,
                    modules: false
                  }
                }]
              ]
            }
          }]
        },
        {
          test: /\.(css|sass|scss)$/,
          include: dir,
          use: [
            {
              loader: 'css-loader',
              options: {
                modules: true,
                localIdentName: '[name]__[local]___[hash:base64:5]',
                sourceMap: true,
                camelCase: true
              }
            },
            {
              loader: 'sass-loader',
              options: {
                sourceMap: true,
                includePaths: [
                  utils_paths.client('styles')
                ]
              }
            }
          ]
        },
        {
          test: /\.(png|jpg|eot|woff|woff2|ttf|svg)$/,
          exclude: /.pdf/,
          include: /.*/, // Because styleguidist requires either include or exclude.
          loader: 'url-loader?limit=8192'
        }
      ]
    }
  }
}

@@ -1343,20 +1343,14 @@
 %0A   
- //
  %7B%0A   
- //
    n
@@ -1371,19 +1371,16 @@
 ts',%0A   
- //
    compo
@@ -1422,19 +1422,16 @@
 SRC)%0A   
- //
  %7D,%0A    
@@ -1530,16 +1530,16 @@
  %7D%0A  %5D,%0A
-
   webpac
@@ -1545,24 +1545,207 @@
 ckConfig: %7B%0A
+    resolve: %7B%0A      modules: %5B%0A        ...ASSET_PATHS.map( (path) =%3E utils_paths.base(path) ),%0A        'node_modules'%0A      %5D,%0A      extensions: %5B'*', '.js', '.jsx', '.json'%5D%0A    %7D,%0A
     module: 

bring back the components