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