import { computed } from 'vue';

const defaultNamespace = 'fes';
const statePrefix = 'is-';
const _bem = (namespace, block, blockSuffix, element, modifier) => {
  let cls = `${namespace}-${block}`;
  if (blockSuffix) {
    cls += `-${blockSuffix}`;
  }
  if (element) {
    cls += `__${element}`;
  }
  if (modifier) {
    cls += `--${modifier}`;
  }
  return cls;
};
const useNamespace = block => {
  const namespace = computed(() => defaultNamespace);
  const b = function () {
    let blockSuffix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    return _bem(namespace.value, block, blockSuffix, '', '');
  };
  const e = element => element ? _bem(namespace.value, block, '', element, '') : '';
  const m = modifier => modifier ? _bem(namespace.value, block, '', '', modifier) : '';
  const be = (blockSuffix, element) => blockSuffix && element ? _bem(namespace.value, block, blockSuffix, element, '') : '';
  const em = (element, modifier) => element && modifier ? _bem(namespace.value, block, '', element, modifier) : '';
  const bm = (blockSuffix, modifier) => blockSuffix && modifier ? _bem(namespace.value, block, blockSuffix, '', modifier) : '';
  const bem = (blockSuffix, element, modifier) => blockSuffix && element && modifier ? _bem(namespace.value, block, blockSuffix, element, modifier) : '';
  const is = function (name) {
    for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      args[_key - 1] = arguments[_key];
    }
    const state = args.length >= 1 ? args[0] : true;
    return name && state ? `${statePrefix}${name}` : '';
  };
  return {
    namespace,
    b,
    e,
    m,
    be,
    em,
    bm,
    bem,
    is
  };
};

export { defaultNamespace, useNamespace };
