"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const planStore = require("../stores/plan-store.cjs");
const usePlanStreamHandler = () => {
  const handleStreamChunk = React.useCallback((chunk, messageId) => {
    if (!chunk.chunkType) return;
    switch (chunk.chunkType) {
      case "plan": {
        if (chunk.metadata?.planSteps) {
          planStore.planActions.initializePlan(chunk.metadata.planSteps, messageId);
        }
        break;
      }
      case "plan_update": {
        if (chunk.metadata?.planSteps) {
          planStore.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;
            planStore.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;
            planStore.planActions.completeStep(stepIndex);
          }
        }
        break;
      }
      case "complete": {
        planStore.planActions.completePlan();
        break;
      }
      case "error": {
        const currentStepIndex = planStore.planStore$.currentStepIndex.peek();
        if (currentStepIndex >= 0 && chunk.content) {
          planStore.planActions.failStep(currentStepIndex, chunk.content);
        }
        break;
      }
      case "replan": {
        if (chunk.metadata?.planSteps) {
          planStore.planActions.updatePlan(chunk.metadata.planSteps);
        }
        break;
      }
    }
  }, []);
  const clearPlan = React.useCallback(() => {
    planStore.planActions.clearPlan();
  }, []);
  return {
    handleStreamChunk,
    clearPlan
  };
};
exports.usePlanStreamHandler = usePlanStreamHandler;
//# sourceMappingURL=use-plan-stream-handler.cjs.map
