import "../../chunks/chunk.QU3DSPNU.js";
import { promises as fs } from "fs";
import * as path from "node:path";
import { fileURLToPath } from "url";
import { PATHS, SERVER_CONFIG } from "./constants.js";
import { DataLoadError, logError } from "./errors.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function getMcpDataPath() {
  return path.resolve(__dirname, "../../generated/mcp");
}
async function getPackageVersion() {
  try {
    const packageJsonPath = path.resolve(
      __dirname,
      `../../${PATHS.PACKAGE_JSON}`
    );
    const content = await fs.readFile(packageJsonPath, "utf-8");
    const pkg = JSON.parse(content);
    return pkg.version || SERVER_CONFIG.DEFAULT_VERSION;
  } catch (error) {
    logError(error, "getPackageVersion");
    return SERVER_CONFIG.DEFAULT_VERSION;
  }
}
async function loadJsonFile(filePath) {
  try {
    const content = await fs.readFile(filePath, "utf-8");
    return JSON.parse(content);
  } catch (error) {
    throw new DataLoadError(
      `Failed to load JSON file: ${error instanceof Error ? error.message : "unknown error"}`,
      filePath
    );
  }
}
async function loadComponentsIndex() {
  try {
    const dataPath = getMcpDataPath();
    return await loadJsonFile(
      path.join(dataPath, PATHS.COMPONENTS_INDEX)
    );
  } catch (error) {
    logError(error, "loadComponentsIndex");
    return null;
  }
}
async function loadIconsIndex() {
  try {
    const dataPath = getMcpDataPath();
    return await loadJsonFile(
      path.join(dataPath, PATHS.ICONS_INDEX)
    );
  } catch (error) {
    logError(error, "loadIconsIndex");
    return null;
  }
}
async function loadGlobalIndex() {
  try {
    const dataPath = getMcpDataPath();
    return await loadJsonFile(
      path.join(dataPath, PATHS.GLOBAL_INDEX)
    );
  } catch (error) {
    logError(error, "loadGlobalIndex");
    return null;
  }
}
function normalizeComponentName(name) {
  const lower = name.toLowerCase().trim();
  if (lower.startsWith("gds-")) {
    return lower;
  }
  if (lower.startsWith("icon-")) {
    return `gds-${lower}`;
  }
  return `gds-${lower}`;
}
function findComponent(name, components) {
  const normalized = normalizeComponentName(name);
  let found = components.find((c) => c.tagName === normalized);
  if (found) return found;
  const withoutPrefix = name.replace(/^gds-/, "");
  found = components.find((c) => c.tagName === `gds-${withoutPrefix}`);
  if (found) return found;
  const lowerName = name.toLowerCase();
  found = components.find(
    (c) => c.tagName.toLowerCase().includes(lowerName) || c.name.toLowerCase().includes(lowerName)
  );
  return found || null;
}
function findIcon(name, icons) {
  const normalized = normalizeComponentName(name);
  let found = icons.find((i) => i.tagName === normalized);
  if (found) return found;
  const withoutPrefix = name.replace(/^gds-/, "").replace(/^icon-/, "");
  found = icons.find((i) => i.tagName === `gds-icon-${withoutPrefix}`);
  if (found) return found;
  const lowerName = name.toLowerCase();
  found = icons.find(
    (i) => i.tagName.toLowerCase().includes(lowerName) || i.name.toLowerCase().includes(lowerName)
  );
  return found || null;
}
async function readMcpFile(relativePath) {
  try {
    const dataPath = getMcpDataPath();
    const fullPath = path.join(dataPath, relativePath);
    return await fs.readFile(fullPath, "utf-8");
  } catch (error) {
    logError(error, "readMcpFile");
    return null;
  }
}
async function fileExists(relativePath) {
  try {
    const dataPath = getMcpDataPath();
    const fullPath = path.join(dataPath, relativePath);
    await fs.access(fullPath);
    return true;
  } catch {
    return false;
  }
}
function buildResourceUri(category, name, docType) {
  if (docType) {
    return `green://${category}/${name}/${docType}`;
  }
  return `green://${category}/${name}`;
}
function parseResourceUri(uri) {
  const match = uri.match(/^green:\/\/([^/]+)\/([^/]+)(?:\/([^/]+))?$/);
  if (!match) return null;
  const [, category, name, docType] = match;
  return { category, name, docType };
}
export {
  buildResourceUri,
  fileExists,
  findComponent,
  findIcon,
  getMcpDataPath,
  getPackageVersion,
  loadComponentsIndex,
  loadGlobalIndex,
  loadIconsIndex,
  normalizeComponentName,
  parseResourceUri,
  readMcpFile
};
