'use strict';

var app;
var generator;
var seedText;

app = require('express')();
seedText = require('./lib/transcriptLoader');
generator = require('./lib/generator')(seedText);

/**
 * Ensures that the response "content" property is an array.
 * @param   {*} content
 * @returns {array}
 */
function normalizeResponseContent(content) {
    return content instanceof Array ? content : [content];
}

/**
 * Builds a "success" response object.
 * @param   {*} content
 * @returns {object}
 */
function buildSuccessResponse(content) {
    return {
        success: true,
        content: normalizeResponseContent(content),
    };
}

/**
 * Builds an "error" response object.
 * @param   {*} error
 * @returns {object}
 */
function buildErrorResponse(error) {
    return {
        success: false,
        content: normalizeResponseContent(error),
    };
}

/**
 * Generates an arbitrary number of paragraphs.
 */
app.get('/:version/paragraphs', function (req, res) {
    var count;

    // Default to 3 paragraphs.
    count = req.query.count || 3;

    res.status(200).json(buildSuccessResponse(generator.generateParagraphs(count)));
});

/**
 * Generates an arbitrary number of sentences.
 */
app.get('/:version/sentences', function (req, res) {
    var count;

    // Default to 3 sentences.
    count = req.query.count || 3;

    res.status(200).json(buildSuccessResponse(generator.generateSentences(count)));
});

/**
 * Generates an arbitrary number of words.
 */
app.get('/:version/words', function (req, res) {
    var count;

    // Default to 10 words.
    count = req.query.count || 10;

    res.status(200).json(buildSuccessResponse(generator.generateWords(count)));
});

/**
 * Handles internal errors.
 */
app.use(function (err, req, res) {
    var content;

    content = [err.name + ' ' + err.message];

    res.status(500).json(buildErrorResponse(content));
});

/**
 * Handles 404 errors.
 */
app.use(function (req, res) {
    var content;

    content = ['Unsupported endpoint'];

    res.status(404).json(buildErrorResponse(content));
});

/**
 * Starts the server.
 */
app.listen(process.env.PORT, function () {
    console.log('Application started.');
});

@@ -1692,16 +1692,11 @@
 les 
-internal
+404
  err
@@ -1726,13 +1726,8 @@
 on (
-err, 
 req,
@@ -1738,73 +1738,8 @@
 ) %7B%0A
-    var content;%0A%0A    content = %5Berr.name + ' ' + err.message%5D;%0A%0A
     
@@ -1753,11 +1753,11 @@
 tus(
-500
+404
 ).js
@@ -1770,39 +1770,54 @@
 ldErrorResponse(
-content
+'Unsupported endpoint'
 ));%0A%7D);%0A%0A/**%0A * 
@@ -1816,35 +1816,40 @@
 %0A/**%0A * Handles 
-404
+internal
  errors.%0A */%0Aapp
@@ -1855,32 +1855,37 @@
 p.use(function (
+err, 
 req, res) %7B%0A    
@@ -1888,114 +1888,90 @@
     
-var content;%0A%0A    content = %5B'Unsupported endpoint'%5D;%0A%0A    res.status(404).json(buildErrorResponse(content
+res.status(err.status %7C%7C 500).json(buildErrorResponse(err.name + ' ' + err.message
 ));%0A

Fix 404 / error handling.