'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

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

var Router = require('koa-router');
var MemoryFile = require('../components/MemoryFile');
var Fs = require('fs');
var Path = require('path');
var Config = require('../components/Config');
var Builder = require('../components/Builder');
var httpRouter = Router();

httpRouter.get('/source/*', _regenerator2.default.mark(function _callee(next) {
    var path, query, file;
    return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
            switch (_context.prev = _context.next) {
                case 0:
                    path = this.params[0];
                    query = this.request.url.split('?');

                    query = query[1] ? '?' + query[1] : '';
                    file = MemoryFile.get(path + query);

                    if (file) {
                        this.response.status = 200;
                        this.response.contentType = 'text/javascript';
                        this.response.body = file.getContent();
                    } else {
                        this.response.status = 404;
                    }

                case 5:
                case 'end':
                    return _context.stop();
            }
        }
    }, _callee, this);
}));
function exists(file) {
    return new Promise(function (resolve, reject) {
        Fs.exists(file, function (flag) {
            resolve(flag);
        });
    });
}
var bundleDir = Path.join(__dirname, '../../frontend/', Config.bundleDir);
httpRouter.get('/' + Config.bundleDir + '/*', _regenerator2.default.mark(function _callee2(next) {
    var ext, dir, basename, bundle, we, targetPath;
    return _regenerator2.default.wrap(function _callee2$(_context2) {
        while (1) {
            switch (_context2.prev = _context2.next) {
                case 0:
                    ext = Path.extname(this.params[0]);

                    if (!(ext == '.js' || ext == '.we')) {
                        _context2.next = 28;
                        break;
                    }

                    dir = Path.dirname(this.params[0]);
                    basename = Path.basename(this.params[0], ext);
                    bundle = Path.join(bundleDir, dir, basename + '.js');
                    we = Path.join(Config.root || bundleDir, dir, basename + '.we');
                    _context2.next = 8;
                    return exists(bundle);

                case 8:
                    if (!_context2.sent) {
                        _context2.next = 14;
                        break;
                    }

                    this.response.status = 200;
                    this.type = 'text/javascript';
                    this.response.body = Fs.createReadStream(bundle);
                    _context2.next = 26;
                    break;

                case 14:
                    _context2.next = 16;
                    return exists(we);

                case 16:
                    if (!_context2.sent) {
                        _context2.next = 25;
                        break;
                    }

                    _context2.next = 19;
                    return Builder[Config.buildMode](we, dir);

                case 19:
                    targetPath = _context2.sent;

                    this.response.status = 200;
                    this.type = 'text/javascript';
                    this.response.body = Fs.createReadStream(targetPath);
                    _context2.next = 26;
                    break;

                case 25:
                    this.response.status = 404;

                case 26:
                    _context2.next = 29;
                    break;

                case 28:
                    this.response.status = 404;

                case 29:
                case 'end':
                    return _context2.stop();
            }
        }
    }, _callee2, this);
}));
module.exports = httpRouter;