// @ts-check

let { CheckerPlugin, TsConfigPathsPlugin } = require('awesome-typescript-loader');
let LiveReloadPlugin = require('webpack-livereload-plugin');
var HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
let path = require('path');

let create = (entries, tsConfig, outDir, filename) => {
  return {
    entry: entries,
    devtool: 'source-map',
    resolve: {
      extensions: ['.ts', '.js'],
      plugins: [
        new TsConfigPathsPlugin({
          baseUrl: '.',
          compiler: 'typescript',
          configFileName: tsConfig
        })
      ]
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          use: [
            'source-map-loader'
          ],
          enforce: 'pre'
        },
        {
          test: /\.ts$/,
          use: [
            {
              loader: 'awesome-typescript-loader',
              options: {
                transpileOnly: true,
                configFileName: tsConfig
              }
            }
          ]
        }
      ]
    },
    plugins: [
      new LiveReloadPlugin(),
      new HardSourceWebpackPlugin()
    ],
    output: {
      filename: typeof entries === 'string' ? filename : "[name]/" + filename,
      path: path.resolve(outDir)
    }
  };
};

let buildDemoEntries = (pluginNames, type, demo) => pluginNames.reduce(
  (acc, name) => {
    acc[name] = `src/${type}/${name}/demo/ts/demo/${demo}`;
    return acc;
  }, {}
)

let buildEntries = (pluginNames, type, entry) => pluginNames.reduce(
  (acc, name) => {
    acc[name] = `src/${type}/${name}/main/ts/${entry}`;
    return acc;
  }, {}
)

let createPlugin = (name) => {
  return create(`src/plugins/${name}/demo/ts/demo/Demo.ts`, 'tsconfig.plugin.json', `scratch/demos/plugins/${name}/`, 'demo.js');
};

let createTheme = (name) => {
  return create(`src/themes/${name}/demo/ts/demo/Demos.ts`, 'tsconfig.theme.json', `scratch/demos/themes/${name}`, 'demo.js');
};

let allPluginDemos = (plugins) => {
  return create(buildDemoEntries(plugins, 'plugins', 'Demo.ts'), 'tsconfig.plugin.json', 'scratch/demos/plugins', 'demo.js')
}

let allThemeDemos = (themes) => {
  return create(buildDemoEntries(themes, 'themes', 'Demos.ts'), 'tsconfig.theme.json', 'scratch/demos/themes', 'demo.js')
}

let all = (plugins, themes) => {
  return [
    allPluginDemos(plugins),
    allThemeDemos(themes),
    create(`src/core/demo/ts/demo/Demos.ts`, 'tsconfig.json', 'scratch/demos/core/', 'demo.js'),
    create('src/core/main/ts/api/Main.ts', 'tsconfig.json', 'js/tinymce/', 'tinymce.js'),
    create(buildEntries(plugins, 'plugins', 'Plugin.ts'), 'tsconfig.plugin.json', 'js/tinymce/plugins', 'plugin.js'),
    create(buildEntries(themes, 'themes', 'Theme.ts'), 'tsconfig.theme.json', 'js/tinymce/themes', 'theme.js')
  ];
}

let generateDemoIndex = (grunt, app, plugins, themes) => {
  let demoList = grunt.file.expand(['src/**/demo/html/*.html'])
  let links = `<ul>${demoList.map(link => `<li><a href="${link}">${link}</a></li>`).join('')}</ul>`
  let html = `
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Demos</title>
  </head>
  <body>
    ${links}
  </body>
  </html>
  `

  app.get('/', (req, res) => res.send(html))
}

module.exports = {
  createPlugin,
  createTheme,
  create,
  all,
  allPluginDemos,
  allThemeDemos,
  generateDemoIndex
};

@@ -2915,49 +2915,405 @@
 let 
-links = %60%3Cul%3E$%7BdemoList.map(link =%3E %60%3Cli%3E
+sortedDemos = demoList.reduce((acc, link) =%3E %7B%0A    const type = link.split('/')%5B1%5D;%0A%0A    if (!acc%5Btype%5D) %7B%0A      acc%5Btype%5D = %5B%5D;%0A    %7D%0A%0A    acc%5Btype%5D.push(link)%0A%0A    return acc;%0A  %7D, %7B%7D)%0A%0A  let lists = Object.keys(sortedDemos).map(%0A    type =%3E %60%0A    %3Ch2%3E$%7Btype%7D%3C/h2%3E%0A    %3Cul%3E%0A      $%7BsortedDemos%5Btype%5D.map(%0A        link =%3E %60%3Cli%3E$%7Btype !== 'core' ? %60%3Cstrong%3E$%7Blink.split('/')%5B2%5D%7D%3C/strong%3E - %60 : ''%7D
 %3Ca h
@@ -3328,20 +3328,35 @@
 ink%7D%22%3E$%7B
+path.basename(
 link
+)
 %7D%3C/a%3E%3C/l
@@ -3372,15 +3372,41 @@
 ('')
-%7D%3C/ul%3E%60
+%0A      %7D%0A    %3C/ul%3E%60%0A  ).join('');
 %0A  l
@@ -3675,18 +3675,18 @@
     $%7Bli
-nk
+st
 s%7D%0A  %3C/b

make generated demo index page prettier