import { jsxs, jsx, Fragment } from "react/jsx-runtime";
import { tcv, useAlertDialog, notifications, IconButton } from "@choiceform/design-system";
import { PromptActionsClipboard, PromptActionsTrash, PromptActionsRegenerate, PromptActionsThumbsUp, PromptActionsThumbsDown } from "@choiceform/icons-react";
import { observer, use$ as useSelector } from "../../../_vendor/@legendapp/state/react.js";
import { useEventCallback } from "../../../_vendor/usehooks-ts/dist-index.js";
import { t } from "../../../i18n/index.js";
import { messageComputed$, messageActions } from "../../../stores/message-store.js";
import { Status } from "../../../types/core.js";
import { ratingActions } from "../../../stores/rating-store.js";
const ActionsTv = tcv({
  slots: {
    actions: [
      "bg-secondary-background flex items-center gap-1 rounded-md border p-0.5",
      "response-actions place-self-start"
    ],
    actionsItem: "text-icon-secondary size-3.5",
    time: "text-secondary-foreground text-body-medium",
    status: "flex items-center gap-1"
  },
  variants: {
    showActions: {
      true: {
        actions: "opacity-100"
      },
      false: {
        actions: "opacity-0"
      }
    },
    ratingActions: {
      true: {
        actionsItem: "text-accent-foreground"
      },
      false: {
        actionsItem: ""
      }
    },
    hasAvatar: {
      true: {
        actions: "col-start-2"
      },
      false: {}
    }
  },
  defaultVariants: {
    showActions: false,
    ratingActions: false,
    hasAvatar: false
  }
});
const ResponseActions = observer(function ResponseActions2(props) {
  const { children, onCopyMessage, onDeleteMessage, onRegenerateMessage, onRegenerate, messageId } = props;
  const { response } = props;
  const { confirm } = useAlertDialog();
  const id = messageId ?? response?.message?.id;
  const message = useSelector(id ? messageComputed$.getMessage(id) : void 0);
  const showActions = props.showActions ?? response?.showActions ?? false;
  const isStreaming = message?.isStreaming ?? false;
  const hasAvatar = props.hasAvatar ?? response?.hasAvatar ?? false;
  const deleteMessageHandler = messageActions.deleteMessage;
  messageActions.updateMessage;
  const retryMessageHandler = messageActions.retryMessage;
  messageActions.cancelStreamingMessage;
  const isUser = message?.role === "user";
  const isFailed = message?.status === Status.Failed;
  const copyMessageSuccess = t("response", "copy-message-success");
  const deleteDialogTitle = t("response", "delete-dialog-title");
  const deleteDialogDescription = t("response", "delete-dialog-description");
  const deleteDialogConfirm = t("common", "confirm");
  const deleteDialogCancel = t("common", "cancel");
  const copyMessage = t("response", "copy-message");
  const deleteMessage = t("response", "delete-message");
  const retryMessage = t("response", "retry-message", {
    retry: message?.retryCount || 0,
    max: message?.maxRetries || 3
  });
  const regenerateResponse = t("response", "regenerate-response");
  const goodResponse = t("response", "good-response");
  const badResponse = t("response", "bad-response");
  const handleCopy = useEventCallback(async () => {
    try {
      if (onCopyMessage) {
        await onCopyMessage(message?.id || "");
      } else {
        await navigator.clipboard.writeText(message?.content || "");
      }
      notifications({
        text: copyMessageSuccess
      });
    } catch (error) {
      console.error("Copy message failed:", error);
    }
  });
  const handleDelete = useEventCallback(async () => {
    const confirmed = await confirm({
      title: deleteDialogTitle,
      description: deleteDialogDescription,
      confirmText: deleteDialogConfirm,
      cancelText: deleteDialogCancel,
      confirmVariant: "destructive",
      variant: "danger"
    });
    if (confirmed) {
      deleteMessageHandler(message?.id || "");
      if (onDeleteMessage) {
        await onDeleteMessage(message?.id || "");
      }
    }
  });
  const handleRate = useEventCallback((rating) => {
    if (!message) return;
    try {
      ratingActions.openFeedbackDialog(message.id, rating);
    } catch (error) {
      console.error("Rate message failed:", error);
    }
  });
  const handleRegenerate = useEventCallback(async () => {
    if (!message) return;
    try {
      if (onRegenerateMessage) {
        await onRegenerateMessage(message.id, () => {
          onRegenerate?.();
        });
      }
    } catch (error) {
      console.error("Regenerate response failed:", error);
    }
  });
  const handleRetry = useEventCallback(async () => {
    if (!message) return;
    try {
      await retryMessageHandler?.(message.id);
      if (onRegenerateMessage) {
        await onRegenerateMessage(message.id, () => {
          onRegenerate?.();
        });
      }
    } catch (error) {
      console.error("Retry message failed:", error);
    }
  });
  const tv = ActionsTv({ showActions: showActions && !isStreaming, hasAvatar });
  if (!message) return null;
  return /* @__PURE__ */ jsxs("div", { className: tv.actions(), children: [
    /* @__PURE__ */ jsx(
      IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleCopy,
        tooltip: { content: copyMessage },
        children: /* @__PURE__ */ jsx(PromptActionsClipboard, {})
      }
    ),
    /* @__PURE__ */ jsx(
      IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleDelete,
        tooltip: { content: deleteMessage },
        children: /* @__PURE__ */ jsx(PromptActionsTrash, {})
      }
    ),
    isFailed && /* @__PURE__ */ jsx(
      IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleRetry,
        tooltip: {
          content: retryMessage
        },
        children: /* @__PURE__ */ jsx(PromptActionsRegenerate, {})
      }
    ),
    isUser ? /* @__PURE__ */ jsx(
      IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleRegenerate,
        tooltip: { content: regenerateResponse },
        children: /* @__PURE__ */ jsx(PromptActionsRegenerate, {})
      }
    ) : /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(
        IconButton,
        {
          variant: "ghost",
          className: tv.actionsItem({ ratingActions: message.rating === "positive" }),
          onClick: () => handleRate("positive"),
          tooltip: { content: goodResponse },
          children: /* @__PURE__ */ jsx(PromptActionsThumbsUp, {})
        }
      ),
      /* @__PURE__ */ jsx(
        IconButton,
        {
          variant: "ghost",
          className: tv.actionsItem({ ratingActions: message.rating === "negative" }),
          onClick: () => handleRate("negative"),
          tooltip: { content: badResponse },
          children: /* @__PURE__ */ jsx(PromptActionsThumbsDown, {})
        }
      )
    ] }),
    children
  ] });
});
export {
  ActionsTv,
  ResponseActions
};
//# sourceMappingURL=response-actions.js.map
