import { readFile } from "fs/promises";
import { existsSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { SpecVerseResourceModel } from "../models/SpecVerseResource.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
class ResourcesProviderService {
  resourceCatalog = /* @__PURE__ */ new Map();
  resourcesPath;
  isInitialized = false;
  constructor(resourcesPath) {
    if (resourcesPath) {
      this.resourcesPath = resourcesPath;
    } else {
      const candidatePaths = [
        join(__dirname, "../resources"),
        // NPM package structure
        join(__dirname, "../../resources")
        // Development structure
      ];
      this.resourcesPath = candidatePaths[0];
      try {
        for (const path of candidatePaths) {
          if (existsSync(path)) {
            this.resourcesPath = path;
            break;
          }
        }
      } catch (error) {
        this.resourcesPath = candidatePaths[0];
      }
    }
  }
  async initializeResources() {
    if (this.isInitialized) {
      return;
    }
    const resources = [
      {
        uri: "specverse://examples/chat-prompts",
        name: "Chat Prompt Examples",
        description: "Example prompts for terminal/chat-based AI interactions",
        mimeType: "text/markdown"
      },
      {
        uri: "specverse://examples/api-calls",
        name: "API Integration Examples",
        description: "Examples for API-based AI integrations",
        mimeType: "application/javascript"
      }
    ];
    for (const resourceData of resources) {
      const resource = SpecVerseResourceModel.create(resourceData);
      this.resourceCatalog.set(resource.uri, resource);
    }
    this.isInitialized = true;
  }
  async listResources() {
    await this.initializeResources();
    return Array.from(this.resourceCatalog.values()).map((resource) => ({
      uri: resource.uri,
      name: resource.name,
      description: resource.description,
      mimeType: resource.mimeType,
      content: resource.content,
      metadata: resource.metadata
    }));
  }
  async getResourceContent(uri) {
    await this.initializeResources();
    const resource = this.resourceCatalog.get(uri);
    if (!resource) {
      throw new Error(`Resource not found: ${uri}`);
    }
    if (resource.isLoaded()) {
      return resource.content;
    }
    const filePath = this.resolveResourcePath(uri);
    try {
      const content = await readFile(filePath, "utf-8");
      const updatedResource = resource.withContent(content);
      this.resourceCatalog.set(uri, updatedResource);
      return content;
    } catch (error) {
      throw new Error(`Failed to load resource ${uri}: ${error instanceof Error ? error.message : String(error)}`);
    }
  }
  async readResource(uri) {
    try {
      const content = await this.getResourceContent(uri);
      const resource = this.resourceCatalog.get(uri);
      return {
        content: [{
          type: "resource",
          resource: {
            uri: resource.uri,
            name: resource.name,
            description: resource.description,
            mimeType: resource.mimeType,
            content
          }
        }]
      };
    } catch (error) {
      return {
        content: [{
          type: "text",
          text: `Error reading resource: ${error instanceof Error ? error.message : String(error)}`
        }],
        isError: true
      };
    }
  }
  resolveResourcePath(uri) {
    switch (uri) {
      // Schemas and catalogs removed - now provided through AI tools
      case "specverse://examples/chat-prompts":
        return join(this.resourcesPath, "examples/chat-prompts/ecommerce-store-example.md");
      case "specverse://examples/api-calls":
        return join(this.resourcesPath, "examples/api-calls/orchestrator-workflow-example.js");
      default:
        throw new Error(`Unknown resource URI: ${uri}`);
    }
  }
  isResourceAvailable(uri) {
    return this.resourceCatalog.has(uri);
  }
  getCachedResourceCount() {
    return Array.from(this.resourceCatalog.values()).filter((r) => r.isLoaded()).length;
  }
}
export {
  ResourcesProviderService
};
