'use strict';

var chunkTXM2XTUT_cjs = require('./chunk-TXM2XTUT.cjs');
var chunk2XZ2466F_cjs = require('./chunk-2XZ2466F.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/prompt-block-versions.ts
var SNAPSHOT_CONFIG_FIELDS = ["name", "description", "content", "rules"];
var LIST_PROMPT_BLOCK_VERSIONS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/prompt-blocks/:promptBlockId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkTXM2XTUT_cjs.promptBlockVersionPathParams,
  queryParamSchema: chunkTXM2XTUT_cjs.listPromptBlockVersionsQuerySchema,
  responseSchema: chunkTXM2XTUT_cjs.listPromptBlockVersionsResponseSchema,
  summary: "List prompt block versions",
  description: "Returns a paginated list of all versions for a stored prompt block",
  tags: ["Prompt Block Versions"],
  handler: async ({ mastra, promptBlockId, page, perPage, orderBy, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const promptBlockStore = await storage.getStore("promptBlocks");
      if (!promptBlockStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Prompt blocks storage domain is not available" });
      }
      const promptBlock = await promptBlockStore.getById(promptBlockId);
      if (!promptBlock) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Prompt block with id ${promptBlockId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(promptBlock, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const result = await promptBlockStore.listVersions({
        blockId: promptBlockId,
        page,
        perPage,
        orderBy
      });
      return result;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error listing prompt block versions");
    }
  }
});
var CREATE_PROMPT_BLOCK_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/prompt-blocks/:promptBlockId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkTXM2XTUT_cjs.promptBlockVersionPathParams,
  bodySchema: chunkTXM2XTUT_cjs.createPromptBlockVersionBodySchema,
  responseSchema: chunkTXM2XTUT_cjs.createPromptBlockVersionResponseSchema,
  summary: "Create prompt block version",
  description: "Creates a new version snapshot of the current prompt block configuration",
  tags: ["Prompt Block Versions"],
  handler: async ({ mastra, promptBlockId, changeMessage, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const promptBlockStore = await storage.getStore("promptBlocks");
      if (!promptBlockStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Prompt blocks storage domain is not available" });
      }
      const promptBlock = await promptBlockStore.getById(promptBlockId);
      if (!promptBlock) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Prompt block with id ${promptBlockId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(promptBlock, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      let currentConfig = {};
      if (promptBlock.activeVersionId) {
        const activeVersion = await promptBlockStore.getVersion(promptBlock.activeVersionId);
        if (activeVersion) {
          currentConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
            activeVersion,
            SNAPSHOT_CONFIG_FIELDS
          );
        }
      }
      const latestVersion = await promptBlockStore.getLatestVersion(promptBlockId);
      if (!promptBlock.activeVersionId && latestVersion) {
        currentConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
          latestVersion,
          SNAPSHOT_CONFIG_FIELDS
        );
      }
      const previousConfig = latestVersion ? chunk2XZ2466F_cjs.extractConfigFromVersion(latestVersion, SNAPSHOT_CONFIG_FIELDS) : null;
      const changedFields = chunk2XZ2466F_cjs.calculateChangedFields(previousConfig, currentConfig);
      const { versionId } = await chunk2XZ2466F_cjs.createVersionWithRetry(
        promptBlockStore,
        promptBlockId,
        "blockId",
        currentConfig,
        changedFields,
        { changeMessage }
      );
      const version = await promptBlockStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await chunk2XZ2466F_cjs.enforceRetentionLimit(
        promptBlockStore,
        promptBlockId,
        "blockId",
        promptBlock.activeVersionId
      );
      return version;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error creating prompt block version");
    }
  }
});
var GET_PROMPT_BLOCK_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/prompt-blocks/:promptBlockId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkTXM2XTUT_cjs.promptBlockVersionIdPathParams,
  responseSchema: chunkTXM2XTUT_cjs.getPromptBlockVersionResponseSchema,
  summary: "Get prompt block version",
  description: "Returns a specific version of a prompt block by its version ID",
  tags: ["Prompt Block Versions"],
  handler: async ({ mastra, promptBlockId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const promptBlockStore = await storage.getStore("promptBlocks");
      if (!promptBlockStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Prompt blocks storage domain is not available" });
      }
      const version = await promptBlockStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.blockId !== promptBlockId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for prompt block ${promptBlockId}`
        });
      }
      const promptBlock = await promptBlockStore.getById(promptBlockId);
      chunkDRDVJ35H_cjs.assertStoredResourceScope(promptBlock, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      return version;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error getting prompt block version");
    }
  }
});
var ACTIVATE_PROMPT_BLOCK_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/prompt-blocks/:promptBlockId/versions/:versionId/activate",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkTXM2XTUT_cjs.promptBlockVersionIdPathParams,
  responseSchema: chunkTXM2XTUT_cjs.activatePromptBlockVersionResponseSchema,
  summary: "Activate prompt block version",
  description: "Sets a specific version as the active version for the prompt block",
  tags: ["Prompt Block Versions"],
  handler: async ({ mastra, promptBlockId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const promptBlockStore = await storage.getStore("promptBlocks");
      if (!promptBlockStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Prompt blocks storage domain is not available" });
      }
      const promptBlock = await promptBlockStore.getById(promptBlockId);
      if (!promptBlock) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Prompt block with id ${promptBlockId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(promptBlock, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const version = await promptBlockStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.blockId !== promptBlockId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for prompt block ${promptBlockId}`
        });
      }
      await promptBlockStore.update({
        id: promptBlockId,
        activeVersionId: versionId,
        status: "published"
      });
      mastra.getEditor()?.prompt.clearCache(promptBlockId);
      return {
        success: true,
        message: `Version ${version.versionNumber} is now active`,
        activeVersionId: versionId
      };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error activating prompt block version");
    }
  }
});
var RESTORE_PROMPT_BLOCK_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/prompt-blocks/:promptBlockId/versions/:versionId/restore",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkTXM2XTUT_cjs.promptBlockVersionIdPathParams,
  responseSchema: chunkTXM2XTUT_cjs.restorePromptBlockVersionResponseSchema,
  summary: "Restore prompt block version",
  description: "Restores the prompt block configuration from a version, creating a new version",
  tags: ["Prompt Block Versions"],
  handler: async ({ mastra, promptBlockId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const promptBlockStore = await storage.getStore("promptBlocks");
      if (!promptBlockStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Prompt blocks storage domain is not available" });
      }
      const promptBlock = await promptBlockStore.getById(promptBlockId);
      if (!promptBlock) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Prompt block with id ${promptBlockId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(promptBlock, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const versionToRestore = await promptBlockStore.getVersion(versionId);
      if (!versionToRestore) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (versionToRestore.blockId !== promptBlockId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for prompt block ${promptBlockId}`
        });
      }
      const restoredConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        versionToRestore,
        SNAPSHOT_CONFIG_FIELDS
      );
      await promptBlockStore.update({
        id: promptBlockId,
        ...restoredConfig
      });
      const latestVersion = await promptBlockStore.getLatestVersion(promptBlockId);
      const previousConfig = latestVersion ? chunk2XZ2466F_cjs.extractConfigFromVersion(latestVersion, SNAPSHOT_CONFIG_FIELDS) : null;
      const changedFields = chunk2XZ2466F_cjs.calculateChangedFields(previousConfig, restoredConfig);
      const { versionId: newVersionId } = await chunk2XZ2466F_cjs.createVersionWithRetry(
        promptBlockStore,
        promptBlockId,
        "blockId",
        restoredConfig,
        changedFields,
        {
          changeMessage: `Restored from version ${versionToRestore.versionNumber}`
        }
      );
      const newVersion = await promptBlockStore.getVersion(newVersionId);
      if (!newVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await chunk2XZ2466F_cjs.enforceRetentionLimit(
        promptBlockStore,
        promptBlockId,
        "blockId",
        promptBlock.activeVersionId
      );
      mastra.getEditor()?.prompt.clearCache(promptBlockId);
      return newVersion;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error restoring prompt block version");
    }
  }
});
var DELETE_PROMPT_BLOCK_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/stored/prompt-blocks/:promptBlockId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkTXM2XTUT_cjs.promptBlockVersionIdPathParams,
  responseSchema: chunkTXM2XTUT_cjs.deletePromptBlockVersionResponseSchema,
  summary: "Delete prompt block version",
  description: "Deletes a specific version (cannot delete the active version)",
  tags: ["Prompt Block Versions"],
  handler: async ({ mastra, promptBlockId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const promptBlockStore = await storage.getStore("promptBlocks");
      if (!promptBlockStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Prompt blocks storage domain is not available" });
      }
      const promptBlock = await promptBlockStore.getById(promptBlockId);
      if (!promptBlock) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Prompt block with id ${promptBlockId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(promptBlock, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const version = await promptBlockStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.blockId !== promptBlockId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for prompt block ${promptBlockId}`
        });
      }
      if (promptBlock.activeVersionId === versionId) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Cannot delete the active version. Activate a different version first."
        });
      }
      await promptBlockStore.deleteVersion(versionId);
      mastra.getEditor()?.prompt.clearCache(promptBlockId);
      return {
        success: true,
        message: `Version ${version.versionNumber} deleted successfully`
      };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error deleting prompt block version");
    }
  }
});
var COMPARE_PROMPT_BLOCK_VERSIONS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/prompt-blocks/:promptBlockId/versions/compare",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkTXM2XTUT_cjs.promptBlockVersionPathParams,
  queryParamSchema: chunkTXM2XTUT_cjs.comparePromptBlockVersionsQuerySchema,
  responseSchema: chunkTXM2XTUT_cjs.comparePromptBlockVersionsResponseSchema,
  summary: "Compare prompt block versions",
  description: "Compares two versions and returns the differences between them",
  tags: ["Prompt Block Versions"],
  handler: async ({ mastra, promptBlockId, from, to, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const promptBlockStore = await storage.getStore("promptBlocks");
      if (!promptBlockStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Prompt blocks storage domain is not available" });
      }
      const promptBlock = await promptBlockStore.getById(promptBlockId);
      chunkDRDVJ35H_cjs.assertStoredResourceScope(promptBlock, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const fromVersion = await promptBlockStore.getVersion(from);
      if (!fromVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${from} not found` });
      }
      if (fromVersion.blockId !== promptBlockId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${from} not found for prompt block ${promptBlockId}`
        });
      }
      const toVersion = await promptBlockStore.getVersion(to);
      if (!toVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${to} not found` });
      }
      if (toVersion.blockId !== promptBlockId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${to} not found for prompt block ${promptBlockId}`
        });
      }
      const fromConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        fromVersion,
        SNAPSHOT_CONFIG_FIELDS
      );
      const toConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        toVersion,
        SNAPSHOT_CONFIG_FIELDS
      );
      const diffs = chunk2XZ2466F_cjs.computeVersionDiffs(fromConfig, toConfig);
      return {
        diffs,
        fromVersion,
        toVersion
      };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error comparing prompt block versions");
    }
  }
});

exports.ACTIVATE_PROMPT_BLOCK_VERSION_ROUTE = ACTIVATE_PROMPT_BLOCK_VERSION_ROUTE;
exports.COMPARE_PROMPT_BLOCK_VERSIONS_ROUTE = COMPARE_PROMPT_BLOCK_VERSIONS_ROUTE;
exports.CREATE_PROMPT_BLOCK_VERSION_ROUTE = CREATE_PROMPT_BLOCK_VERSION_ROUTE;
exports.DELETE_PROMPT_BLOCK_VERSION_ROUTE = DELETE_PROMPT_BLOCK_VERSION_ROUTE;
exports.GET_PROMPT_BLOCK_VERSION_ROUTE = GET_PROMPT_BLOCK_VERSION_ROUTE;
exports.LIST_PROMPT_BLOCK_VERSIONS_ROUTE = LIST_PROMPT_BLOCK_VERSIONS_ROUTE;
exports.RESTORE_PROMPT_BLOCK_VERSION_ROUTE = RESTORE_PROMPT_BLOCK_VERSION_ROUTE;
//# sourceMappingURL=chunk-HR4F4XZV.cjs.map
//# sourceMappingURL=chunk-HR4F4XZV.cjs.map