import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

// src/platform/prompts/markdown-assets.ts
var PACKAGE_JSON_FILE_NAME = "package.json";
function findPackageRoot(moduleFilePath) {
  let currentDirectoryPath = path.dirname(moduleFilePath);
  while (!fs.existsSync(path.join(currentDirectoryPath, PACKAGE_JSON_FILE_NAME))) {
    const parentDirectoryPath = path.dirname(currentDirectoryPath);
    if (parentDirectoryPath === currentDirectoryPath) {
      throw new Error(`markdown \uC790\uC0B0 package root\uB97C \uCC3E\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4. start=${moduleFilePath}`);
    }
    currentDirectoryPath = parentDirectoryPath;
  }
  return currentDirectoryPath;
}
function resolvePromptPackageFilePath(importMetaUrl, packageRelativeFilePath) {
  const moduleFilePath = fileURLToPath(importMetaUrl);
  const packageRoot = findPackageRoot(moduleFilePath);
  return path.normalize(path.resolve(packageRoot, packageRelativeFilePath));
}
function createPromptMarkdownFileAsset(importMetaUrl, packageRelativeFilePath) {
  return {
    filePath: resolvePromptPackageFilePath(importMetaUrl, packageRelativeFilePath),
    kind: "file-reference"
  };
}
function isPromptMarkdownFileReference(asset) {
  return typeof asset !== "string";
}
function isPromptRuleGroupAsset(asset) {
  return typeof asset !== "string" && !("kind" in asset && asset.kind === "file-reference");
}
function clonePromptMarkdownAsset(asset) {
  return isPromptMarkdownFileReference(asset) ? {
    ...asset,
    filePath: path.normalize(asset.filePath)
  } : asset;
}
function getPromptMarkdownAssetDedupKey(asset) {
  return isPromptMarkdownFileReference(asset) ? `file:${path.normalize(asset.filePath)}` : `inline:${asset.trim()}`;
}

export { clonePromptMarkdownAsset, createPromptMarkdownFileAsset, getPromptMarkdownAssetDedupKey, isPromptMarkdownFileReference, isPromptRuleGroupAsset, resolvePromptPackageFilePath };
