// Force strict mode and setup for ESM
"use strict";
import {
  INBOXES_DIR,
  LEADER_NAME,
  TASKS_DIR,
  TEAMMATE_COLORS,
  TEAMS_DIR,
  TEAM_CONFIG_FILENAME
} from "./chunk-CPBF7KYF.js";
import {
  atomicWriteJSON
} from "./chunk-7XATOOE7.js";
import {
  Storage,
  isNodeError
} from "./chunk-UHUSJ6X7.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/agents/team/teamHelpers.ts
init_esbuild_shims();
import * as fs from "node:fs/promises";
import * as path from "node:path";
function getTeamsRootDir() {
  return path.join(Storage.getGlobalQwenDir(), TEAMS_DIR);
}
__name(getTeamsRootDir, "getTeamsRootDir");
function getTeamDir(teamName) {
  return path.join(getTeamsRootDir(), teamName);
}
__name(getTeamDir, "getTeamDir");
function getTeamFilePath(teamName) {
  return path.join(getTeamDir(teamName), TEAM_CONFIG_FILENAME);
}
__name(getTeamFilePath, "getTeamFilePath");
function getInboxesDir(teamName) {
  return path.join(getTeamDir(teamName), INBOXES_DIR);
}
__name(getInboxesDir, "getInboxesDir");
function getTasksDir(teamName) {
  return path.join(Storage.getGlobalQwenDir(), TASKS_DIR, teamName);
}
__name(getTasksDir, "getTasksDir");
function sanitizeName(name) {
  return name.toLowerCase().replace(/[^a-z0-9-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "");
}
__name(sanitizeName, "sanitizeName");
function formatAgentId(name, teamName) {
  return `${sanitizeName(name)}@${sanitizeName(teamName)}`;
}
__name(formatAgentId, "formatAgentId");
function generateUniqueTeammateName(baseName, existingMembers) {
  const sanitized = sanitizeName(baseName);
  if (!sanitized) {
    throw new Error(
      `Teammate name "${baseName}" sanitizes to an empty string. Choose a name with at least one alphanumeric character.`
    );
  }
  if (sanitized === LEADER_NAME) {
    throw new Error(
      `"${LEADER_NAME}" is reserved for the team leader. Choose a different teammate name.`
    );
  }
  const existingNames = new Set(existingMembers.map((m) => m.name));
  if (existingNames.has(sanitized)) {
    const existingList = [...existingNames].join(", ") || "<none>";
    throw new Error(
      `A teammate named "${sanitized}" already exists in this team (existing: ${existingList}). Choose a different name.`
    );
  }
  return sanitized;
}
__name(generateUniqueTeammateName, "generateUniqueTeammateName");
function assignTeammateColor(existingMembers) {
  const usedColors = new Set(
    existingMembers.map((m) => m.color).filter((c) => c !== void 0)
  );
  for (const color of TEAMMATE_COLORS) {
    if (!usedColors.has(color)) {
      return color;
    }
  }
  return TEAMMATE_COLORS[existingMembers.length % TEAMMATE_COLORS.length];
}
__name(assignTeammateColor, "assignTeammateColor");
function findMemberByName(members, name) {
  const sanitized = sanitizeName(name);
  return members.find((m) => m.name === sanitized);
}
__name(findMemberByName, "findMemberByName");
function classifyShutdownResponse(message) {
  const trimmed = message.trimStart();
  if (/^shutdown_approved\b/i.test(trimmed)) return "shutdown_approved";
  if (/^shutdown_rejected\b/i.test(trimmed)) return "shutdown_rejected";
  return void 0;
}
__name(classifyShutdownResponse, "classifyShutdownResponse");
async function readTeamFile(teamName) {
  const filePath = getTeamFilePath(teamName);
  try {
    const raw = await fs.readFile(filePath, "utf-8");
    return JSON.parse(raw);
  } catch (err) {
    if (isNodeError(err) && err.code === "ENOENT") {
      return void 0;
    }
    throw err;
  }
}
__name(readTeamFile, "readTeamFile");
async function writeTeamFile(teamName, teamFile) {
  const filePath = getTeamFilePath(teamName);
  await fs.mkdir(path.dirname(filePath), { recursive: true });
  await atomicWriteJSON(filePath, teamFile);
}
__name(writeTeamFile, "writeTeamFile");
async function createTeamFile(teamName, teamFile) {
  const filePath = getTeamFilePath(teamName);
  await fs.mkdir(path.dirname(filePath), { recursive: true });
  await fs.writeFile(filePath, JSON.stringify(teamFile, null, 2) + "\n", {
    encoding: "utf-8",
    flag: "wx"
  });
}
__name(createTeamFile, "createTeamFile");
function isPidAlive(pid) {
  try {
    process.kill(pid, 0);
    return true;
  } catch (err) {
    return isNodeError(err) && err.code === "EPERM";
  }
}
__name(isPidAlive, "isPidAlive");
async function tryReclaimStaleTeam(teamName) {
  let existing;
  try {
    existing = await readTeamFile(teamName);
  } catch {
    return false;
  }
  if (!existing) {
    await deleteTeamDirs(teamName);
    return true;
  }
  if (typeof existing.leadPid !== "number" || existing.leadPid <= 0) {
    return false;
  }
  if (existing.leadPid !== process.pid && isPidAlive(existing.leadPid)) {
    return false;
  }
  await deleteTeamDirs(teamName);
  return true;
}
__name(tryReclaimStaleTeam, "tryReclaimStaleTeam");
async function deleteTeamDirs(teamName) {
  const teamDir = getTeamDir(teamName);
  const tasksDir = getTasksDir(teamName);
  await Promise.allSettled([
    fs.rm(teamDir, { recursive: true, force: true }),
    fs.rm(tasksDir, { recursive: true, force: true })
  ]);
}
__name(deleteTeamDirs, "deleteTeamDirs");

export {
  getTeamDir,
  getInboxesDir,
  getTasksDir,
  sanitizeName,
  formatAgentId,
  generateUniqueTeammateName,
  assignTeammateColor,
  findMemberByName,
  classifyShutdownResponse,
  writeTeamFile,
  createTeamFile,
  tryReclaimStaleTeam,
  deleteTeamDirs
};
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
