'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _nodeBits = require('node-bits');

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

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

var pathToFile = function pathToFile(file) {
  return _path2.default.resolve() + '/node_modules/node-bits-admin/lib/admin/' + file;
};

var isFile = function isFile(url) {
  return (/\.(js|css|woff|woff2|eot|svg|ttf|)$/.test(_path2.default.basename(url))
  );
};

var adminSpa = {
  get: function get(req, res) {
    var file = pathToFile('index.html');
    if (isFile(req.url)) {
      file = pathToFile(_path2.default.basename(req.url));
    }

    _fs2.default.access(file, _fs2.default.constants.R_OK, function (err) {
      if (err) {
        (0, _nodeBits.logError)(err);
        res.status(404).send();
        return;
      }

      res.status(200).sendFile(file);
    });
  }
};

exports.default = function () {
  var adminRoot = '/admin';
  return [{
    verb: _nodeBits.GET,
    route: adminRoot,
    implementation: adminSpa
  }, {
    verb: _nodeBits.GET,
    route: adminRoot + '*',
    implementation: adminSpa
  }];
};