var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
function sameDerivativeOption(left, right) {
  if (left.length !== right.length)
    return false;
  for (let i = 0; i < left.length; i++) {
    if (left[i] !== right[i])
      return false;
  }
  return true;
}
const _ThemeCache = class _ThemeCache {
  constructor() {
    __publicField(this, "cache");
    __publicField(this, "keys");
    __publicField(this, "cacheCallTimes");
    this.cache = /* @__PURE__ */ new Map();
    this.keys = [];
    this.cacheCallTimes = 0;
  }
  size() {
    return this.keys.length;
  }
  internalGet(derivativeOption, updateCallTimes = false) {
    let cache = { map: this.cache };
    derivativeOption.forEach((derivative) => {
      var _a;
      if (!cache)
        cache = void 0;
      else
        cache = (_a = cache == null ? void 0 : cache.map) == null ? void 0 : _a.get(derivative);
    });
    if ((cache == null ? void 0 : cache.value) && updateCallTimes)
      cache.value[1] = this.cacheCallTimes++;
    return cache == null ? void 0 : cache.value;
  }
  get(derivativeOption) {
    var _a;
    return (_a = this.internalGet(derivativeOption, true)) == null ? void 0 : _a[0];
  }
  has(derivativeOption) {
    return !!this.internalGet(derivativeOption);
  }
  set(derivativeOption, value) {
    if (!this.has(derivativeOption)) {
      if (this.size() + 1 > _ThemeCache.MAX_CACHE_SIZE + _ThemeCache.MAX_CACHE_OFFSET) {
        const [targetKey] = this.keys.reduce((result, key) => {
          const [, callTimes] = result;
          if (this.internalGet(key)[1] < callTimes)
            return [key, this.internalGet(key)[1]];
          return result;
        }, [this.keys[0], this.cacheCallTimes]);
        this.delete(targetKey);
      }
      this.keys.push(derivativeOption);
    }
    let cache = this.cache;
    derivativeOption.forEach((derivative, index) => {
      if (index === derivativeOption.length - 1) {
        cache.set(derivative, { value: [value, this.cacheCallTimes++] });
      } else {
        const cacheValue = cache.get(derivative);
        if (!cacheValue)
          cache.set(derivative, { map: /* @__PURE__ */ new Map() });
        else if (!cacheValue.map)
          cacheValue.map = /* @__PURE__ */ new Map();
        cache = cache.get(derivative).map;
      }
    });
  }
  deleteByPath(currentCache, derivatives) {
    var _a;
    const cache = currentCache.get(derivatives[0]);
    if (derivatives.length === 1) {
      if (!cache.map)
        currentCache.delete(derivatives[0]);
      else
        currentCache.set(derivatives[0], { map: cache.map });
      return (_a = cache.value) == null ? void 0 : _a[0];
    }
    const result = this.deleteByPath(cache.map, derivatives.slice(1));
    if ((!cache.map || cache.map.size === 0) && !cache.value)
      currentCache.delete(derivatives[0]);
    return result;
  }
  delete(derivativeOption) {
    if (this.has(derivativeOption)) {
      this.keys = this.keys.filter((item) => !sameDerivativeOption(item, derivativeOption));
      return this.deleteByPath(this.cache, derivativeOption);
    }
    return void 0;
  }
};
__publicField(_ThemeCache, "MAX_CACHE_SIZE", 20);
__publicField(_ThemeCache, "MAX_CACHE_OFFSET", 5);
let ThemeCache = _ThemeCache;

export { ThemeCache as default, sameDerivativeOption };
//# sourceMappingURL=ThemeCache.mjs.map
