// src/apollo/Feedback/Feedback.tsx
import { graphql, useFragment, useMutation } from "@nova/react";
import * as React from "react";
import {
  useOnDeleteFeedback,
  useFeedbackTelemetry
} from "../../events/helpers.mjs";
var Feedback_feedbackFragment = graphql`
  fragment Feedback_feedbackFragment on Feedback {
    id
    message {
      text
    }
    doesViewerLike
  }
`;
var FeedbackComponent = (props) => {
  const [errorMessage, setErrorMessage] = React.useState(null);
  const feedback = useFragment(Feedback_feedbackFragment, props.feedback);
  const [like, isPending] = useMutation(graphql`
    mutation FeedbackComponent_LikeMutation($input: FeedbackLikeInput!) {
      feedbackLike(input: $input) {
        feedback {
          id
          doesViewerLike
        }
      }
    }
  `);
  const onDeleteFeedback = useOnDeleteFeedback(
    feedback.id,
    feedback.message.text
  );
  const feedbackTelemetry = useFeedbackTelemetry();
  React.useEffect(() => {
    return () => {
      feedbackTelemetry("FeedbackComponentUnmounted");
    };
  }, []);
  return /* @__PURE__ */ React.createElement(
    "div",
    {
      style: {
        display: "flex",
        flexDirection: "column",
        gap: 4,
        alignItems: "start"
      }
    },
    errorMessage != null && /* @__PURE__ */ React.createElement("div", { style: { color: "red" } }, errorMessage),
    "Feedback: ",
    feedback.message.text,
    /* @__PURE__ */ React.createElement(
      "button",
      {
        id: "likeButton",
        disabled: isPending,
        onClick: () => {
          like({
            variables: {
              input: {
                feedbackId: feedback.id,
                doesViewerLike: !feedback.doesViewerLike
              }
            },
            optimisticResponse: {
              feedbackLike: {
                feedback: {
                  id: feedback.id,
                  doesViewerLike: !feedback.doesViewerLike
                }
              }
            },
            onCompleted: (response) => {
              if (response.feedbackLike.feedback.doesViewerLike) {
                feedbackTelemetry("FeedbackLiked");
              } else {
                feedbackTelemetry("FeedbackUnliked");
              }
            }
          }).catch(() => {
            setErrorMessage("Something went wrong");
          });
        }
      },
      feedback.doesViewerLike ? "Unlike" : "Like"
    ),
    /* @__PURE__ */ React.createElement("button", { onClick: onDeleteFeedback }, "Delete feedback")
  );
};
export {
  FeedbackComponent
};
