// src/actions/packman/rewriteScripts.ts
function xyCommand(pm) {
  switch (pm) {
    case "bun": {
      return "bun run xy";
    }
    case "npm": {
      return "npm exec -- xy";
    }
    case "pnpm": {
      return "pnpm xy";
    }
    default: {
      return "yarn xy";
    }
  }
}
function rootRunCommand(pm, script, rest = "") {
  const suffix = rest.trim() ? ` ${rest.trim()}` : "";
  return `${pm} run ${script}${suffix}`;
}
function addCommand(pm) {
  switch (pm) {
    case "bun": {
      return "bun add";
    }
    case "npm": {
      return "npm install";
    }
    case "pnpm": {
      return "pnpm add";
    }
    default: {
      return "yarn add";
    }
  }
}
function removeCommand(pm) {
  switch (pm) {
    case "bun": {
      return "bun remove";
    }
    case "npm": {
      return "npm uninstall";
    }
    case "pnpm": {
      return "pnpm remove";
    }
    default: {
      return "yarn remove";
    }
  }
}
function installCommand(pm) {
  return `${pm} install`;
}
function outdatedCommand(pm) {
  return `${pm} outdated`;
}
function dedupeCommand(pm) {
  return pm === "npm" ? "npm dedupe" : `${pm} dedupe`;
}
function trimRest(rest) {
  if (!rest) return "";
  const normalized = rest.trim();
  return normalized ? ` ${normalized}` : "";
}
function workspaceRunCommand(pm, pkg, script, rest = "") {
  const suffix = trimRest(rest);
  switch (pm) {
    case "bun": {
      return `bun run --filter ${pkg} ${script}${suffix}`;
    }
    case "npm": {
      return `npm run ${script} --workspace=${pkg}${suffix}`;
    }
    case "pnpm": {
      return `pnpm --filter ${pkg} run ${script}${suffix}`;
    }
    default: {
      return `yarn workspace ${pkg} run ${script}${suffix}`;
    }
  }
}
function workspaceRunAllCommand(pm, script, rest = "") {
  const suffix = trimRest(rest);
  switch (pm) {
    case "bun": {
      return `bun run --workspaces --if-present ${script}${suffix}`;
    }
    case "npm": {
      return `npm run ${script} --workspaces --if-present${suffix}`;
    }
    case "pnpm": {
      return `pnpm -r run ${script}${suffix}`;
    }
    default: {
      return `yarn workspaces foreach -Apt run ${script}${suffix}`;
    }
  }
}
function normalizeWorkspaceCommands(script, target) {
  let result = script;
  result = result.replaceAll(/\byarn workspace (\S+) run (\S+)(?:\s+(.*))?$/g, (_, pkg, command, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\byarn workspace (\S+) (package-\S+)(?:\s+(.*))?$/g, (_, pkg, command, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\bpnpm --filter (\S+) run (\S+)(?:\s+(.*))?$/g, (_, pkg, command, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\bpnpm --filter (\S+) (package-\S+)(?:\s+(.*))?$/g, (_, pkg, command, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\bnpm run (\S+) --workspace(?:=|\s+)(\S+)(?:\s+(.*))?$/g, (_, command, pkg, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\bnpm --workspace(?:=|\s+)(\S+) run (\S+)(?:\s+(.*))?$/g, (_, pkg, command, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\bnpm run (\S+) -w (\S+)(?:\s+(.*))?$/g, (_, command, pkg, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\bbun run --filter (\S+) (\S+)(?:\s+(.*))?$/g, (_, pkg, command, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\bbun --filter (\S+) run (\S+)(?:\s+(.*))?$/g, (_, pkg, command, rest) => workspaceRunCommand(target, pkg, command, rest));
  result = result.replaceAll(/\byarn workspaces foreach\s+(?:[^\s]+\s+)*run (\S+)(?:\s+(.*))?$/g, (_, command, rest) => workspaceRunAllCommand(target, command, rest));
  result = result.replaceAll(/\bpnpm -r(?:\s+[^\s]+)* run (\S+)(?:\s+(.*))?$/g, (_, command, rest) => workspaceRunAllCommand(target, command, rest));
  result = result.replaceAll(/\bnpm run (\S+) --workspaces(?: --if-present)?(?:\s+(.*))?$/g, (_, command, rest) => workspaceRunAllCommand(target, command, rest));
  result = result.replaceAll(/\bbun run(?:\s+--[^\s]+)* --workspaces(?: --if-present)? (\S+)(?:\s+(.*))?$/g, (_, command, rest) => workspaceRunAllCommand(target, command, rest));
  return result;
}
function normalizeRootRunCommands(script, target) {
  let result = script;
  result = result.replaceAll(/\byarn run (\S+)(?:\s+(.*))?$/g, (_, command, rest) => rootRunCommand(target, command, rest));
  result = result.replaceAll(/\bpnpm run (\S+)(?:\s+(.*))?$/g, (_, command, rest) => rootRunCommand(target, command, rest));
  result = result.replaceAll(/\bnpm run (\S+)(?!\s+(?:--workspace(?:=|\s)|-w\s|--workspaces)\b)(?:\s+(.*))?$/g, (_, command, rest) => rootRunCommand(target, command, rest));
  result = result.replaceAll(/\bbun run (\S+)(?!\s+(?:--filter|--workspaces)\b)(?:\s+(.*))?$/g, (_, command, rest) => rootRunCommand(target, command, rest));
  return result;
}
function normalizeXyCommands(script, target) {
  return script.replaceAll(/\byarn xy\b/g, xyCommand(target)).replaceAll(/\bpnpm xy\b/g, xyCommand(target)).replaceAll(/\bnpm exec(?: --)? xy\b/g, xyCommand(target)).replaceAll(/\bnpx(?: --no-install)? xy\b/g, xyCommand(target)).replaceAll(/\bbun run xy\b/g, xyCommand(target)).replaceAll(/\bbun x xy\b/g, xyCommand(target));
}
function rewriteScript(script, target) {
  let result = normalizeWorkspaceCommands(script, target);
  result = normalizeRootRunCommands(result, target);
  result = normalizeXyCommands(result, target);
  result = result.replaceAll(/\byarn add\b/g, addCommand(target));
  result = result.replaceAll(/\bpnpm add\b/g, addCommand(target));
  result = result.replaceAll(/\bbun add\b/g, addCommand(target));
  result = result.replaceAll(/\byarn remove\b/g, removeCommand(target));
  result = result.replaceAll(/\bpnpm remove\b/g, removeCommand(target));
  result = result.replaceAll(/\bbun remove\b/g, removeCommand(target));
  result = result.replaceAll(/\bnpm uninstall\b/g, removeCommand(target));
  result = result.replaceAll(/\byarn install\b/g, installCommand(target));
  result = result.replaceAll(/\bpnpm install\b/g, installCommand(target));
  result = result.replaceAll(/\bbun install\b/g, installCommand(target));
  result = result.replaceAll(/\bnpm install\b/g, installCommand(target));
  result = result.replaceAll(/\byarn dedupe\b/g, dedupeCommand(target));
  result = result.replaceAll(/\bpnpm dedupe\b/g, dedupeCommand(target));
  result = result.replaceAll(/\bnpm dedupe\b/g, dedupeCommand(target));
  result = result.replaceAll(/\byarn outdated\b/g, outdatedCommand(target));
  result = result.replaceAll(/\bpnpm outdated\b/g, outdatedCommand(target));
  result = result.replaceAll(/\bbun outdated\b/g, outdatedCommand(target));
  result = result.replaceAll(/\bnpm outdated\b/g, outdatedCommand(target));
  return result;
}
function rewriteScriptsInPackageJson(pkg, target) {
  const scripts = pkg.scripts;
  if (!scripts) return pkg;
  const rewritten = {};
  for (const [name, script] of Object.entries(scripts)) {
    rewritten[name] = rewriteScript(script, target);
  }
  return { ...pkg, scripts: rewritten };
}
export {
  rewriteScript,
  rewriteScriptsInPackageJson
};
//# sourceMappingURL=rewriteScripts.mjs.map