'use strict';

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

var hash = require('@emotion/hash');
var vue = require('vue');
var StyleContext = require('../StyleContext.cjs');
var util = require('../util.cjs');
var useGlobalCache = require('./useGlobalCache.cjs');

const EMPTY_OVERRIDE = {};
const hashPrefix = process.env.NODE_ENV !== "production" ? "css-dev-only-do-not-override" : "css";
const tokenKeys = /* @__PURE__ */ new Map();
function recordCleanToken(tokenKey) {
  tokenKeys.set(tokenKey, (tokenKeys.get(tokenKey) || 0) + 1);
}
function removeStyleTags(key, instanceId) {
  if (typeof document !== "undefined") {
    const styles = document.querySelectorAll(`style[${StyleContext.ATTR_TOKEN}="${key}"]`);
    styles.forEach((style) => {
      var _a;
      if (style[StyleContext.CSS_IN_JS_INSTANCE] === instanceId)
        (_a = style.parentNode) == null ? void 0 : _a.removeChild(style);
    });
  }
}
const TOKEN_THRESHOLD = 0;
function cleanTokenStyle(tokenKey, instanceId) {
  tokenKeys.set(tokenKey, (tokenKeys.get(tokenKey) || 0) - 1);
  const tokenKeyList = Array.from(tokenKeys.keys());
  const cleanableKeyList = tokenKeyList.filter((key) => {
    const count = tokenKeys.get(key) || 0;
    return count <= 0;
  });
  if (tokenKeyList.length - cleanableKeyList.length > TOKEN_THRESHOLD) {
    cleanableKeyList.forEach((key) => {
      removeStyleTags(key, instanceId);
      tokenKeys.delete(key);
    });
  }
}
function getComputedToken(originToken, overrideToken, theme, format) {
  const derivativeToken = theme.getDerivativeToken(originToken);
  let mergedDerivativeToken = {
    ...derivativeToken,
    ...overrideToken
  };
  if (format)
    mergedDerivativeToken = format(mergedDerivativeToken);
  return mergedDerivativeToken;
}
function useCacheToken(theme, tokens, option = vue.ref({})) {
  const style = StyleContext.useStyleInject();
  const mergedToken = vue.computed(() => Object.assign({}, ...tokens.value));
  const tokenStr = vue.computed(() => util.flattenToken(mergedToken.value));
  const overrideTokenStr = vue.computed(() => util.flattenToken(option.value.override || EMPTY_OVERRIDE));
  const cachedToken = useGlobalCache.default(
    "token",
    vue.computed(() => [
      option.value.salt || "",
      theme.value.id,
      tokenStr.value,
      overrideTokenStr.value
    ]),
    () => {
      const {
        salt = "",
        override = EMPTY_OVERRIDE,
        formatToken,
        getComputedToken: compute
      } = option.value;
      const mergedDerivativeToken = compute ? compute(mergedToken.value, override, theme.value) : getComputedToken(mergedToken.value, override, theme.value, formatToken);
      const tokenKey = util.token2key(mergedDerivativeToken, salt);
      mergedDerivativeToken._tokenKey = tokenKey;
      recordCleanToken(tokenKey);
      const hashId = `${hashPrefix}-${hash(tokenKey)}`;
      mergedDerivativeToken._hashId = hashId;
      return [mergedDerivativeToken, hashId];
    },
    (cache) => {
      var _a;
      cleanTokenStyle(cache[0]._tokenKey, (_a = style.value) == null ? void 0 : _a.cache.instanceId);
    }
  );
  return cachedToken;
}

exports.default = useCacheToken;
exports.getComputedToken = getComputedToken;
//# sourceMappingURL=useCacheToken.cjs.map
