'use strict';

var chunkO4O2EJF7_cjs = require('./chunk-O4O2EJF7.cjs');
var chunkKSN5CWZ7_cjs = require('./chunk-KSN5CWZ7.cjs');
var chunkHDIYJI2Z_cjs = require('./chunk-HDIYJI2Z.cjs');
var chunkTIWGWGIO_cjs = require('./chunk-TIWGWGIO.cjs');
var chunkDRDVJ35H_cjs = require('./chunk-DRDVJ35H.cjs');
var chunkZ7LCIYK7_cjs = require('./chunk-Z7LCIYK7.cjs');
var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');

// src/server/handlers/stored-agent-favorites.ts
async function getFavoritesContext(mastra) {
  const storage = mastra.getStorage();
  if (!storage) {
    throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
  }
  const agentStore = await storage.getStore("agents");
  if (!agentStore) {
    throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Agents storage domain is not available" });
  }
  const favoritesStore = await storage.getStore("favorites");
  if (!favoritesStore) {
    throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Favorites storage domain is not available" });
  }
  return { agentStore, favoritesStore };
}
var FAVORITE_STORED_AGENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "PUT",
  path: "/stored/agents/:storedAgentId/favorite",
  responseType: "json",
  pathParamSchema: chunkTIWGWGIO_cjs.storedAgentIdPathParams,
  responseSchema: chunkO4O2EJF7_cjs.favoriteToggleResponseSchema,
  summary: "Favorite a stored agent",
  description: "Marks the stored agent as favorited by the calling user. Idempotent.",
  tags: ["Stored Agents"],
  requiresAuth: true,
  requiresPermission: "stored-agents:read",
  handler: async ({ mastra, requestContext, storedAgentId }) => {
    try {
      await chunkKSN5CWZ7_cjs.requireBuilderFeature(mastra, "favorites");
      const callerId = chunkHDIYJI2Z_cjs.getCallerAuthorId(requestContext);
      if (!callerId) {
        throw new chunk64ITUOXI_cjs.HTTPException(401, { message: "Authentication required" });
      }
      const { agentStore, favoritesStore } = await getFavoritesContext(mastra);
      const agent = await agentStore.getById(storedAgentId);
      if (!agent) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored agent with id ${storedAgentId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(agent, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      chunkHDIYJI2Z_cjs.assertReadAccess({ requestContext, resource: "stored-agents", resourceId: storedAgentId, record: agent });
      const result = await favoritesStore.favorite({
        userId: callerId,
        entityType: "agent",
        entityId: storedAgentId
      });
      return result;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error favoriting stored agent");
    }
  }
});
var UNFAVORITE_STORED_AGENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/stored/agents/:storedAgentId/favorite",
  responseType: "json",
  pathParamSchema: chunkTIWGWGIO_cjs.storedAgentIdPathParams,
  responseSchema: chunkO4O2EJF7_cjs.favoriteToggleResponseSchema,
  summary: "Unfavorite a stored agent",
  description: "Removes the caller\u2019s favorite from the stored agent. Idempotent.",
  tags: ["Stored Agents"],
  requiresAuth: true,
  requiresPermission: "stored-agents:read",
  handler: async ({ mastra, requestContext, storedAgentId }) => {
    try {
      await chunkKSN5CWZ7_cjs.requireBuilderFeature(mastra, "favorites");
      const callerId = chunkHDIYJI2Z_cjs.getCallerAuthorId(requestContext);
      if (!callerId) {
        throw new chunk64ITUOXI_cjs.HTTPException(401, { message: "Authentication required" });
      }
      const { agentStore, favoritesStore } = await getFavoritesContext(mastra);
      const agent = await agentStore.getById(storedAgentId);
      if (!agent) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored agent with id ${storedAgentId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(agent, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      chunkHDIYJI2Z_cjs.assertReadAccess({ requestContext, resource: "stored-agents", resourceId: storedAgentId, record: agent });
      const result = await favoritesStore.unfavorite({
        userId: callerId,
        entityType: "agent",
        entityId: storedAgentId
      });
      return result;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error unfavoriting stored agent");
    }
  }
});

exports.FAVORITE_STORED_AGENT_ROUTE = FAVORITE_STORED_AGENT_ROUTE;
exports.UNFAVORITE_STORED_AGENT_ROUTE = UNFAVORITE_STORED_AGENT_ROUTE;
//# sourceMappingURL=chunk-D6FUPRXX.cjs.map
//# sourceMappingURL=chunk-D6FUPRXX.cjs.map