'use strict';

var fs          = require('fs')
var template    = fs.readFileSync(__dirname + '/../templates/key.html', 'utf8')
var handlebars  = require('handlebars')
var keyTemplate = handlebars.compile(template)

/**
 * Class to handle reading and writing to the map key dom structure
 */
class MapKey {

  /**
   * @constructor
   */
  constructor(config) {
    this.config = config
    this.title_ = ''
    this.items = []
    this.domElement = document.getElementById(config.domElement)
    this.domElement.innerHTML = keyTemplate()
  }

  /**
   * Used to set the title of the key
   * @param {string} title
   */
  set title(title) {
    this.title_ = title
    this.domElement.querySelector('.title').innerHTML = title
  }

  /**
   * Returns the title of the key
   */
  get title() {
    return this.title_
  }

  /**
   * Clears all key dom elements
   */
  clearDomElements() {
    this.domElement.querySelector('.items').innerHTML = ''
  }

  /**
   * Renders all key item data from memory onto the dom
   * creates a dom node for each item and inserts into list
   */
  renderDomElements() {
    this.items.forEach((item) => {
      this.renderDomElement(item)
    })
  }

  /**
   * Renders a single dom list element into items list from item object
   * @param {object} item
   */
  renderDomElement(item) {
    var li = document.createElement('li')
    li.innerHTML = item.value
    this.domElement.querySelector('.items').appendChild(li)
  }

  /**
   * Adds item by some unique id. Stores the item by id and renders item
   * to the dom
   * @param {string} id
   * @param {object} item
   */
  addItem(id, item) {
    item = {key: id, value: item}
    this.items.push(item)
    this.renderDomElement(item)
  }

  /**
   * Removes an item by unique id. Trims back the internal collection of items
   * before clearing and re-rendering the collection to the dom
   * @param {string} id
   */
  removeItem(id) {
    var length = this.items.length
    this.items = this.items.filter((item) => {
      return item.key !== id
    })
    if (length !== this.items.length) {
      this.clearDomElements()
      this.renderDomElements()
    }
  }
}

module.exports = function (config) {
  return new MapKey(config)
}

@@ -206,16 +206,52 @@
 emplate)
+%0Avar assert      = require('assert')
 %0A%0A/**%0A *
@@ -666,24 +666,94 @@
 le(title) %7B%0A
+    assert.equal(typeof title, 'string', '%60title%60 must be a string')%0A%0A
     this.tit
@@ -1684,38 +1684,38 @@
 id%0A   * @param %7B
-object
+string
 %7D item%0A   */%0A  a
@@ -1729,24 +1729,155 @@
 id, item) %7B%0A
+    assert.equal(typeof id, 'string', '%60id%60 must be a string')%0A    assert.equal(typeof item, 'string', '%60item%60 must be a string')%0A%0A
     item = %7B
@@ -2155,24 +2155,88 @@
 eItem(id) %7B%0A
+    assert.equal(typeof id, 'string', '%60id%60 must be a string')%0A%0A
     var leng
@@ -2455,16 +2455,16 @@
 %0A  %7D%0A%7D%0A%0A
-
 module.e
@@ -2492,16 +2492,158 @@
 nfig) %7B%0A
+  assert.equal(typeof config, 'object', '%60config%60 must be an object')%0A  assert.ok(!!config.domElement, '%60config.domElement%60 must be defined')%0A
   return

Update to harden external facing interfaces