import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
import {
  __require
} from "../../../chunks/chunk-TG2EQLX2.js";

// builtin/extensions/changelog/index.ts
function getRecentCommits(cwd, count = 10) {
  try {
    const { spawnSync } = __require("child_process");
    const result = spawnSync(
      "git",
      ["log", `--max-count=${count}`, "--pretty=format:%h %s", "--no-merges"],
      { cwd, encoding: "utf8" }
    );
    return result.status === 0 ? result.stdout : "";
  } catch {
    return "";
  }
}
function getChangedFiles(cwd) {
  try {
    const { spawnSync } = __require("child_process");
    const result = spawnSync(
      "git",
      ["diff", "HEAD~5", "--name-only", "--stat"],
      { cwd, encoding: "utf8" }
    );
    return result.status === 0 ? result.stdout : "";
  } catch {
    return "";
  }
}
async function readChangelog(cwd) {
  try {
    const fs = await import("fs");
    const path = await import("path");
    const changelogPath = path.join(cwd, "CHANGELOG.md");
    if (fs.existsSync(changelogPath)) {
      return fs.readFileSync(changelogPath, "utf-8");
    }
  } catch {
  }
  return null;
}
function changelogExtension(ctx) {
  ctx.log("Changelog extension loaded");
  ctx.registerCommand({
    path: ["tools", "changelog"],
    description: "Smart changelog management with gap detection",
    handler: async (ctx2, args) => {
      const fs = await import("fs");
      const path = await import("path");
      const changelogPath = path.join(ctx2.cwd, "CHANGELOG.md");
      if (args?.includes("--show")) {
        if (!fs.existsSync(changelogPath)) {
          return "No CHANGELOG.md found.";
        }
        const content = fs.readFileSync(changelogPath, "utf-8");
        return "Current CHANGELOG:\n\n" + content.slice(0, 2e3) + (content.length > 2e3 ? "\n\n..." : "");
      }
      if (args?.trim() === "check") {
        const commits2 = getRecentCommits(ctx2.cwd, 15);
        const changelog2 = await readChangelog(ctx2.cwd);
        const changedFiles2 = getChangedFiles(ctx2.cwd);
        if (!commits2) {
          return "No git history found. Cannot check for missing entries.";
        }
        return `\u{1F50D} CHANGELOG \u9057\u6F0F\u68C0\u67E5

\u8BF7\u5206\u6790\u4EE5\u4E0B\u4FE1\u606F\uFF0C\u68C0\u67E5 CHANGELOG \u662F\u5426\u6709\u9057\u6F0F\uFF1A

## \u6700\u8FD1 Git \u63D0\u4EA4
${commits2 || "(\u65E0\u6CD5\u83B7\u53D6)"}

## \u53D8\u66F4\u6587\u4EF6
${changedFiles2 || "(\u65E0\u6CD5\u83B7\u53D6)"}

## \u5F53\u524D CHANGELOG
${changelog2 ? changelog2.slice(0, 1500) + "\n..." : "(\u6587\u4EF6\u4E0D\u5B58\u5728)"}

\u8BF7\u5BF9\u6BD4\u5206\u6790\uFF1A
1. \u54EA\u4E9B\u91CD\u8981\u7684\u53D8\u66F4\u5DF2\u7ECF\u63D0\u4EA4\u5230 git \u4F46\u6CA1\u6709\u8BB0\u5F55\u5728 CHANGELOG\uFF1F
2. \u5F53\u524D [Unreleased] \u90E8\u5206\u662F\u5426\u5B8C\u6574\u53CD\u6620\u4E86\u6700\u8FD1\u7684\u5DE5\u4F5C\uFF1F
3. \u5217\u51FA\u9057\u6F0F\u7684\u6761\u76EE\u5EFA\u8BAE\uFF08\u6309 Added/Changed/Fixed \u5206\u7C7B\uFF09

\u53EA\u8FD4\u56DE\u5206\u6790\u62A5\u544A\uFF0C\u4E0D\u8981\u4FEE\u6539\u6587\u4EF6\u3002`;
      }
      if (args?.startsWith("add ")) {
        const description = args.slice(4).trim();
        const existingChangelog = await readChangelog(ctx2.cwd);
        ctx2.ui.notify(`\u{1F4DD} \u6B63\u5728\u751F\u6210 changelog \u6761\u76EE...`, "info");
        return `\u8BF7\u5C06\u4EE5\u4E0B\u63CF\u8FF0\u8F6C\u6362\u4E3A\u4E13\u4E1A\u7684 CHANGELOG \u6761\u76EE\uFF1A

\u63CF\u8FF0\uFF1A"${description}"

${existingChangelog ? `
\u53C2\u8003\u73B0\u6709\u683C\u5F0F\uFF1A
\`\`\`
${existingChangelog.slice(0, 800)}
...
\`\`\`` : ""}

\u8981\u6C42\uFF1A
1. \u9009\u62E9\u5408\u9002\u7684\u5206\u7C7B\uFF1A**Added** / **Changed** / **Fixed** / **Removed** / **Deprecated** / **Security**
2. \u4F7F\u7528\u73B0\u5728\u65F6\u6001\uFF0C\u7B80\u6D01\u4F46\u5305\u542B\u5173\u952E\u6280\u672F\u7EC6\u8282
3. \u5982\u6709\u76F8\u5173\u6587\u4EF6\u8DEF\u5F84\uFF0C\u8BF7\u5305\u542B

\u683C\u5F0F\u793A\u4F8B\uFF1A
- **Added**: \u65B0\u589E\u7528\u6237\u8BA4\u8BC1\u6A21\u5757\uFF0C\u652F\u6301 JWT token \u9A8C\u8BC1\uFF08src/auth/\uFF09
- **Fixed**: \u4FEE\u590D\u5185\u5B58\u6CC4\u6F0F\u95EE\u9898\uFF0C\u4F18\u5316\u4E86\u5927\u6587\u4EF6\u5904\u7406\u6027\u80FD

\u4F7F\u7528 write \u5DE5\u5177\u66F4\u65B0 CHANGELOG.md\uFF1A
- \u5982\u679C\u4E0D\u5B58\u5728\uFF0C\u521B\u5EFA\u6807\u51C6\u683C\u5F0F\uFF08\u5305\u542B ## [Unreleased] \u90E8\u5206\uFF09
- \u5C06\u65B0\u6761\u76EE\u6DFB\u52A0\u5230 [Unreleased] \u90E8\u5206\u7684\u5408\u9002\u4F4D\u7F6E
- \u4FDD\u6301\u73B0\u6709\u6761\u76EE\u4E0D\u53D8`;
      }
      if (args?.startsWith("release")) {
        const versionHint = args.slice(7).trim();
        const existingChangelog = await readChangelog(ctx2.cwd);
        if (!existingChangelog) {
          return "No CHANGELOG.md found. Create one first with /changelog";
        }
        ctx2.ui.notify(`\u{1F4DD} Preparing release ${versionHint || "..."}`, "info");
        return `\u8BF7\u5C06 [Unreleased] \u90E8\u5206\u7684\u5185\u5BB9\u6574\u7406\u4E3A\u4E00\u4E2A\u65B0\u7248\u672C\u53D1\u5E03\u3002

\u5F53\u524D CHANGELOG\uFF1A
\`\`\`
${existingChangelog.slice(0, 1500)}
...
\`\`\`

\u7248\u672C\u63D0\u793A\uFF1A${versionHint || "\u6839\u636E\u53D8\u66F4\u5185\u5BB9\u81EA\u52A8\u5224\u65AD\uFF08patch/minor/major\uFF09"}

Semantic Versioning \u89C4\u5219\uFF1A
- **patch** (0.0.x): Bug fixes only
- **minor** (0.x.0): New features, backward compatible
- **major** (x.0.0): Breaking changes

\u4EFB\u52A1\uFF1A
1. \u5206\u6790 [Unreleased] \u4E2D\u7684\u53D8\u66F4\u7C7B\u578B
2. \u786E\u5B9A\u5408\u9002\u7684\u7248\u672C\u53F7\uFF08\u5982\u679C\u7528\u6237\u672A\u6307\u5B9A\uFF09
3. \u521B\u5EFA\u65B0\u7684\u7248\u672C\u8282\uFF08\u5982 ## [1.2.0] - 2024-03-06\uFF09
4. \u5C06 [Unreleased] \u7684\u5185\u5BB9\u79FB\u52A8\u5230\u65B0\u7248\u672C\u8282
5. \u6E05\u7A7A [Unreleased] \u90E8\u5206\uFF0C\u53EA\u4FDD\u7559\u7A7A\u5217\u8868
6. \u5728\u6587\u4EF6\u5E95\u90E8\u66F4\u65B0\u7248\u672C\u5BF9\u6BD4\u94FE\u63A5\uFF08\u5982\u679C\u9700\u8981\uFF09

\u4F7F\u7528 write \u5DE5\u5177\u66F4\u65B0 CHANGELOG.md`;
      }
      const commits = getRecentCommits(ctx2.cwd, 15);
      const changelog = await readChangelog(ctx2.cwd);
      const changedFiles = getChangedFiles(ctx2.cwd);
      if (!commits && !changelog) {
        return "No git history or CHANGELOG.md found. Initialize with: /changelog add <description>";
      }
      ctx2.ui.notify("\u{1F50D} \u5206\u6790\u53D8\u66F4\u5E76\u68C0\u67E5\u9057\u6F0F...", "info");
      return `\u{1F4DD} \u667A\u80FD Changelog \u66F4\u65B0

\u8BF7\u5206\u6790\u4EE5\u4E0B\u4FE1\u606F\uFF0C\u68C0\u67E5\u5E76\u8865\u5145\u9057\u6F0F\u7684 CHANGELOG \u6761\u76EE\uFF1A

## \u6700\u8FD1 Git \u63D0\u4EA4\u8BB0\u5F55
${commits || "(\u65E0\u6CD5\u83B7\u53D6 git \u5386\u53F2)"}

## \u6700\u8FD1\u53D8\u66F4\u7684\u6587\u4EF6
${changedFiles || "(\u65E0\u6CD5\u83B7\u53D6)"}

## \u5F53\u524D CHANGELOG.md
${changelog ? "\n```\n" + changelog.slice(0, 2e3) + "\n...\n```" : "(\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u9700\u8981\u521B\u5EFA)"}

## \u4EFB\u52A1

1. **\u5BF9\u6BD4\u5206\u6790**\uFF1A
   - \u67E5\u770B git \u63D0\u4EA4\u4E2D\u6709\u54EA\u4E9B\u91CD\u8981\u53D8\u66F4
   - \u5BF9\u6BD4 CHANGELOG \u7684 [Unreleased] \u90E8\u5206
   - \u8BC6\u522B\u9057\u6F0F\u7684\u6761\u76EE

2. **\u8865\u5145\u9057\u6F0F**\uFF08\u5982\u679C\u6709\uFF09\uFF1A
   - \u4E3A\u6BCF\u4E2A\u9057\u6F0F\u7684\u53D8\u66F4\u521B\u5EFA\u4E13\u4E1A\u6761\u76EE
   - \u5206\u7C7B\uFF1A**Added** / **Changed** / **Fixed** / **Removed**
   - \u5305\u542B\u6280\u672F\u7EC6\u8282\u548C\u6587\u4EF6\u8DEF\u5F84

3. **\u66F4\u65B0\u6587\u4EF6**\uFF1A
   - \u4F7F\u7528 write \u5DE5\u5177\u66F4\u65B0 CHANGELOG.md
   - \u5982\u679C\u4E0D\u5B58\u5728\uFF0C\u521B\u5EFA\u6807\u51C6\u683C\u5F0F
   - \u53EA\u6DFB\u52A0\u9057\u6F0F\u7684\u6761\u76EE\uFF0C\u4FDD\u6301\u73B0\u6709\u5185\u5BB9

4. **\u62A5\u544A\u7ED3\u679C**\uFF1A
   - \u6DFB\u52A0\u4E86\u54EA\u4E9B\u6761\u76EE
   - \u662F\u5426\u6709\u4E0D\u786E\u5B9A\u662F\u5426\u9700\u8981\u8BB0\u5F55\u7684\u5185\u5BB9`;
    }
  });
  ctx.log("Changelog extension ready");
}
export {
  changelogExtension as default
};
