import { jsxs, jsx } from "react/jsx-runtime";
import { useState } from "react";
import { getAssetUrl } from "../../../../../utils/assetBase.js";
import { Menu } from "../../../../Menu/index.js";
import { Prompt } from "../../../../Prompt/index.js";
import ChatDefaultMessageType from "../ChatDefaultMessageType/ChatDefaultMessageType.js";
import { ChatDeletedMessageType } from "../ChatDeleteMessageType/ChatDeletedMessageType.js";
import ChatEditedMessageType from "../ChatEditedMessageType /ChatEditedMessageType.js";
import { ChatMessageAvatar } from "../ChatMessageAvatar/ChatMessageAvatar.js";
/* empty css                        */
const DeleteIcon = getAssetUrl("icons/deleteIcon.svg");
const EditIcon = getAssetUrl("icons/pencilIcon.svg");
getAssetUrl("icons/replyIcon.svg");
getAssetUrl("icons/unpinedIcon.svg");
const ChatMessage = ({
  messageId,
  author,
  authorName,
  authorAvatar,
  content,
  timestamp,
  isUser = false,
  showActions = true,
  showAvatar = true,
  isDeleted = false,
  edited = false,
  deleted = false,
  onReply,
  onReact,
  onBookmark,
  onPin,
  pinned,
  onMore,
  onEdit,
  onMessageDelete,
  onRefetchMessages,
  type = "default",
  priority = "default",
  actions = [],
  className = "",
  onThreadClick,
  isThreadIconVisible,
  isPinChatIconvisible,
  isHighlighted = false,
  highlightColor = null
}) => {
  const [moreOptionsAnchorEl, setMoreOptionsAnchorEl] = useState(null);
  const [moreOptionsMenuOpen, setMoreOptionsMenuOpen] = useState(false);
  const [showDeleteModal, setShowDeleteModal] = useState(false);
  const [deleting, setDeleting] = useState(false);
  const moreOptionsOptions = [
    // {
    //   label: "Reply",
    //   value: "reply",
    //   icon: <img src={ReplyIcon} alt="reply" />,
    //   onClick: () => {
    //     console.log("Reply");
    //   },
    // },
    {
      label: "Edit",
      value: "edit",
      icon: /* @__PURE__ */ jsx("img", { alt: "edit", src: EditIcon }),
      onClick: () => {
        if (onEdit) {
          onEdit();
        }
        setMoreOptionsMenuOpen(false);
      }
    },
    // {
    //   label: "Unpin",
    //   value: "unpin",
    //   icon: <img src={UnpinIcon} alt="unpin" />,
    //   onClick: () => {
    //     console.log("Unpin");
    //   },
    // },
    {
      label: "Delete",
      value: "delete",
      icon: /* @__PURE__ */ jsx("img", { alt: "delete", src: DeleteIcon }),
      onClick: () => {
        handleDeleteClick();
      }
    }
  ];
  const handleDeleteClick = () => {
    setShowDeleteModal(true);
    setMoreOptionsMenuOpen(false);
  };
  const handleMsgDelete = async (messageId2) => {
    try {
      setDeleting(true);
      if (onMessageDelete) {
        const response = await onMessageDelete(messageId2);
        console.log("[ChatMessage] Message deleted successfully", response);
      }
      setShowDeleteModal(false);
    } catch (err) {
      console.error("[ChatMessage] Delete failed:", err);
    } finally {
      setDeleting(false);
    }
  };
  const messageClass = `chat-message ${isUser ? "user-message" : "assistant-message"} ${isHighlighted ? "highlighted-message" : ""} ${className}`;
  const handleClickOnMoreOptions = (e) => {
    e.stopPropagation();
    setMoreOptionsAnchorEl(e.currentTarget);
    setMoreOptionsMenuOpen(true);
  };
  const isMessageDeleted = isDeleted || deleted;
  const messageTypes = {
    deleted: /* @__PURE__ */ jsx(ChatDeletedMessageType, { isUser, timestamp }),
    default: /* @__PURE__ */ jsx(
      ChatDefaultMessageType,
      {
        content,
        edited,
        handleClickOnMoreOptions,
        highlightColor,
        isHighlighted,
        isPinChatIconvisible,
        isThreadIconVisible,
        isUser,
        pinned,
        priority,
        timestamp,
        visibility: moreOptionsMenuOpen,
        onPin,
        onThreadClick
      }
    ),
    editable: /* @__PURE__ */ jsx(
      ChatEditedMessageType,
      {
        content,
        edited,
        handleClickOnMoreOptions,
        isUser,
        pinned,
        priority,
        timestamp,
        onThreadClick
      }
    )
  };
  return /* @__PURE__ */ jsxs("div", { className: messageClass, children: [
    showAvatar && !isUser && /* @__PURE__ */ jsx(ChatMessageAvatar, { authorAvatar, authorName }),
    /* @__PURE__ */ jsxs("div", { className: "message-content", children: [
      messageTypes[isMessageDeleted ? "deleted" : type],
      /* @__PURE__ */ jsx(
        Menu,
        {
          anchorEl: moreOptionsAnchorEl,
          iconPlacement: "top",
          menuContainerClassName: "more-options-menu",
          open: moreOptionsMenuOpen,
          options: moreOptionsOptions,
          selected: "Edit",
          onClose: () => {
            console.log("onClose");
            setMoreOptionsMenuOpen(false);
          }
        }
      )
    ] }),
    /* @__PURE__ */ jsx(
      Prompt,
      {
        handleClose: () => {
          setShowDeleteModal(false);
        },
        isOpen: showDeleteModal,
        primaryButtonLabel: deleting ? "Deleting..." : "Delete",
        secondaryButtonLabel: "Cancel",
        title: "Are you sure",
        variant: "error",
        onPrimaryButtonClick: () => {
          handleMsgDelete(messageId);
        },
        onSecondaryButtonClick: () => {
          setShowDeleteModal(false);
        },
        children: "This will permanently delete all messages in this conversation."
      }
    )
  ] });
};
export {
  ChatMessage
};
