import { Utility } from '../../core/utility';
import './checkbox.scss';

var CHECKBOX_CLASS = 'checkbox';
var CHECKBOX_INITIALIZED_CLASS = 'checkbox--initialized';

@Utility({
  selector: 'input[type="checkbox"]',
})
export class Checkbox {

  constructor(element) {
    if (!element) {
      throw new Error('Checkbox utility cannot be setup without an element!');
    }

    if (element.classList.contains(CHECKBOX_INITIALIZED_CLASS)) {
      // throw new Error('Checkbox utility already initialized!');
      return false;
    }

    if (element.parentElement.classList.contains(CHECKBOX_CLASS)) {
      // throw new Error('Checkbox element\'s wrapper already has class '' + CHECKBOX_CLASS + ''!');
      return false;
    }

    var siblingEl = element.nextSibling;
    var parentEl = element.parentElement;

    var wrapperEl = document.createElement('div');
    wrapperEl.classList.add(CHECKBOX_CLASS);

    var labelEl = document.createElement('label');
    labelEl.setAttribute('for', element.id);

    wrapperEl.appendChild(element);
    wrapperEl.appendChild(labelEl);

    parentEl.insertBefore(wrapperEl, siblingEl);

    element.classList.add(CHECKBOX_INITIALIZED_CLASS);
  }
}
