"use strict";

var _fs = _interopRequireDefault(require("fs"));

var _path = _interopRequireDefault(require("path"));

var _qrcode = _interopRequireDefault(require("qrcode"));

var _express = _interopRequireDefault(require("express"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var rpath = function rpath(p) {
  return _path["default"].join(__dirname, p);
}; // when installed from npm, the "www" folder contains
// the pre-compiled assets


var preBuilt = _fs["default"].existsSync(rpath('www')) && rpath('www');

module.exports = function (app) {
  app.get('/qr/:data', function (req, res) {
    return _qrcode["default"].toFileStream(res.type('png'), req.params.data);
  });

  if (preBuilt) {
    var html = _fs["default"].readFileSync(_path["default"].join(preBuilt, 'index.html')).toString().replace(/\{\{manifestKey\}\}/g, app.settings.manifestKey).replace(/\{\{accessKey\}\}/g, app.settings.accessKey);

    app.get('/', function (req, res) {
      return res.send(html);
    });
    app.use('/', _express["default"]["static"](preBuilt));
  } else require('../client/serve')(app);
};