'use strict'

var getPage = require('../page/loader')
var loadOptions = require('../default-options').load
var map = require('async.map')
var clone = require('lodash.clone')
var merge = require('lodash.merge')

/**
 * Given a spec, generates the pages.
 * @param  {Object}   spec     an spec object for each page.
 * @param  {Function} callback
 */
module.exports = function (spec, files, callback) {
  var simpleSpec = clone(spec)
  var speclateOptions = loadOptions(simpleSpec.options)
  delete simpleSpec.options
  delete simpleSpec.defaultSpec
  var pages = Object.keys(simpleSpec)

  map(pages, function (route, next) {
    var newSpec = {}
    if (spec.defaultSpec) {
      merge(newSpec, spec.defaultSpec, spec[route].spec)
    } else {
      newSpec = spec[route].spec
    }
    spec[route].spec = newSpec
    spec[route].route = route
    getPage(spec[route], speclateOptions, files, function (err, pageMarkup) {
      next(err, {
        name: route,
        spec: spec[route],
        markup: pageMarkup
      })
    })
  }, callback)
}

@@ -135,44 +135,8 @@
 p')%0A
-var clone = require('lodash.clone')%0A
 var 
@@ -271,16 +271,62 @@
 h page.%0A
+ * @param %7BObject%7D  options  the spec options%0A
  * @para
@@ -384,16 +384,25 @@
 n (spec,
+ options,
  files, 
@@ -424,147 +424,45 @@
 ar s
-impleSpec = clone(spec)%0A  var speclateOptions = loadOptions(simpleSpec.options)%0A  delete simpleSpec.options%0A  delete simpleSpec.defaultSpec
+peclateOptions = loadOptions(options)
 %0A  v
@@ -489,14 +489,8 @@
 ys(s
-impleS
 pec)
@@ -554,108 +554,8 @@
  %7B%7D%0A
-    if (spec.defaultSpec) %7B%0A      merge(newSpec, spec.defaultSpec, spec%5Broute%5D.spec)%0A    %7D else %7B%0A  
     
@@ -585,14 +585,8 @@
 pec%0A
-    %7D%0A
     

remove default spec