'use strict';

var fs = require('fs');
var path = require('path');
var url = require('url');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var fs__default = /*#__PURE__*/_interopDefault(fs);
var path__default = /*#__PURE__*/_interopDefault(path);

// src/platform/prompts/markdown-assets.ts
var PACKAGE_JSON_FILE_NAME = "package.json";
function findPackageRoot(moduleFilePath) {
  let currentDirectoryPath = path__default.default.dirname(moduleFilePath);
  while (!fs__default.default.existsSync(path__default.default.join(currentDirectoryPath, PACKAGE_JSON_FILE_NAME))) {
    const parentDirectoryPath = path__default.default.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 = url.fileURLToPath(importMetaUrl);
  const packageRoot = findPackageRoot(moduleFilePath);
  return path__default.default.normalize(path__default.default.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__default.default.normalize(asset.filePath)
  } : asset;
}
function getPromptMarkdownAssetDedupKey(asset) {
  return isPromptMarkdownFileReference(asset) ? `file:${path__default.default.normalize(asset.filePath)}` : `inline:${asset.trim()}`;
}

exports.clonePromptMarkdownAsset = clonePromptMarkdownAsset;
exports.createPromptMarkdownFileAsset = createPromptMarkdownFileAsset;
exports.getPromptMarkdownAssetDedupKey = getPromptMarkdownAssetDedupKey;
exports.isPromptMarkdownFileReference = isPromptMarkdownFileReference;
exports.isPromptRuleGroupAsset = isPromptRuleGroupAsset;
exports.resolvePromptPackageFilePath = resolvePromptPackageFilePath;
