"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Feedback_exports = {};
__export(Feedback_exports, {
  FeedbackComponent: () => FeedbackComponent,
  Feedback_feedbackRelayFragment: () => Feedback_feedbackRelayFragment,
  Feedback_viewDataRelayFragment: () => Feedback_viewDataRelayFragment
});
module.exports = __toCommonJS(Feedback_exports);
var import_react_relay = require("react-relay");
var React = __toESM(require("react"), 1);
var import_helpers = require("../../events/helpers");
var import_react = require("@nova/react");
const Feedback_feedbackRelayFragment = import_react_relay.graphql`
  fragment Feedback_feedbackRelayFragment on Feedback {
    id
    message {
      text
    }
    doesViewerLike
    displayLabel
  }
`;
const Feedback_viewDataRelayFragment = import_react_relay.graphql`
  fragment Feedback_viewDataRelayFragment on ViewData {
    viewDataField
    likeLabel
    unlikeLabel
    numberOfLikesLabel
  }
`;
const FeedbackComponent = (props) => {
  const [errorMessage, setErrorMessage] = React.useState(null);
  const feedback = (0, import_react_relay.useFragment)(Feedback_feedbackRelayFragment, props.feedback);
  const viewData = (0, import_react_relay.useFragment)(Feedback_viewDataRelayFragment, props.viewData);
  const [like, isPending] = (0, import_react_relay.useMutation)(
    import_react_relay.graphql`
      mutation FeedbackComponent_RelayLikeMutation($input: FeedbackLikeInput!) {
        feedbackLike(input: $input) {
          feedback {
            id
            doesViewerLike
          }
        }
      }
    `
  );
  const onDeleteFeedback = (0, import_helpers.useOnDeleteFeedback)(
    feedback.id,
    feedback.message.text
  );
  const feedbackTelemetry = (0, import_helpers.useFeedbackTelemetry)();
  const format = (0, import_react.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")
  );
};
