'use strict';

var chunkHDIYJI2Z_cjs = require('./chunk-HDIYJI2Z.cjs');
var chunk4C5RVGHE_cjs = require('./chunk-4C5RVGHE.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-workspaces.ts
var LIST_STORED_WORKSPACES_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/workspaces",
  responseType: "json",
  queryParamSchema: chunk4C5RVGHE_cjs.listStoredWorkspacesQuerySchema,
  responseSchema: chunk4C5RVGHE_cjs.listStoredWorkspacesResponseSchema,
  summary: "List stored workspaces",
  description: "Returns a paginated list of all workspace configurations stored in the database",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({ mastra, page, perPage, orderBy, authorId, metadata, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const filter = chunkHDIYJI2Z_cjs.resolveAuthorFilter({
        requestContext,
        resource: "stored-workspaces",
        queryAuthorId: authorId
      });
      const scope = await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext);
      const result = await workspaceStore.listResolved({
        page,
        perPage,
        orderBy,
        authorId: filter.kind === "exact" ? filter.authorId : void 0,
        metadata: chunkDRDVJ35H_cjs.scopeStoredResourceMetadata(metadata, scope)
      });
      const visibleWorkspaces = result.workspaces.filter((record) => chunkHDIYJI2Z_cjs.matchesAuthorFilter(record, filter));
      const runtimeWorkspaces = mastra.listWorkspaces();
      const workspaces = visibleWorkspaces.map((ws) => ({
        ...ws,
        runtimeRegistered: ws.id in runtimeWorkspaces
      }));
      return { ...result, workspaces };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error listing stored workspaces");
    }
  }
});
var GET_STORED_WORKSPACE_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/workspaces/:storedWorkspaceId",
  responseType: "json",
  pathParamSchema: chunk4C5RVGHE_cjs.storedWorkspaceIdPathParams,
  responseSchema: chunk4C5RVGHE_cjs.getStoredWorkspaceResponseSchema,
  summary: "Get stored workspace by ID",
  description: "Returns a specific workspace from storage by its unique identifier (resolved with active version config)",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({ mastra, storedWorkspaceId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const workspace = await workspaceStore.getByIdResolved(storedWorkspaceId);
      if (!workspace) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored workspace with id ${storedWorkspaceId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(workspace, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      chunkHDIYJI2Z_cjs.assertReadAccess({
        requestContext,
        resource: "stored-workspaces",
        resourceId: storedWorkspaceId,
        record: workspace
      });
      return workspace;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error getting stored workspace");
    }
  }
});
var CREATE_STORED_WORKSPACE_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/workspaces",
  responseType: "json",
  bodySchema: chunk4C5RVGHE_cjs.createStoredWorkspaceBodySchema,
  responseSchema: chunk4C5RVGHE_cjs.createStoredWorkspaceResponseSchema,
  summary: "Create stored workspace",
  description: "Creates a new workspace configuration in storage with the provided settings",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({
    mastra,
    id: providedId,
    metadata,
    name,
    description,
    filesystem,
    sandbox,
    mounts,
    search,
    skills,
    tools,
    autoSync,
    operationTimeout,
    requestContext
  }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const id = providedId || chunkDRDVJ35H_cjs.toSlug(name);
      if (!id) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Could not derive workspace ID from name. Please provide an explicit id."
        });
      }
      const existing = await workspaceStore.getById(id);
      if (existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(409, { message: `Workspace with id ${id} already exists` });
      }
      const authorId = chunkHDIYJI2Z_cjs.getCallerAuthorId(requestContext) ?? void 0;
      await workspaceStore.create({
        workspace: {
          id,
          authorId,
          metadata: chunkDRDVJ35H_cjs.scopeStoredResourceMetadata(metadata, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext)),
          name,
          description,
          filesystem,
          sandbox,
          mounts,
          search,
          skills,
          tools,
          autoSync,
          operationTimeout
        }
      });
      const resolved = await workspaceStore.getByIdResolved(id);
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve created workspace" });
      }
      return resolved;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error creating stored workspace");
    }
  }
});
var UPDATE_STORED_WORKSPACE_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "PATCH",
  path: "/stored/workspaces/:storedWorkspaceId",
  responseType: "json",
  pathParamSchema: chunk4C5RVGHE_cjs.storedWorkspaceIdPathParams,
  bodySchema: chunk4C5RVGHE_cjs.updateStoredWorkspaceBodySchema,
  responseSchema: chunk4C5RVGHE_cjs.updateStoredWorkspaceResponseSchema,
  summary: "Update stored workspace",
  description: "Updates an existing workspace in storage with the provided fields",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({
    mastra,
    storedWorkspaceId,
    // Metadata-level fields
    metadata,
    // Config fields (snapshot-level)
    name,
    description,
    filesystem,
    sandbox,
    mounts,
    search,
    skills,
    tools,
    autoSync,
    operationTimeout,
    requestContext
  }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const existing = await workspaceStore.getById(storedWorkspaceId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored workspace with id ${storedWorkspaceId} not found` });
      }
      const scope = await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext);
      chunkDRDVJ35H_cjs.assertStoredResourceScope(existing, scope);
      chunkHDIYJI2Z_cjs.assertWriteAccess({
        requestContext,
        resource: "stored-workspaces",
        resourceId: storedWorkspaceId,
        action: "edit",
        record: existing
      });
      const scopedMetadata = metadata !== void 0 ? chunkDRDVJ35H_cjs.scopeStoredResourceMetadata({ ...existing.metadata ?? {}, ...metadata }, scope) : void 0;
      const updateInput = { id: storedWorkspaceId };
      const candidate = {
        ...scopedMetadata !== void 0 ? { metadata: scopedMetadata } : {},
        name,
        description,
        filesystem,
        sandbox,
        mounts,
        search,
        skills,
        tools,
        autoSync,
        operationTimeout
      };
      for (const [key, value] of Object.entries(candidate)) {
        if (value !== void 0) updateInput[key] = value;
      }
      await workspaceStore.update(updateInput);
      const resolved = await workspaceStore.getByIdResolved(storedWorkspaceId);
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve updated workspace" });
      }
      return resolved;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error updating stored workspace");
    }
  }
});
var DELETE_STORED_WORKSPACE_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/stored/workspaces/:storedWorkspaceId",
  responseType: "json",
  pathParamSchema: chunk4C5RVGHE_cjs.storedWorkspaceIdPathParams,
  responseSchema: chunk4C5RVGHE_cjs.deleteStoredWorkspaceResponseSchema,
  summary: "Delete stored workspace",
  description: "Deletes a workspace from storage by its unique identifier",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({ mastra, storedWorkspaceId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const existing = await workspaceStore.getById(storedWorkspaceId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored workspace with id ${storedWorkspaceId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(existing, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      chunkHDIYJI2Z_cjs.assertWriteAccess({
        requestContext,
        resource: "stored-workspaces",
        resourceId: storedWorkspaceId,
        action: "delete",
        record: existing
      });
      await workspaceStore.delete(storedWorkspaceId);
      return {
        success: true,
        message: `Workspace ${storedWorkspaceId} deleted successfully`
      };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error deleting stored workspace");
    }
  }
});

exports.CREATE_STORED_WORKSPACE_ROUTE = CREATE_STORED_WORKSPACE_ROUTE;
exports.DELETE_STORED_WORKSPACE_ROUTE = DELETE_STORED_WORKSPACE_ROUTE;
exports.GET_STORED_WORKSPACE_ROUTE = GET_STORED_WORKSPACE_ROUTE;
exports.LIST_STORED_WORKSPACES_ROUTE = LIST_STORED_WORKSPACES_ROUTE;
exports.UPDATE_STORED_WORKSPACE_ROUTE = UPDATE_STORED_WORKSPACE_ROUTE;
//# sourceMappingURL=chunk-GJBHQJBK.cjs.map
//# sourceMappingURL=chunk-GJBHQJBK.cjs.map