'use strict';

var chunkT3C54LPX_cjs = require('./chunk-T3C54LPX.cjs');
var chunkO4O2EJF7_cjs = require('./chunk-O4O2EJF7.cjs');
var chunkKSN5CWZ7_cjs = require('./chunk-KSN5CWZ7.cjs');
var chunkHDIYJI2Z_cjs = require('./chunk-HDIYJI2Z.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-skill-favorites.ts
async function getFavoritesContext(mastra) {
  const storage = mastra.getStorage();
  if (!storage) {
    throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
  }
  const skillStore = await storage.getStore("skills");
  if (!skillStore) {
    throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Skills 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 { skillStore, favoritesStore };
}
var FAVORITE_STORED_SKILL_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "PUT",
  path: "/stored/skills/:storedSkillId/favorite",
  responseType: "json",
  pathParamSchema: chunkT3C54LPX_cjs.storedSkillIdPathParams,
  responseSchema: chunkO4O2EJF7_cjs.favoriteToggleResponseSchema,
  summary: "Favorite a stored skill",
  description: "Marks the stored skill as favorited by the calling user. Idempotent.",
  tags: ["Stored Skills"],
  requiresAuth: true,
  requiresPermission: "stored-skills:read",
  handler: async ({ mastra, requestContext, storedSkillId }) => {
    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 { skillStore, favoritesStore } = await getFavoritesContext(mastra);
      const skill = await skillStore.getByIdResolved(storedSkillId);
      if (!skill) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(skill, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      chunkHDIYJI2Z_cjs.assertReadAccess({ requestContext, resource: "stored-skills", resourceId: storedSkillId, record: skill });
      const result = await favoritesStore.favorite({
        userId: callerId,
        entityType: "skill",
        entityId: storedSkillId
      });
      return result;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error favoriting stored skill");
    }
  }
});
var UNFAVORITE_STORED_SKILL_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/stored/skills/:storedSkillId/favorite",
  responseType: "json",
  pathParamSchema: chunkT3C54LPX_cjs.storedSkillIdPathParams,
  responseSchema: chunkO4O2EJF7_cjs.favoriteToggleResponseSchema,
  summary: "Unfavorite a stored skill",
  description: "Removes the caller\u2019s favorite from the stored skill. Idempotent.",
  tags: ["Stored Skills"],
  requiresAuth: true,
  requiresPermission: "stored-skills:read",
  handler: async ({ mastra, requestContext, storedSkillId }) => {
    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 { skillStore, favoritesStore } = await getFavoritesContext(mastra);
      const skill = await skillStore.getByIdResolved(storedSkillId);
      if (!skill) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(skill, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      chunkHDIYJI2Z_cjs.assertReadAccess({ requestContext, resource: "stored-skills", resourceId: storedSkillId, record: skill });
      const result = await favoritesStore.unfavorite({
        userId: callerId,
        entityType: "skill",
        entityId: storedSkillId
      });
      return result;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error unfavoriting stored skill");
    }
  }
});

exports.FAVORITE_STORED_SKILL_ROUTE = FAVORITE_STORED_SKILL_ROUTE;
exports.UNFAVORITE_STORED_SKILL_ROUTE = UNFAVORITE_STORED_SKILL_ROUTE;
//# sourceMappingURL=chunk-3YFDOQMM.cjs.map
//# sourceMappingURL=chunk-3YFDOQMM.cjs.map