import { useEffect } from "react";
import { use$ as useSelector } from "../../../../../_vendor/@legendapp/state/react.js";
import { messageStore$ } from "../../../../../stores/message-store.js";
import { planStore$, planActions } from "../../../../../stores/plan-store.js";
import { ContentType } from "../../../../../types/core.js";
const usePlanUpdates = () => {
  const messages = useSelector(messageStore$.loadedMessages);
  const currentMessageId = useSelector(planStore$.currentMessageId);
  useEffect(() => {
    if (!currentMessageId) return;
    const message = messages[currentMessageId];
    if (!message) return;
    if (message.contentSegments) {
      message.contentSegments.forEach((segment) => {
        if (segment.type === ContentType.Planning && segment.planSteps) {
          const isNewPlan = !planStore$.currentPlan.peek();
          const extendedSteps = segment.planSteps.map((step) => ({
            ...step,
            completed: false
          }));
          if (isNewPlan) {
            planActions.initializePlan(extendedSteps, currentMessageId);
          } else {
            planActions.updatePlan(extendedSteps);
          }
        }
      });
    }
    const messageContent = typeof message.content === "string" ? message.content : "";
    if (message.contentType === ContentType.Progress) {
      const stepMatch = messageContent.match(/正在执行步骤\s*(\d+)\/(\d+)/);
      if (stepMatch) {
        const currentStep = parseInt(stepMatch[1]) - 1;
        planActions.startStep(currentStep);
      }
    }
    if (messageContent.includes("步骤") && messageContent.includes("已完成")) {
      const stepMatch = messageContent.match(/步骤\s*(\d+)\/(\d+)\s*已完成/);
      if (stepMatch) {
        const completedStep = parseInt(stepMatch[1]) - 1;
        planActions.completeStep(completedStep);
      }
    }
    if (messageContent.includes("所有步骤已完成") || messageContent.includes("任务成功执行")) {
      planActions.completePlan();
    }
  }, [messages, currentMessageId]);
  return {
    currentPlan: useSelector(planStore$.currentPlan),
    currentStepIndex: useSelector(planStore$.currentStepIndex),
    isExecuting: useSelector(planStore$.isExecuting),
    progress: planActions.getProgress()
  };
};
export {
  usePlanUpdates
};
//# sourceMappingURL=use-plan-updates.js.map
