import _extends from "@babel/runtime/helpers/esm/extends";
var _globalObject$documen;
import createEmotion from '@emotion/css/create-instance';
import extraScopePlugin from 'stylis-plugin-extra-scope';
import { globalObject } from '@skbkontur/global-object';
import { Upgrade } from "../../Upgrades";
var REACT_UI_PREFIX = Upgrade.getSpecificityClassName();
var scope = new Array(Upgrade.getSpecificityLevel()).fill("." + REACT_UI_PREFIX).join('');
var _createEmotion = createEmotion({
    key: REACT_UI_PREFIX,
    prepend: true,
    stylisPlugins: scope ? [extraScopePlugin(scope)] : undefined,
    container: (_globalObject$documen = globalObject.document) == null ? void 0 : _globalObject$documen.head
  }),
  flush = _createEmotion.flush,
  hydrate = _createEmotion.hydrate,
  cx = _createEmotion.cx,
  merge = _createEmotion.merge,
  getRegisteredStyles = _createEmotion.getRegisteredStyles,
  injectGlobal = _createEmotion.injectGlobal,
  keyframes = _createEmotion.keyframes,
  css = _createEmotion.css,
  sheet = _createEmotion.sheet,
  cache = _createEmotion.cache;
export { flush, hydrate, cx, merge, getRegisteredStyles, injectGlobal, keyframes, css, sheet, cache };
function isZeroArgs(fn) {
  return fn.length === 0;
}
var memoize = function memoize(fn) {
  if (isZeroArgs(fn)) {
    var isCalled = false;
    var result;
    return function () {
      if (!isCalled) {
        isCalled = true;
        result = fn();
      }
      return result;
    };
  }
  var cache = new WeakMap();
  return function (arg) {
    if (!cache.has(arg)) {
      cache.set(arg, fn(arg));
    }
    return cache.get(arg);
  };
};
export var memoizeStyle = function memoizeStyle(styles) {
  Object.keys(styles).forEach(function (className) {
    return styles[className] = memoize(styles[className]);
  });
  return styles;
};
export var prefix = function prefix(component, app) {
  if (app === void 0) {
    app = REACT_UI_PREFIX;
  }
  return function (classes) {
    return Object.keys(classes).reduce(function (acc, key) {
      var _extends2;
      return _extends({}, acc, (_extends2 = {}, _extends2[key] = app + "-" + component + "-" + classes[key], _extends2));
    }, {});
  };
};