'use strict';

var chunkK2SXZLA3_cjs = require('./chunk-K2SXZLA3.cjs');
var zodToJson = require('@mastra/core/utils/zod-to-json');

function normalizeRoutePath(path) {
  let normalized = path.trim();
  if (normalized.includes("..") || normalized.includes("?") || normalized.includes("#")) {
    throw new Error(`Invalid route path: "${path}". Path cannot contain '..', '?', or '#'`);
  }
  normalized = normalized.replace(/\/+/g, "/");
  if (normalized === "/" || normalized === "") {
    return "";
  }
  if (normalized.endsWith("/")) {
    normalized = normalized.slice(0, -1);
  }
  if (!normalized.startsWith("/")) {
    normalized = `/${normalized}`;
  }
  return normalized;
}
function looksLikeProcessorStepSchema(schema) {
  if (!schema) return false;
  try {
    const jsonSchema = zodToJson.zodToJsonSchema(schema);
    const variants = jsonSchema.anyOf || jsonSchema.oneOf;
    if (!variants || !Array.isArray(variants)) return false;
    const processorPhases = /* @__PURE__ */ new Set(["input", "inputStep", "outputStream", "outputResult", "outputStep"]);
    for (const variant of variants) {
      const properties = variant.properties;
      if (!properties?.phase) return false;
      const phaseSchema = properties.phase;
      const phaseConst = phaseSchema?.const;
      const phaseEnum = Array.isArray(phaseSchema?.enum) ? phaseSchema.enum : [];
      const phaseValues = phaseConst ? [phaseConst] : phaseEnum;
      if (!phaseValues.length || phaseValues.some((phase) => !processorPhases.has(phase))) {
        return false;
      }
    }
    return variants.length > 0;
  } catch {
    return false;
  }
}
function getSteps(steps, path) {
  return Object.entries(steps).reduce((acc, [key, step]) => {
    const fullKey = path ? `${path}.${key}` : key;
    acc[fullKey] = {
      id: step.id,
      description: step.description,
      inputSchema: step.inputSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.inputSchema)) : void 0,
      outputSchema: step.outputSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.outputSchema)) : void 0,
      resumeSchema: step.resumeSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.resumeSchema)) : void 0,
      suspendSchema: step.suspendSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.suspendSchema)) : void 0,
      stateSchema: step.stateSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.stateSchema)) : void 0,
      isWorkflow: step.component === "WORKFLOW",
      component: step.component
    };
    if (step.component === "WORKFLOW" && step.steps) {
      const nestedSteps = getSteps(step.steps, fullKey) || {};
      acc = { ...acc, ...nestedSteps };
    }
    return acc;
  }, {});
}
function getWorkflowInfo(workflow, partial = false) {
  if (partial) {
    return {
      name: workflow.name,
      description: workflow.description,
      stepCount: Object.keys(workflow.steps).length,
      stepGraph: workflow.serializedStepGraph,
      options: workflow.options,
      steps: {},
      allSteps: {},
      inputSchema: void 0,
      outputSchema: void 0,
      stateSchema: void 0
    };
  }
  return {
    name: workflow.name,
    description: workflow.description,
    steps: Object.entries(workflow.steps).reduce((acc, [key, step]) => {
      acc[key] = {
        id: step.id,
        description: step.description,
        inputSchema: step.inputSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.inputSchema)) : void 0,
        outputSchema: step.outputSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.outputSchema)) : void 0,
        resumeSchema: step.resumeSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.resumeSchema)) : void 0,
        suspendSchema: step.suspendSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.suspendSchema)) : void 0,
        stateSchema: step.stateSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.stateSchema)) : void 0,
        requestContextSchema: step.requestContextSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(step.requestContextSchema)) : void 0,
        component: step.component
      };
      return acc;
    }, {}),
    allSteps: getSteps(workflow.steps) || {},
    stepGraph: workflow.serializedStepGraph,
    inputSchema: workflow.inputSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(workflow.inputSchema)) : void 0,
    outputSchema: workflow.outputSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(workflow.outputSchema)) : void 0,
    stateSchema: workflow.stateSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(workflow.stateSchema)) : void 0,
    requestContextSchema: workflow.requestContextSchema ? chunkK2SXZLA3_cjs.stringify(zodToJson.zodToJsonSchema(workflow.requestContextSchema)) : void 0,
    options: workflow.options,
    isProcessorWorkflow: workflow.type === "processor" || looksLikeProcessorStepSchema(workflow.inputSchema)
  };
}
var WorkflowRegistry = class {
  static additionalWorkflows = {};
  /**
   * Register a workflow temporarily
   */
  static registerTemporaryWorkflow(id, workflow) {
    this.additionalWorkflows[id] = workflow;
  }
  /**
   * Register all workflows from map
   */
  static registerTemporaryWorkflows(workflows, mastra) {
    for (const [id, workflow] of Object.entries(workflows)) {
      if (mastra) {
        workflow.__registerMastra(mastra);
        workflow.__registerPrimitives({
          logger: mastra.getLogger(),
          storage: mastra.getStorage(),
          agents: mastra.listAgents(),
          tts: mastra.getTTS(),
          vectors: mastra.listVectors()
        });
      }
      this.additionalWorkflows[id] = workflow;
    }
  }
  /**
   * Get a workflow by ID from the registry (returns undefined if not found)
   */
  static getWorkflow(workflowId) {
    return this.additionalWorkflows[workflowId];
  }
  /**
   * Get all workflows from the registry
   */
  static getAllWorkflows() {
    return { ...this.additionalWorkflows };
  }
  /**
   * Clean up a temporary workflow
   */
  static cleanupTemporaryWorkflow(workflowId) {
    delete this.additionalWorkflows[workflowId];
  }
  /**
   * Clean up all registered workflows
   */
  static cleanup() {
    this.additionalWorkflows = {};
  }
  /**
   * Check if a workflow ID is a valid agent-builder workflow
   */
  static isAgentBuilderWorkflow(workflowId) {
    return workflowId in this.additionalWorkflows;
  }
  /**
   * Get all registered temporary workflow IDs (for debugging)
   */
  static getRegisteredWorkflowIds() {
    return Object.keys(this.additionalWorkflows);
  }
};
function convertInstructionsToString(message) {
  if (!message) {
    return "";
  }
  if (typeof message === "string") {
    return message;
  }
  if (Array.isArray(message)) {
    return message.map((m) => {
      if (typeof m === "string") {
        return m;
      }
      return typeof m.content === "string" ? m.content : "";
    }).filter((content) => content).join("\n");
  }
  return typeof message.content === "string" ? message.content : "";
}

exports.WorkflowRegistry = WorkflowRegistry;
exports.convertInstructionsToString = convertInstructionsToString;
exports.getWorkflowInfo = getWorkflowInfo;
exports.normalizeRoutePath = normalizeRoutePath;
//# sourceMappingURL=chunk-A2K5ZL47.cjs.map
//# sourceMappingURL=chunk-A2K5ZL47.cjs.map