"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dataSourceCache = /* @__PURE__ */ new Map();
const dictionaryCache = /* @__PURE__ */ new Map();
const pendingRequests = /* @__PURE__ */ new Map();
const DEFAULT_CACHE_TIME = 10 * 60 * 1e3;
function getDataSourceCache(dataSourceId) {
  const cached = dataSourceCache.get(dataSourceId);
  if (!cached) return null;
  if (Date.now() - cached.timestamp > cached.expiresIn) {
    dataSourceCache.delete(dataSourceId);
    return null;
  }
  return cached.data;
}
function setDataSourceCache(dataSourceId, data, expiresIn = DEFAULT_CACHE_TIME) {
  dataSourceCache.set(dataSourceId, {
    data,
    timestamp: Date.now(),
    expiresIn
  });
}
function getDictionaryCache(dictionaryKey) {
  const cached = dictionaryCache.get(dictionaryKey);
  if (!cached) return null;
  if (Date.now() - cached.timestamp > cached.expiresIn) {
    dictionaryCache.delete(dictionaryKey);
    return null;
  }
  return cached.data;
}
function setDictionaryCache(dictionaryKey, data, expiresIn = DEFAULT_CACHE_TIME) {
  dictionaryCache.set(dictionaryKey, {
    data,
    timestamp: Date.now(),
    expiresIn
  });
}
async function getDataSourceWithConcurrencyControl(dataSourceId, fetchFn) {
  const cachedData = getDataSourceCache(dataSourceId);
  if (cachedData !== null) {
    return cachedData;
  }
  const requestKey = `dataSource:${dataSourceId}`;
  if (pendingRequests.has(requestKey)) {
    return pendingRequests.get(requestKey);
  }
  const requestPromise = fetchFn().then((data) => {
    setDataSourceCache(dataSourceId, data);
    return data;
  }).finally(() => {
    pendingRequests.delete(requestKey);
  });
  pendingRequests.set(requestKey, requestPromise);
  return requestPromise;
}
async function getDictionaryWithConcurrencyControl(dictionaryKey, fetchFn) {
  const cachedData = getDictionaryCache(dictionaryKey);
  if (cachedData !== null) {
    return cachedData;
  }
  const requestKey = `dictionary:${dictionaryKey}`;
  if (pendingRequests.has(requestKey)) {
    return pendingRequests.get(requestKey);
  }
  const requestPromise = fetchFn().then((data) => {
    setDictionaryCache(dictionaryKey, data);
    return data;
  }).finally(() => {
    pendingRequests.delete(requestKey);
  });
  pendingRequests.set(requestKey, requestPromise);
  return requestPromise;
}
exports.getDataSourceCache = getDataSourceCache;
exports.getDataSourceWithConcurrencyControl = getDataSourceWithConcurrencyControl;
exports.getDictionaryCache = getDictionaryCache;
exports.getDictionaryWithConcurrencyControl = getDictionaryWithConcurrencyControl;
exports.setDataSourceCache = setDataSourceCache;
exports.setDictionaryCache = setDictionaryCache;
