const async = require('async');
const path = require('path');
const glob = require('glob');
const basePath = process.cwd();

module.exports = (section, callback) => {

  // Task flow control
  async.parallel([

    // Get file deps for top level page
    async.apply(getContentList, section),

    // Get file deps for children if required
    (cb) => async.map(section.children, getContentList, cb)

  ], done);

  // On complete handler
  function done(err, result) {
    if (err) {
      callback(err);
    } else {
      callback(null, section)
    }
  }
}

function getContentList(page, cb) {
  glob(path.join(basePath, page.contentSrc), (err, filenames) => {
    if (err) {
      cb(err)
    } else {
      page.fileDependencies = filenames;
      cb();
    }
  });
}
