var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import createEmotion from '@emotion/css/create-instance';
import extraScopePlugin from 'stylis-plugin-extra-scope';
import { Upgrade } from '../Upgrades.js';
export var REACT_UI_PREFIX = Upgrade.getSpecificityClassName();
var scope = new Array(Upgrade.getSpecificityLevel()).fill(".".concat(REACT_UI_PREFIX)).join('');
export var getEmotion = function (_a) {
    var _b = _a.key, key = _b === void 0 ? REACT_UI_PREFIX : _b, container = _a.container, nonce = _a.nonce;
    return createEmotion({
        key: key,
        prepend: true,
        stylisPlugins: scope ? [extraScopePlugin(scope)] : undefined,
        container: container,
        nonce: nonce,
    });
};
function isZeroArgs(fn) {
    return fn.length === 0;
}
var memoize = function (fn) {
    if (isZeroArgs(fn)) {
        var isCalled_1 = false;
        var result_1;
        return function () {
            if (!isCalled_1) {
                isCalled_1 = true;
                result_1 = fn();
            }
            return result_1;
        };
    }
    var cache = new WeakMap();
    return function (arg) {
        if (!cache.has(arg)) {
            cache.set(arg, fn(arg));
        }
        return cache.get(arg);
    };
};
export var memoizeStyle = function (styles) {
    Object.keys(styles).forEach(function (className) { return (styles[className] = memoize(styles[className])); });
    return styles;
};
export var memoizeGetStyles = function (getStyles) {
    var stylesCache = new WeakMap();
    return function (emotion) {
        if (!stylesCache.has(emotion)) {
            stylesCache.set(emotion, memoizeStyle(getStyles(emotion)));
        }
        return stylesCache.get(emotion);
    };
};
export var prefix = function (component, app) {
    if (app === void 0) { app = REACT_UI_PREFIX; }
    return function (classes) {
        return Object.keys(classes).reduce(function (acc, key) {
            var _a;
            return __assign(__assign({}, acc), (_a = {}, _a[key] = "".concat(app, "-").concat(component, "-").concat(classes[key]), _a));
        }, {});
    };
};
//# sourceMappingURL=Emotion.js.map