/**
 * Colorpint context.
 * @memberof module:colorprint/lib
 * @inner
 * @constructor Colorprint
 * @param {object} config - Context configuration.
 */

'use strict'

const formatMsg = require('./msg/format_msg')
const decorateMsg = require('./msg/decorate_msg')
const indentMsg = require('./msg/indent_msg')

/** @lends module:colorprint/lib~Colorprint */
function Colorprint () {
  this.init.apply(this, arguments)
}

Colorprint.prototype = {
  disabled: false,
  prepareMsg () {
    const msg = formatMsg.apply(formatMsg, arguments)
    return [this.PREFIX, indentMsg(msg, this.indent), this.SUFFIX].join('')
  },
  writeToStdout (msg, color) {
    if (this.disabled) {
      return
    }
    console.log(decorateMsg(msg, color))
  },
  writeToStderr (msg, color) {
    console.error(decorateMsg(msg, color))
  },
{{#each levels}}
  /** Color for {{lowercase @key}} print. */
  {{uppercase @key}}_COLOR: '{{color}}',
{{/each}}
{{#each levels}}
  /** Alias for module:colorprint/lib~Colorprint#{{lowercase @key}}. */
  {{uppercase @key}} () {
    this.{{lowercase @key}}.apply(this, arguments)
  },
{{/each}}
  /** @constructs module:colorprint/lib~Colorprint */
  init (config = {}) {
    Object.assign(this, config)
    this.PREFIX = config.prefix || this.PREFIX
    this.SUFFIX = config.suffix || this.SUFFIX
  },
  /** Number of indent */
  indent: 0,
  /** Message prefix */
  PREFIX: '',
  /** Message suffix */
  SUFFIX: '',
{{#each levels}}
  /**
   * Print {{lowercase @key}} message.
   * @param {...string} msg - Message to print.
   */
  {{camelcase @key}} (msg) {
    this.{{#if error}}writeToStderr{{else}}writeToStdout{{/if}}(this.prepareMsg.apply(this, arguments), this.{{uppercase @key}}_COLOR)
  }{{#if @last}}{{else}},{{/if}}
{{/each}}
}

module.exports = Colorprint
