import { jsx } from "react/jsx-runtime";
import { observer, use$ as useSelector } from "../../_vendor/@legendapp/state/react.js";
import React__default, { useMemo } from "react";
import { responseTv } from "./tv.js";
import { useResponseState } from "./hooks/use-response-state.js";
import { motion } from "../../_vendor/framer-motion/dist-es-render-components-motion-proxy.js";
import { ResponseAvatar } from "./components/response-avatar.js";
import { ResponseActions } from "./components/response-actions.js";
import { ResponseTimestamp } from "./components/response-timestamp.js";
import { ResponseError } from "./components/response-error.js";
import { ResponseContent } from "./components/response-content.js";
import { messageComputed$ } from "../../stores/message-store.js";
import { Status } from "../../types/core.js";
const ResponseItem = observer(function ResponseItem2(props) {
  const { message, children, className = "" } = props;
  const selectedMessage = useSelector(message ? messageComputed$.getMessage(message.id) : void 0);
  const { itemRef, showActions } = useResponseState(selectedMessage ?? null);
  const hasComponents = useMemo(() => {
    let hasAvatar = false;
    let hasActions = false;
    let hasTimestamp = false;
    let hasError = false;
    const scan = (nodes) => {
      React__default.Children.forEach(nodes, (node) => {
        if (!React__default.isValidElement(node)) return;
        const el = node;
        if (el.type === ResponseAvatar) hasAvatar = true;
        if (el.type === ResponseActions) hasActions = true;
        if (el.type === ResponseTimestamp) hasTimestamp = true;
        if (el.type === ResponseError) hasError = true;
        const childNodes = el.props?.children;
        if (childNodes) scan(childNodes);
      });
    };
    scan(children);
    return { hasAvatar, hasActions, hasTimestamp, hasError };
  }, [children]);
  const tv = responseTv({
    hasAvatar: hasComponents.hasAvatar,
    hasActions: hasComponents.hasActions,
    hasTimestamp: hasComponents.hasTimestamp,
    hasError: hasComponents.hasError
  });
  const injectResponse = (nodes) => {
    return React__default.Children.map(nodes, (node) => {
      if (!React__default.isValidElement(node)) return node;
      const el = node;
      const nextChildren = el.props?.children ? injectResponse(el.props.children) : el.props?.children;
      const isTargetComponent = el.type === ResponseAvatar || el.type === ResponseActions || el.type === ResponseTimestamp || el.type === ResponseError || el.type === ResponseContent;
      if (isTargetComponent) {
        if (el.type === ResponseContent) {
          return React__default.cloneElement(
            el,
            {
              hasActions: hasComponents.hasActions,
              hasAvatar: hasComponents.hasAvatar,
              hasTimestamp: hasComponents.hasTimestamp,
              messageId: message?.id,
              showActions
            }
          );
        }
        return React__default.cloneElement(
          el,
          {
            hasActions: hasComponents.hasActions,
            hasAvatar: hasComponents.hasAvatar,
            hasTimestamp: hasComponents.hasTimestamp,
            messageId: message?.id,
            showActions
          },
          nextChildren
        );
      }
      return React__default.cloneElement(el, void 0, nextChildren);
    });
  };
  const injectedChildren = injectResponse(children);
  if (!selectedMessage) return null;
  return /* @__PURE__ */ jsx(
    motion.div,
    {
      initial: { opacity: 0 },
      animate: { opacity: 1 },
      exit: { opacity: 0 },
      transition: {
        duration: 0.2,
        delay: message?.role === "user" || message?.status === Status.Completed ? 0 : 0.5
      },
      className: tv.root({ className }),
      ref: itemRef,
      children: injectedChildren
    }
  );
});
export {
  ResponseItem
};
//# sourceMappingURL=response-item.js.map
