import hash from '@emotion/hash';
import { computed, ref } from 'vue';
import { ATTR_TOKEN, CSS_IN_JS_INSTANCE, useStyleInject } from '../StyleContext.mjs';
import { flattenToken, token2key } from '../util.mjs';
import useClientCache from './useGlobalCache.mjs';

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[${ATTR_TOKEN}="${key}"]`);
    styles.forEach((style) => {
      var _a;
      if (style[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 = ref({})) {
  const style = useStyleInject();
  const mergedToken = computed(() => Object.assign({}, ...tokens.value));
  const tokenStr = computed(() => flattenToken(mergedToken.value));
  const overrideTokenStr = computed(() => flattenToken(option.value.override || EMPTY_OVERRIDE));
  const cachedToken = useClientCache(
    "token",
    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 = 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;
}

export { useCacheToken as default, getComputedToken };
//# sourceMappingURL=useCacheToken.mjs.map
