"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require$$0 = require("react/jsx-runtime");
const designSystem = require("@choiceform/design-system");
const iconsReact = require("@choiceform/icons-react");
const react = require("../../../_vendor/@legendapp/state/react.cjs");
const index$1 = require("../../../_vendor/usehooks-ts/dist-index.cjs");
const index = require("../../../i18n/index.cjs");
const messageStore = require("../../../stores/message-store.cjs");
const core = require("../../../types/core.cjs");
const ratingStore = require("../../../stores/rating-store.cjs");
const ActionsTv = designSystem.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 = react.observer(function ResponseActions2(props) {
  const { children, onCopyMessage, onDeleteMessage, onRegenerateMessage, onRegenerate, messageId } = props;
  const { response } = props;
  const { confirm } = designSystem.useAlertDialog();
  const id = messageId ?? response?.message?.id;
  const message = react.use$(id ? messageStore.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 = messageStore.messageActions.deleteMessage;
  messageStore.messageActions.updateMessage;
  const retryMessageHandler = messageStore.messageActions.retryMessage;
  messageStore.messageActions.cancelStreamingMessage;
  const isUser = message?.role === "user";
  const isFailed = message?.status === core.Status.Failed;
  const copyMessageSuccess = index.t("response", "copy-message-success");
  const deleteDialogTitle = index.t("response", "delete-dialog-title");
  const deleteDialogDescription = index.t("response", "delete-dialog-description");
  const deleteDialogConfirm = index.t("common", "confirm");
  const deleteDialogCancel = index.t("common", "cancel");
  const copyMessage = index.t("response", "copy-message");
  const deleteMessage = index.t("response", "delete-message");
  const retryMessage = index.t("response", "retry-message", {
    retry: message?.retryCount || 0,
    max: message?.maxRetries || 3
  });
  const regenerateResponse = index.t("response", "regenerate-response");
  const goodResponse = index.t("response", "good-response");
  const badResponse = index.t("response", "bad-response");
  const handleCopy = index$1.useEventCallback(async () => {
    try {
      if (onCopyMessage) {
        await onCopyMessage(message?.id || "");
      } else {
        await navigator.clipboard.writeText(message?.content || "");
      }
      designSystem.notifications({
        text: copyMessageSuccess
      });
    } catch (error) {
      console.error("Copy message failed:", error);
    }
  });
  const handleDelete = index$1.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 = index$1.useEventCallback((rating) => {
    if (!message) return;
    try {
      ratingStore.ratingActions.openFeedbackDialog(message.id, rating);
    } catch (error) {
      console.error("Rate message failed:", error);
    }
  });
  const handleRegenerate = index$1.useEventCallback(async () => {
    if (!message) return;
    try {
      if (onRegenerateMessage) {
        await onRegenerateMessage(message.id, () => {
          onRegenerate?.();
        });
      }
    } catch (error) {
      console.error("Regenerate response failed:", error);
    }
  });
  const handleRetry = index$1.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__ */ require$$0.jsxs("div", { className: tv.actions(), children: [
    /* @__PURE__ */ require$$0.jsx(
      designSystem.IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleCopy,
        tooltip: { content: copyMessage },
        children: /* @__PURE__ */ require$$0.jsx(iconsReact.PromptActionsClipboard, {})
      }
    ),
    /* @__PURE__ */ require$$0.jsx(
      designSystem.IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleDelete,
        tooltip: { content: deleteMessage },
        children: /* @__PURE__ */ require$$0.jsx(iconsReact.PromptActionsTrash, {})
      }
    ),
    isFailed && /* @__PURE__ */ require$$0.jsx(
      designSystem.IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleRetry,
        tooltip: {
          content: retryMessage
        },
        children: /* @__PURE__ */ require$$0.jsx(iconsReact.PromptActionsRegenerate, {})
      }
    ),
    isUser ? /* @__PURE__ */ require$$0.jsx(
      designSystem.IconButton,
      {
        variant: "ghost",
        className: tv.actionsItem(),
        onClick: handleRegenerate,
        tooltip: { content: regenerateResponse },
        children: /* @__PURE__ */ require$$0.jsx(iconsReact.PromptActionsRegenerate, {})
      }
    ) : /* @__PURE__ */ require$$0.jsxs(require$$0.Fragment, { children: [
      /* @__PURE__ */ require$$0.jsx(
        designSystem.IconButton,
        {
          variant: "ghost",
          className: tv.actionsItem({ ratingActions: message.rating === "positive" }),
          onClick: () => handleRate("positive"),
          tooltip: { content: goodResponse },
          children: /* @__PURE__ */ require$$0.jsx(iconsReact.PromptActionsThumbsUp, {})
        }
      ),
      /* @__PURE__ */ require$$0.jsx(
        designSystem.IconButton,
        {
          variant: "ghost",
          className: tv.actionsItem({ ratingActions: message.rating === "negative" }),
          onClick: () => handleRate("negative"),
          tooltip: { content: badResponse },
          children: /* @__PURE__ */ require$$0.jsx(iconsReact.PromptActionsThumbsDown, {})
        }
      )
    ] }),
    children
  ] });
});
exports.ActionsTv = ActionsTv;
exports.ResponseActions = ResponseActions;
//# sourceMappingURL=response-actions.cjs.map
