// src/server/handlers/author-enrichment.ts
function getAuthProvider(mastra) {
  const serverConfig = mastra.getServer?.();
  if (!serverConfig?.auth) return null;
  if (typeof serverConfig.auth.authenticateToken === "function") {
    return serverConfig.auth;
  }
  return null;
}
function isUserProvider(p) {
  return p !== null && typeof p === "object" && typeof p.getCurrentUser === "function" && typeof p.getUser === "function";
}
function dedupeIds(authorIds) {
  const set = /* @__PURE__ */ new Set();
  for (const id of authorIds) {
    if (typeof id === "string" && id.length > 0) set.add(id);
  }
  return Array.from(set);
}
function projectUser(user) {
  if (!user) return null;
  const out = {};
  if (typeof user.name === "string") out.name = user.name;
  if (typeof user.email === "string") out.email = user.email;
  if (typeof user.avatarUrl === "string") out.avatarUrl = user.avatarUrl;
  return out;
}
async function prepareAuthorEnrichment(mastra, _requestContext, authorIds) {
  const provider = getAuthProvider(mastra);
  if (!provider || !isUserProvider(provider)) return null;
  const uniqueIds = dedupeIds(authorIds);
  if (uniqueIds.length === 0) return /* @__PURE__ */ new Map();
  let users;
  if (typeof provider.getUsers === "function") {
    try {
      users = await provider.getUsers(uniqueIds);
    } catch {
      return /* @__PURE__ */ new Map();
    }
  } else {
    users = await Promise.all(uniqueIds.map((id) => provider.getUser(id).catch(() => null)));
  }
  const map = /* @__PURE__ */ new Map();
  uniqueIds.forEach((id, index) => {
    const projected = projectUser(users[index] ?? null);
    if (projected !== null) {
      map.set(id, { id, ...projected });
    }
  });
  return map;
}
function attachAuthor(record, authors) {
  if (!authors) return record;
  const id = record.authorId;
  if (typeof id !== "string" || id.length === 0) return record;
  const author = authors.get(id);
  if (!author) return record;
  return { ...record, author };
}

export { attachAuthor, prepareAuthorEnrichment };
//# sourceMappingURL=chunk-4W5AVWZH.js.map
//# sourceMappingURL=chunk-4W5AVWZH.js.map