/**
 * Bud constructor.
 * {{about}}
 * @memberof module:coz-bud/lib
 * @inner
 * @constructor Bud
 * @param {object} config - Bud configuration.{{#each properties}}{{#if configurable}}
 * @param {{braces type}} {{#if optional}}[{{/if}}config.{{name}}{{#if default}}={{{default}}}{{/if}}{{#if optional}}]{{/if}} - {{description}}.{{/if}}{{/each}}
 */

'use strict'

const copying = require('./copying')
const assert = require('assert')

/** @lends Bud */
function Bud (config) {
  copying.copy(config, this)
  copying.fallbackCopy(Bud.defaults, this)
  this.validate()
}

Bud.prototype = {
{{#each properties}}
  /**
   * {{description}}
   * @type {{braces type}}
   */
  {{name}}: {{#if default}}{{{default}}}{{else}}undefined{{/if}},
{{/each}}
  /**
   * Validate this configuration.
   * @throws Will throw an error if configuration invalid.
   */
  validate () {
    const _assertType = (value, type, msg) => {
      if (typeof value === 'undefined') {
        return
      }
      const valid = !!~type.split(/\|/g).indexOf(typeof(value))
      assert(valid, msg)
    }

{{#each properties}}    _assertType(this.{{name}}, '{{{type}}}', 'bud.{{name}} should be {{type}}.')
{{/each}}
  }
}

module.exports = Bud
