var ELEMENT = '__';
var MODS = '--';

function join(name, el, symbol) {
  return el ? name + symbol + el : name;
}

function prefix(name, mods) {
  if (typeof mods === 'string') {
    return [join(name, mods, MODS)];
  }

  if (Array.isArray(mods)) {
    return mods.map(function (item) {
      return prefix(name, item);
    });
  }

  var res = [];

  if (mods) {
    Object.keys(mods).forEach(function (key) {
      if (mods[key]) {
        res.push(name + MODS + key);
      }
    });
  }

  return res;
}

export function createBEM(name) {
  return function (el, mods) {
    if (el && typeof el !== 'string') {
      mods = el;
      el = '';
    }

    el = join(name, el, ELEMENT);
    var bemClass = mods ? [el].concat(prefix(el, mods)) : [el];
    return bemClass;
  };
}