'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var react = require('react');
var _typeof = require('@babel/runtime/helpers/typeof');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

var _typeof__default = /*#__PURE__*/_interopDefaultCompat(_typeof);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultCompat(_slicedToArray);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck

var withPreset = function withPreset(preset) {
  var b = 'ek-';
  var e = '__';
  var m = '_';
  var v = '_';
  var getCSSModuleClassName = function getCSSModuleClassName(cls) {
    return cls;
  };
  if (_typeof__default.default(preset) === 'object' && preset !== null) {
    if (typeof preset.b === 'string') {
      b = preset.b;
    }
    if (typeof preset.e === 'string') {
      e = preset.e;
    }
    if (typeof preset.m === 'string') {
      m = preset.m;
    }
    if (typeof preset.v === 'string') {
      v = preset.v;
    }
    if (_typeof__default.default(preset.css) === 'object' && preset.css !== null) {
      getCSSModuleClassName = function getCSSModuleClassName(cls) {
        return preset.css[cls];
      };
    }
  }
  var getBlockClassName = function getBlockClassName(name, elem) {
    if (elem) {
      return b + name + e + elem;
    }
    return b + name;
  };
  var getModClassName = function getModClassName(blockName, modName, modValue) {
    if (modValue) {
      return blockName + m + modName + v + modValue;
    }
    return blockName + m + modName;
  };
  return function (name, elem) {
    if (typeof name !== 'string') {
      throw new Error('The argument "name" is not a string');
    }
    var block = getBlockClassName(name, elem);
    return function (mods, mix) {
      var modsExtended = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
      var result = '';
      var appendResult = function appendResult(cls) {
        var CSSModuleClassName = getCSSModuleClassName(cls);
        if (CSSModuleClassName) {
          if (result) {
            result += ' ';
          }
          result += CSSModuleClassName;
        }
      };
      appendResult(block);
      var appendFn = function appendFn(modKey, modVal) {
        if (modVal.indexOf(' ') !== -1) {
          var splitVal = modVal.split(' ');
          if (splitVal.length > 1 && Object.prototype.hasOwnProperty.call(modsExtended, modKey)) {
            for (var index = 0; index < splitVal.length; index++) {
              var extendedVal = modsExtended[modKey];
              var extendedKey = extendedVal[index];
              if (extendedKey) {
                if (Array.isArray(extendedKey)) {
                  for (var i = 0; i < extendedKey.length; i++) {
                    var extKey = extendedKey[i];
                    var extIndex = extendedVal.indexOf(extKey);
                    if (i === 0 || extIndex !== -1 && !splitVal[extIndex]) {
                      appendResult(getModClassName(block, extKey, splitVal[index]));
                    }
                  }
                } else {
                  appendResult(getModClassName(block, extendedKey, splitVal[index]));
                }
              }
            }
          }
        } else {
          appendResult(getModClassName(block, modKey, modVal));
        }
      };
      if (_typeof__default.default(mods) === 'object') {
        // eslint-disable-next-line no-restricted-syntax
        for (var key in mods) {
          if (Object.prototype.hasOwnProperty.call(mods, key)) {
            var modVal = mods[key];
            if (typeof modVal === 'string' || typeof modVal === 'number') {
              appendFn(key, '' + modVal);
            } else if (typeof modVal === 'boolean') {
              if (modVal) {
                appendResult(getModClassName(block, key));
              }
            } else if (_typeof__default.default(modVal) === 'object') {
              var k = void 0;
              if (Array.isArray(modVal)) {
                for (k = 0; k < modVal.length; k++) {
                  if (typeof modVal[k] === 'string' || typeof modVal[k] === 'number') {
                    appendFn(key, '' + modVal[k]);
                  }
                }
              } else {
                // eslint-disable-next-line no-restricted-syntax
                for (k in modVal) {
                  if (modVal[k]) {
                    appendFn(key, '' + k);
                  }
                }
              }
            }
          }
        }
      }
      if (mix) {
        if (typeof mix === 'string') {
          if (result) {
            result += ' ';
          }
          result += mix;
        } else if (Array.isArray(mix)) {
          for (var i = 0; i < mix.length; i++) {
            if (result) {
              result += ' ';
            }
            result += mix[i];
          }
        }
      }
      return result;
    };
  };
};
withPreset();

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck

var flatten = function flatten(arr) {
  var flattend = [];
  var _flat = function flat(array) {
    array.forEach(function (el) {
      if (Array.isArray(el)) {
        _flat(el);
      } else if (flattend.indexOf(el) === -1) {
        flattend.push(el);
      }
    });
  };
  _flat(arr);
  return flattend;
};
var getMapPropModsExtend = function getMapPropModsExtend(blockMods) {
  return blockMods.reduce(function (acc, modName) {
    if (Array.isArray(modName) && modName.length > 0) {
      var _modName = _slicedToArray__default.default(modName, 2),
        key = _modName[0],
        value = _modName[1];
      acc[key] = value;
    }
    return acc;
  }, {});
};

/* eslint-disable react/prop-types, @typescript-eslint/no-empty-object-type */

var createBlock = function createBlock() {
  var tagName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'div';
  var blockName = arguments.length > 1 ? arguments[1] : undefined;
  var blockMods = arguments.length > 2 ? arguments[2] : undefined;
  var blockPreset = arguments.length > 3 ? arguments[3] : undefined;
  var blockDefaultProps = arguments.length > 4 ? arguments[4] : undefined;
  var PREFIX = blockName.replace(/__/, '-');
  var BLOCK_PROP_AS = "".concat(PREFIX, "-as");
  var MODS_BLOCK_PROPS = flatten(blockMods);
  var MODS_BLOCK_PROPS_EXTENDED = getMapPropModsExtend(blockMods);
  var MODS_BLOCK_PROPS_WITH_PREFIX = MODS_BLOCK_PROPS.map(function (val) {
    return "".concat(PREFIX, "-").concat(val);
  });

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore-next-line
  var createClassName = withPreset(blockPreset)(blockName);
  var Block = /*#__PURE__*/react.forwardRef(function (props, ref) {
    var elementType = tagName;
    var elementProps = {};
    var modsProps = {};
    if (blockDefaultProps) {
      // eslint-disable-next-line no-restricted-syntax
      for (var key in blockDefaultProps) {
        if (key === BLOCK_PROP_AS) {
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore-next-line
          elementType = blockDefaultProps[key];
          continue;
        }
        var index = MODS_BLOCK_PROPS_WITH_PREFIX.indexOf(key);
        if (index !== -1) {
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore-next-line
          modsProps[MODS_BLOCK_PROPS[index]] = blockDefaultProps[key];
        } else {
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore-next-line
          elementProps[key] = blockDefaultProps[key];
        }
      }
    }

    // eslint-disable-next-line no-restricted-syntax
    for (var _key in props) {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore-next-line
      if (props[_key] === undefined) {
        continue;
      }
      if (_key === BLOCK_PROP_AS) {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore-next-line
        elementType = props[_key];
        continue;
      }
      var _index = MODS_BLOCK_PROPS_WITH_PREFIX.indexOf(_key);
      if (_index !== -1) {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore-next-line
        modsProps[MODS_BLOCK_PROPS[_index]] = props[_key];
      } else {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore-next-line
        elementProps[_key] = props[_key];
      }
    }

    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore-next-line
    elementProps.ref = ref;
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore-next-line
    elementProps.className = createClassName(modsProps, props.className, MODS_BLOCK_PROPS_EXTENDED);
    return /*#__PURE__*/react.createElement(elementType, elementProps);
  });
  Block.displayName = blockName;
  return Block;
};

exports.createBlock = createBlock;
