// src/relay/pure-relay/Feedback.tsx
import { graphql, useFragment, useMutation } from "react-relay";
import * as React from "react";
import {
  useOnDeleteFeedback,
  useFeedbackTelemetry
} from "../../events/helpers.mjs";
import { useFormat } from "@nova/react";
var Feedback_feedbackRelayFragment = graphql`
  fragment Feedback_feedbackRelayFragment on Feedback {
    id
    message {
      text
    }
    doesViewerLike
    displayLabel
  }
`;
var Feedback_viewDataRelayFragment = graphql`
  fragment Feedback_viewDataRelayFragment on ViewData {
    viewDataField
    likeLabel
    unlikeLabel
    numberOfLikesLabel
  }
`;
var FeedbackComponent = (props) => {
  const [errorMessage, setErrorMessage] = React.useState(null);
  const feedback = useFragment(Feedback_feedbackRelayFragment, props.feedback);
  const viewData = useFragment(Feedback_viewDataRelayFragment, props.viewData);
  const [like, isPending] = useMutation(
    graphql`
      mutation FeedbackComponent_RelayLikeMutation($input: FeedbackLikeInput!) {
        feedbackLike(input: $input) {
          feedback {
            id
            doesViewerLike
          }
        }
      }
    `
  );
  const onDeleteFeedback = useOnDeleteFeedback(
    feedback.id,
    feedback.message.text
  );
  const feedbackTelemetry = useFeedbackTelemetry();
  const format = useFormat();
  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.displayLabel,
    ": ",
    feedback.message.text,
    /* @__PURE__ */ React.createElement("div", null, format(viewData.numberOfLikesLabel, { count: 2 })),
    /* @__PURE__ */ React.createElement("div", null, viewData.viewDataField),
    /* @__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");
              }
            },
            onError: () => {
              setErrorMessage("Something went wrong");
            }
          });
        }
      },
      feedback.doesViewerLike ? viewData.unlikeLabel : viewData.likeLabel
    ),
    /* @__PURE__ */ React.createElement("button", { onClick: onDeleteFeedback }, "Delete feedback")
  );
};
export {
  FeedbackComponent,
  Feedback_feedbackRelayFragment,
  Feedback_viewDataRelayFragment
};
