const overrideCache = /* @__PURE__ */ new Map();
const CACHE_TTL = process.env.NODE_ENV === "development" ? 5 * 60 * 1e3 : 30 * 60 * 1e3;
function getCacheKey(componentPath, themeName) {
  return `${themeName}:${componentPath}`;
}
function isCacheValid(entry) {
  return Date.now() - entry.timestamp < CACHE_TTL;
}
async function resolveComponentOverride(componentPath, theme) {
  var _a, _b;
  const cacheKey = getCacheKey(componentPath, theme.name);
  const cached = overrideCache.get(cacheKey);
  if (cached && isCacheValid(cached)) {
    console.log(`[Override] Cache hit for ${componentPath} in theme ${theme.name}`);
    return cached.component;
  }
  try {
    const override = (_b = (_a = theme.components) == null ? void 0 : _a.overrides) == null ? void 0 : _b[componentPath];
    if (!override) {
      overrideCache.set(cacheKey, {
        component: null,
        timestamp: Date.now(),
        theme: theme.name
      });
      return null;
    }
    overrideCache.set(cacheKey, {
      component: override,
      timestamp: Date.now(),
      theme: theme.name
    });
    console.log(`[Override] Resolved override for ${componentPath} in theme ${theme.name}`);
    return override;
  } catch (error) {
    console.error(`[Override] Error resolving override for ${componentPath}:`, error);
    overrideCache.set(cacheKey, {
      component: null,
      timestamp: Date.now(),
      theme: theme.name
    });
    return null;
  }
}
async function resolveComponent(componentPath, originalComponent, theme) {
  const override = await resolveComponentOverride(componentPath, theme);
  if (override) {
    console.log(`[Override] Using override for ${componentPath}`);
    return override;
  }
  console.log(`[Override] Using original component for ${componentPath}`);
  return originalComponent;
}
function createOverridableComponent(componentPath, originalComponent) {
  return originalComponent;
}
function clearOverrideCache(componentPath, themeName) {
  if (componentPath && themeName) {
    const cacheKey = getCacheKey(componentPath, themeName);
    overrideCache.delete(cacheKey);
    console.log(`[Override] Cleared cache for ${componentPath} in theme ${themeName}`);
  } else if (themeName) {
    for (const [key] of overrideCache) {
      if (key.startsWith(`${themeName}:`)) {
        overrideCache.delete(key);
      }
    }
    console.log(`[Override] Cleared all cache entries for theme ${themeName}`);
  } else {
    overrideCache.clear();
    console.log("[Override] Cleared entire override cache");
  }
}
function getOverrideCacheStats() {
  return {
    size: overrideCache.size,
    entries: Array.from(overrideCache.keys())
  };
}
async function preloadThemeOverrides(theme) {
  var _a;
  if (!((_a = theme.components) == null ? void 0 : _a.overrides)) {
    return;
  }
  const overridePaths = Object.keys(theme.components.overrides);
  console.log(`[Override] Preloading ${overridePaths.length} overrides for theme ${theme.name}`);
  const promises = overridePaths.map(async (path) => {
    try {
      await resolveComponentOverride(path, theme);
    } catch (error) {
      console.warn(`[Override] Failed to preload ${path}:`, error);
    }
  });
  await Promise.allSettled(promises);
  console.log(`[Override] Preloading complete for theme ${theme.name}`);
}
export {
  clearOverrideCache,
  createOverridableComponent,
  getOverrideCacheStats,
  preloadThemeOverrides,
  resolveComponent,
  resolveComponentOverride
};
