const rssPlugin = require('@11ty/eleventy-plugin-rss');
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const fs = require('fs');
const util = require('util')

// Import filters
const dateFilter = require('./src/filters/date-filter.js');
const markdownFilter = require('./src/filters/markdown-filter.js');
const w3DateFilter = require('./src/filters/w3-date-filter.js');

// Import transforms
const htmlMinTransform = require('./src/transforms/html-min-transform.js');
const parseTransform = require('./src/transforms/parse-transform.js');

// Import data files
const site = require('./src/_data/site.json');

module.exports = function(config) {
  // Filters
  config.addFilter('dateFilter', dateFilter);
  config.addFilter('markdownFilter', markdownFilter);
  config.addFilter('w3DateFilter', w3DateFilter);
  config.addFilter('dump', obj => {
    return util.inspect(obj)
  });

  // Layout aliases
  config.addLayoutAlias('home', 'layouts/home.njk');

  // Transforms
  config.addTransform('htmlmin', htmlMinTransform);
  config.addTransform('parse', parseTransform);

  // Passthrough copy
  config.addPassthroughCopy('src/fonts');
  config.addPassthroughCopy('src/images');
  config.addPassthroughCopy('src/js');
  config.addPassthroughCopy('src/admin/config.yml');
  config.addPassthroughCopy('src/admin/previews.js');
  config.addPassthroughCopy('node_modules/nunjucks/browser/nunjucks-slim.js');
  config.addPassthroughCopy('src/robots.txt');
  config.addPassthroughCopy('src/.htaccess');
  config.addPassthroughCopy('src/form');

  const now = new Date();

  // Custom collections
  const livePosts = post => post.date <= now && !post.data.draft;
  const newsPosts = post => post.data.type === 'news';
  const eventPosts = post => post.data.type === 'event';
  
  config.addCollection('posts', collection => {
    return [
      ...collection.getFilteredByGlob('./src/posts/*.md')
    ].reverse();
  });
  config.addCollection('news', collection => {
    return [
      ...collection.getFilteredByGlob('./src/posts/*.md').filter(newsPosts)
    ].reverse();
  });
  config.addCollection('events', collection => {
    return [
      ...collection.getFilteredByGlob('./src/posts/*.md').filter(eventPosts)
    ].reverse();
  });
  config.addCollection('newsFeed', collection => {
    return [...collection.getFilteredByGlob('./src/posts/*.md').filter(livePosts)]
      .reverse()
      .slice(0, site.maxNewsPerPage);
  });
  config.addCollection('members', collection => {
    return [...collection.getFilteredByGlob("./src/members/*.md")];
  });
  config.addCollection('profiles', collection => {
    return [...collection.getFilteredByGlob("./src/members/*.md")]
    .reverse()
    .slice(0, site.maxProfilePreview)
    ;
  });
  config.addCollection("tagsList", function(collectionApi) {
    const tagsList = new Set();
    collectionApi.getAll().map( item => {
        if (item.data.tags) { // handle pages that don't have tags
            item.data.tags.map( tag => tagsList.add(tag))
        }
    });
    return tagsList;
  });
  config.addCollection("skillsList", function(collectionApi) {
    const skillsList = new Set();
    collectionApi.getFilteredByGlob("./src/members/*.md").map( item => {
        if (item.data.tags) { // handle pages that don't have skills
          item.data.tags.map( skill => { // exclude non related tags
            if (['post', 'news', 'event'].indexOf(skill) == -1) {
              skillsList.add(skill)
            }
          })
        }
    });
    return skillsList;
  });
  config.addCollection("membersLocations", function(collectionApi) {
    return collectionApi.getFilteredByGlob("./src/members/*.md")
      .filter(item => typeof item.data.location !== 'undefined')
      .map( member => {
        return {
          name: member.data.name,
          url: member.data.url,
          location: member.data.location,
        };
    });
  });
  config.addCollection('customers', collection => {
    return [...collection.getFilteredByGlob("./src/customers/*.md")]
    .reverse()
    .slice(0, site.maxCustomerPerPage);
  });
  config.addCollection('partners', collection => {
    return [...collection.getFilteredByGlob("./src/partners/*.md")]
    .reverse()
    .slice(0, site.maxPartnerPerPage);
  });

  // Plugins
  config.addPlugin(rssPlugin);
  config.addPlugin(syntaxHighlight);

  // 404
  config.setBrowserSyncConfig({
    callbacks: {
      ready: function(err, browserSync) {
        const content_404 = fs.readFileSync('dist/404.html');

        browserSync.addMiddleware('*', (req, res) => {
          // Provides the 404 content without redirect.
          res.write(content_404);
          res.end();
        });
      }
    }
  });

  return {
    dir: {
      input: 'src',
      output: 'dist'
    },
    passthroughFileCopy: true,
  };
};
