import { existsSync, readFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { ResourcesProviderService } from "./ResourcesProviderService.js";
import { EmbeddedResourcesAdapter } from "./EmbeddedResourcesAdapter.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
class HybridResourcesProvider {
  provider;
  mode;
  constructor(config) {
    this.mode = this.detectMode(config);
    if (this.mode === "embedded") {
      if (!config.embeddedResources) {
        this.provider = new EmbeddedResourcesAdapter({});
      } else {
        this.provider = new EmbeddedResourcesAdapter(config.embeddedResources);
      }
    } else {
      this.provider = new ResourcesProviderService(config.resourcesPath);
    }
  }
  detectMode(config) {
    if (config.mode === "filesystem") return "filesystem";
    if (config.mode === "embedded") return "embedded";
    if (config.embeddedResources) {
      return "embedded";
    }
    if (this.hasEmbeddedResourcesFile()) {
      return "embedded";
    }
    if (this.isWebEnvironment()) {
      return "embedded";
    }
    const resourcesPath = config.resourcesPath || join(__dirname, "../../resources");
    if (existsSync(resourcesPath)) {
      return "filesystem";
    }
    if (this.tryImportEmbeddedResources()) {
      return "embedded";
    }
    return "filesystem";
  }
  hasEmbeddedResourcesFile() {
    try {
      const embeddedPath2 = join(dirname(__dirname), "embedded-resources.js");
      return existsSync(embeddedPath2);
    } catch {
      return false;
    }
  }
  isWebEnvironment() {
    try {
      if (typeof window !== "undefined") return true;
      return this.hasEmbeddedResourcesFile();
    } catch {
      return false;
    }
  }
  loadEmbeddedResources() {
    try {
      const embeddedPath = join(dirname(__dirname), "embedded-resources.js");
      if (existsSync(embeddedPath)) {
        const content = readFileSync(embeddedPath, "utf-8");
        const match = content.match(/export const EMBEDDED_RESOURCES = ({[\s\S]*?});/);
        if (match) {
          const embeddedResources = eval("(" + match[1] + ")");
          return embeddedResources;
        }
      }
      return null;
    } catch (error) {
      console.warn("Failed to load embedded resources:", error);
      return null;
    }
  }
  async loadEmbeddedResourcesAsync() {
    try {
      const embeddedPath2 = join(dirname(__dirname), "embedded-resources.js");
      if (existsSync(embeddedPath2)) {
        const embeddedModule = await import(embeddedPath2);
        return embeddedModule.EMBEDDED_RESOURCES || null;
      }
      return null;
    } catch (error) {
      console.warn("Dynamic import of embedded resources failed:", error);
      return null;
    }
  }
  tryImportEmbeddedResources() {
    return this.loadEmbeddedResources();
  }
  async initializeResources() {
    if (this.mode === "embedded" && this.provider instanceof EmbeddedResourcesAdapter) {
      const providerInfo = this.provider.getEmbeddedResourcesInfo();
      if (providerInfo.count === 0) {
        try {
          const embeddedResources2 = await this.loadEmbeddedResourcesAsync();
          if (embeddedResources2) {
            this.provider = new EmbeddedResourcesAdapter(embeddedResources2);
          }
        } catch (error) {
          console.warn("Failed to load embedded resources, falling back to filesystem:", error);
          this.mode = "filesystem";
          this.provider = new ResourcesProviderService();
        }
      }
    }
    return this.provider.initializeResources();
  }
  async listResources() {
    return this.provider.listResources();
  }
  async getResourceContent(uri) {
    return this.provider.getResourceContent(uri);
  }
  async readResource(uri) {
    return this.provider.readResource(uri);
  }
  isResourceAvailable(uri) {
    return this.provider.isResourceAvailable(uri);
  }
  getCachedResourceCount() {
    return this.provider.getCachedResourceCount();
  }
  getMode() {
    return this.mode;
  }
  getProviderInfo() {
    const info = {
      mode: this.mode,
      type: this.provider.constructor.name,
      resourcesInfo: void 0
    };
    if (this.provider instanceof EmbeddedResourcesAdapter) {
      info.resourcesInfo = this.provider.getEmbeddedResourcesInfo();
    } else if (this.provider instanceof ResourcesProviderService) {
      info.resourcesInfo = {
        cached: this.provider.getCachedResourceCount()
      };
    }
    return info;
  }
}
export {
  HybridResourcesProvider
};
