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