// Default semantic-release config used by just-ci/templates.
//
// Conventional-commits commit-analyzer + release notes, changelog, git
// commit + tag (CHANGELOG.md asset), GitLab release.
//
// The block at the bottom (after `module.exports`-style setup) auto-loads
// `.releaserc.files.{yaml,yml,json}` if present and splices
// `semantic-release-replace-plugin` into the plugin chain, with the listed
// files appended to `@semantic-release/git`'s `assets`. This lets you bump
// version-bearing files (READMEs, CI manifests, source constants) at
// release time without writing a full custom releaserc.
//
// =====================================================================
// IMPORTANT — committing this file to your repo:
//   If you wget this file as your permanent `.releaserc.cjs` AND you want
//   to manage `semantic-release-replace-plugin` yourself in this same
//   config (instead of via the sidecar `.releaserc.files.{yaml,yml,json}`),
//   delete the auto-merge block at the bottom of this file (marked with
//   `BEGIN auto-merge` / `END auto-merge`). Otherwise leave it: it is a
//   no-op when no `.releaserc.files.*` sidecar exists.
// =====================================================================
//
// Local debug:
//   wget https://gitlab.com/just-ci/templates/-/raw/v9.0.1/project-automation/release/releaserc-default.cjs -O .releaserc.cjs
//   npm i semantic-release @semantic-release/{gitlab,git,changelog} \
//         semantic-release-replace-plugin \
//         conventional-changelog-conventionalcommits js-yaml
//   npx semantic-release --dry-run --no-ci

const fs = require("node:fs");
const yaml = require("js-yaml");

const config = {
  branches: [
    "+([0-9])?(.{+([0-9]),x}).x",
    "master",
    "main",
    "next",
    "next-major",
    { name: "alpha", channel: "alpha", prerelease: "alpha" },
    { name: "beta", channel: "beta", prerelease: "beta" },
    { name: "rc", channel: "rc", prerelease: "rc" },
  ],
  plugins: [
    ["@semantic-release/commit-analyzer", { preset: "conventionalcommits" }],
    [
      "@semantic-release/release-notes-generator",
      { preset: "conventionalcommits" },
    ],
    "@semantic-release/changelog",
    [
      "@semantic-release/git",
      {
        assets: ["CHANGELOG.md"],
        message:
          "chore(release): ${nextRelease.version}\n\n${nextRelease.notes}",
        successComment: false,
      },
    ],
    "@semantic-release/gitlab",
  ],
};

// =====================================================================
// BEGIN auto-merge: `.releaserc.files.{yaml,yml,json}` -> replace plugin
// Remove this block if you commit this file and want to manage
// `semantic-release-replace-plugin` directly in `config.plugins` above.
// =====================================================================
const candidate = [
  ".releaserc.files.yaml",
  ".releaserc.files.yml",
  ".releaserc.files.json",
].find((f) => fs.existsSync(f));

if (candidate) {
  const raw = fs.readFileSync(candidate, "utf8");
  const parsed = candidate.endsWith(".json") ? JSON.parse(raw) : yaml.load(raw);
  const replacements = (parsed && parsed.replacements) || [];

  if (replacements.length > 0) {
    const extraAssets = [
      ...new Set(replacements.flatMap((r) => r.files || [])),
    ];
    const gitIdx = config.plugins.findIndex(
      (p) => Array.isArray(p) && p[0] === "@semantic-release/git",
    );
    config.plugins.splice(gitIdx, 0, [
      "semantic-release-replace-plugin",
      { replacements },
    ]);
    const gitPluginConfig = config.plugins[gitIdx + 1][1];
    gitPluginConfig.assets = [...gitPluginConfig.assets, ...extraAssets];
  }
}
// =====================================================================
// END auto-merge
// =====================================================================

module.exports = config;
