import { useCallback } from "react";
import { planActions, planStore$ } from "../stores/plan-store.js";
const usePlanStreamHandler = () => {
  const handleStreamChunk = useCallback((chunk, messageId) => {
    if (!chunk.chunkType) return;
    switch (chunk.chunkType) {
      case "plan": {
        if (chunk.metadata?.planSteps) {
          planActions.initializePlan(chunk.metadata.planSteps, messageId);
        }
        break;
      }
      case "plan_update": {
        if (chunk.metadata?.planSteps) {
          planActions.updatePlan(chunk.metadata.planSteps);
        }
        break;
      }
      case "step_start": {
        if (chunk.content) {
          const match = chunk.content.match(/正在执行步骤\s*(\d+)\/(\d+)/);
          if (match) {
            const stepIndex = parseInt(match[1]) - 1;
            planActions.startStep(stepIndex);
          }
        }
        break;
      }
      case "step_complete": {
        if (chunk.content) {
          const match = chunk.content.match(/步骤\s*(\d+)\/(\d+)\s*已完成/);
          if (match) {
            const stepIndex = parseInt(match[1]) - 1;
            planActions.completeStep(stepIndex);
          }
        }
        break;
      }
      case "complete": {
        planActions.completePlan();
        break;
      }
      case "error": {
        const currentStepIndex = planStore$.currentStepIndex.peek();
        if (currentStepIndex >= 0 && chunk.content) {
          planActions.failStep(currentStepIndex, chunk.content);
        }
        break;
      }
      case "replan": {
        if (chunk.metadata?.planSteps) {
          planActions.updatePlan(chunk.metadata.planSteps);
        }
        break;
      }
    }
  }, []);
  const clearPlan = useCallback(() => {
    planActions.clearPlan();
  }, []);
  return {
    handleStreamChunk,
    clearPlan
  };
};
export {
  usePlanStreamHandler
};
//# sourceMappingURL=use-plan-stream-handler.js.map
