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AA,CAAO,EAAE,CAAC,CAAA;AACnG,EAAA,IAAI,UAAA,EAAY;AACd,IAAA,OAAO,EAAE,GAAG,MAAA,EAAQ,WAAA,EAAa,WAAW,UAAA,CAAW,GAAA,CAAI,MAAA,CAAO,EAAE,CAAA,EAAE;AAAA,EACxE;AACA,EAAA,OAAO,oBAAoB,MAAM,CAAA;AACnC","file":"chunk-M2ZGNEQS.cjs","sourcesContent":["import type { Mastra } from '@mastra/core';\nimport type { RequestContext } from '@mastra/core/di';\nimport type { FavoritesStorage, StorageFavoriteEntityType } from '@mastra/core/storage';\n\nimport { getCallerAuthorId } from './authorship';\nimport { isBuilderFeatureEnabled } from './editor-builder';\n\n/**\n * Result of `prepareFavoritesEnrichment` — `null` when the `favorites` EE feature is off.\n * When non-null the caller may use `starredIds` to set `isFavorited` on records\n * and may pass `userId` along to storage list paths for pin-favorited-first\n * sorting (`pinFavoritedFor`).\n */\nexport type FavoritesEnrichmentContext = {\n userId: string;\n starredIds: Set;\n favoritesStore: FavoritesStorage;\n} | null;\n\n/**\n * Resolve the EE feature flag plus the caller's favorited set for a list of\n * candidate entity IDs in one shot. Soft-gated: returns `null` if the feature\n * is off or there's no caller — handlers should drop `isFavorited` / `favoriteCount`\n * fields and ignore `?favoritedOnly=true` in that case.\n */\nexport async function prepareFavoritesEnrichment(\n mastra: Mastra,\n requestContext: RequestContext,\n entityType: StorageFavoriteEntityType,\n entityIds: string[],\n): Promise {\n if (!(await isBuilderFeatureEnabled(mastra, 'favorites'))) return null;\n\n const userId = getCallerAuthorId(requestContext);\n if (!userId) return null;\n\n const storage = mastra.getStorage();\n if (!storage) return null;\n const favoritesStore = await storage.getStore('favorites');\n if (!favoritesStore) return null;\n\n const starredIds =\n entityIds.length === 0\n ? new Set()\n : await favoritesStore.isFavoritedBatch({ userId, entityType, entityIds });\n return { userId, starredIds, favoritesStore };\n}\n\n/**\n * Strip the favorites EE fields from a record. Used when the feature is off so\n * stale values from storage do not leak through the API.\n */\nexport function stripFavoriteFields(record: T): T {\n if ('isFavorited' in record || 'favoriteCount' in record) {\n const copy = { ...record } as Record;\n delete copy.isFavorited;\n delete copy.favoriteCount;\n return copy as T;\n }\n return record;\n}\n\n/**\n * Convenience for single-entity handlers (GET / POST / PATCH): annotate\n * `isFavorited` from the caller's favorites set when enrichment is available,\n * otherwise strip both favorite fields. `favoriteCount` is the canonical\n * mirrored counter and is left in place so callers can render share-count UI\n * (it's identical across viewers).\n */\nexport async function enrichOrStripFavorites(\n mastra: Mastra,\n requestContext: RequestContext,\n entityType: StorageFavoriteEntityType,\n record: T,\n): Promise {\n const enrichment = await prepareFavoritesEnrichment(mastra, requestContext, entityType, [record.id]);\n if (enrichment) {\n return { ...record, isFavorited: enrichment.starredIds.has(record.id) };\n }\n return stripFavoriteFields(record);\n}\n"]}