import { MASTRA_RESOURCE_ID_KEY, MASTRA_THREAD_ID_KEY } from './chunk-JVQ5EXNJ.js';
import { HTTPException } from './chunk-6QWQZI4Q.js';

// src/server/handlers/utils.ts
function validateBody(body) {
  const errorResponse = Object.entries(body).reduce((acc, [key, value]) => {
    if (!value) {
      acc[key] = `Argument "${key}" is required`;
    }
    return acc;
  }, {});
  if (Object.keys(errorResponse).length > 0) {
    throw new HTTPException(400, { message: Object.values(errorResponse)[0] });
  }
}
function sanitizeBody(body, disallowedKeys) {
  for (const key of disallowedKeys) {
    if (key in body) {
      delete body[key];
    }
  }
}
function parsePerPage(value, defaultValue = 100, max = 1e3) {
  const normalized = (value || "").trim().toLowerCase();
  if (normalized === "false") {
    return false;
  }
  const parsed = parseInt(value || String(defaultValue), 10);
  if (isNaN(parsed)) return defaultValue;
  return Math.min(max, Math.max(1, parsed));
}
function parseFilters(filters) {
  if (!filters) return void 0;
  return Object.fromEntries(
    (Array.isArray(filters) ? filters : [filters]).map((attr) => {
      const [key, ...valueParts] = attr.split(":");
      const value = valueParts.join(":");
      return [key, value];
    })
  );
}
function getEffectiveResourceId(requestContext, clientResourceId) {
  const contextResourceId = requestContext?.get(MASTRA_RESOURCE_ID_KEY);
  return contextResourceId || clientResourceId;
}
function getEffectiveThreadId(requestContext, clientThreadId) {
  const contextThreadId = requestContext?.get(MASTRA_THREAD_ID_KEY);
  return contextThreadId || clientThreadId;
}
async function validateThreadOwnership(thread, effectiveResourceId) {
  if (thread && effectiveResourceId && thread.resourceId && thread.resourceId !== effectiveResourceId) {
    throw new HTTPException(403, { message: "Access denied: thread belongs to a different resource" });
  }
}
async function validateRunOwnership(run, effectiveResourceId) {
  if (run && effectiveResourceId && run.resourceId && run.resourceId !== effectiveResourceId) {
    throw new HTTPException(403, { message: "Access denied: workflow run belongs to a different resource" });
  }
}

export { getEffectiveResourceId, getEffectiveThreadId, parseFilters, parsePerPage, sanitizeBody, validateBody, validateRunOwnership, validateThreadOwnership };
//# sourceMappingURL=chunk-VLNRGJEM.js.map
//# sourceMappingURL=chunk-VLNRGJEM.js.map