const fs = require('fs');
const path = require('path');
const packageJson = require('../package');
const handlebars = require('handlebars');

function compile(isDevMode = false) {
  const dataPath = path.resolve(__dirname, 'data.json');
  const data = JSON.parse(fs.readFileSync(dataPath).toString());
  const templatePath = path.resolve(__dirname, 'index.html.handlebars');
  const template = fs.readFileSync(templatePath).toString();
  const delegate = handlebars.compile(template);
  let extendedData;
  if (isDevMode) {
    extendedData = data.dev;
  } else {
    extendedData = data.prod;
  }
  extendedData.appBundle = './bundle.js';
  extendedData.appTitle = packageJson.productName;

  return delegate(extendedData);
}

exports.compile = compile;
