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

// src/features/commit/application/prompt.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 getPromptMarkdownAssetDedupKey(asset) {
  return isPromptMarkdownFileReference(asset) ? `file:${path.normalize(asset.filePath)}` : `inline:${asset.trim()}`;
}

// src/platform/prompts/build-common-prompt.ts
createPromptMarkdownFileAsset(
  import.meta.url,
  "src/platform/prompts/assets/common/base-instructions.md"
);
function buildCommonPrompt(options) {
  return {
    autoYes: options.autoYes,
    forms: options.forms,
    historyReferences: options.historyReferences,
    instructions: options.instructions,
    rules: options.rules,
    sections: options.sections,
    service: options.service,
    title: options.title,
    workspaceSnapshot: options.workspaceSnapshot
  };
}
function normalizePromptFilePath(filePath) {
  return path.normalize(filePath).replace(/\\/g, "/").trim();
}
function getNormalizedFileNamePatterns(group) {
  return (group.fileNamePatterns || []).map((pattern) => pattern.trim()).filter(Boolean);
}
function getNormalizedRules(group) {
  return (group.rules || []).filter((asset) => typeof asset === "string" ? Boolean(asset.trim()) : Boolean(asset.filePath.trim()));
}
function createFileMatchCandidates(filePath) {
  const normalized = normalizePromptFilePath(filePath);
  if (!normalized) {
    return [];
  }
  const baseName = path.posix.basename(normalized);
  return normalized === baseName ? [normalized] : [normalized, baseName];
}
function compileFileNamePattern(pattern) {
  try {
    return new RegExp(pattern);
  } catch (error) {
    throw new Error(
      `\uC720\uD6A8\uD558\uC9C0 \uC54A\uC740 fileNamePatterns \uC815\uADDC\uC2DD\uC785\uB2C8\uB2E4: ${pattern}
${error instanceof Error ? error.message : String(error)}`
    );
  }
}
function normalizePromptTargetFilePaths(filePaths) {
  return filePaths.map(normalizePromptFilePath).filter(Boolean);
}
function matchesPromptRuleGroup(group, targetFilePaths) {
  const normalizedPatterns = getNormalizedFileNamePatterns(group);
  if (normalizedPatterns.length === 0) {
    return true;
  }
  if (targetFilePaths.length === 0) {
    return false;
  }
  return normalizedPatterns.some((pattern) => {
    const regex = compileFileNamePattern(pattern);
    return targetFilePaths.some(
      (filePath) => createFileMatchCandidates(filePath).some((candidate) => regex.test(candidate))
    );
  });
}
function resolvePromptRules(ruleAssets, targetFilePaths = []) {
  if (!ruleAssets || ruleAssets.length === 0) {
    return void 0;
  }
  const normalizedTargetFilePaths = normalizePromptTargetFilePaths(targetFilePaths);
  const resolvedRules = [];
  const seenRules = /* @__PURE__ */ new Set();
  for (const asset of ruleAssets) {
    const nextRules = !isPromptRuleGroupAsset(asset) ? [asset] : matchesPromptRuleGroup(asset, normalizedTargetFilePaths) ? getNormalizedRules(asset) : [];
    for (const rule of nextRules) {
      const dedupeKey = getPromptMarkdownAssetDedupKey(rule);
      if (seenRules.has(dedupeKey)) {
        continue;
      }
      seenRules.add(dedupeKey);
      resolvedRules.push(rule);
    }
  }
  return resolvedRules.length > 0 ? resolvedRules : void 0;
}

// src/features/commit/application/prompt.ts
function buildCommitPrompt(options) {
  return buildCommonPrompt({
    autoYes: options.autoYes,
    forms: options.promptAssets?.forms,
    historyReferences: options.historyReferences,
    instructions: options.promptAssets?.instructions,
    rules: resolvePromptRules(options.promptAssets?.rules, options.targetFilePaths),
    sections: [
      {
        heading: "\uCEE4\uBC0B \uB300\uC0C1 diff \uCC38\uC870",
        markdown: [
          "- \uD604\uC7AC \uCEE4\uBC0B \uB300\uC0C1 \uC804\uCCB4 diff\uB294 \uC544\uB798 markdown \uD30C\uC77C\uC5D0 \uC800\uC7A5\uB418\uC5B4 \uC788\uB2E4.",
          `- diff \uD30C\uC77C: \`${path.normalize(options.diffReferencePath)}\``,
          "- \uC2E0\uADDC \uD30C\uC77C\uC774 \uD3EC\uD568\uB418\uC5B4 \uC788\uC73C\uBA74 diff \uD30C\uC77C\uC758 untracked files \uC139\uC158\uACFC \uC2E4\uC81C \uC6CC\uD06C\uC2A4\uD398\uC774\uC2A4 \uD30C\uC77C\uC744 \uD568\uAED8 \uD655\uC778\uD55C\uB2E4."
        ].join("\n")
      },
      {
        heading: "\uCEE4\uBC0B \uC694\uCCAD",
        markdown: [
          "- \uD604\uC7AC \uC6CC\uD06C\uC2A4\uD398\uC774\uC2A4 \uBCC0\uACBD \uC804\uCCB4\uB97C \uC810\uAC80\uD558\uACE0 \uCEE4\uBC0B \uADDC\uCE59\uC5D0 \uB9DE\uB294 \uD55C \uC904 \uBA54\uC2DC\uC9C0\uB97C \uC0DD\uC131\uD55C\uB2E4.",
          "- \uC2E4\uC81C `git add`, `git commit`, `git log -1 --stat` \uC2E4\uD589\uC740 `/commit` \uBA85\uB839 \uC2E4\uD589 \uD750\uB984\uC774 \uC218\uD589\uD55C\uB2E4.",
          "- \uC751\uB2F5\uC5D0\uB294 \uC81C\uC548 \uCEE4\uBC0B \uBA54\uC2DC\uC9C0\uC640 \uD310\uB2E8 \uADFC\uAC70\uB9CC \uD3EC\uD568\uD55C\uB2E4."
        ].join("\n")
      }
    ],
    service: options.service,
    title: "Ju-hong-e Commit",
    workspaceSnapshot: options.workspaceSnapshot
  });
}

export { buildCommitPrompt };
