"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
});
module.exports = __toCommonJS(Feedback_exports);
var import_react = require("@nova/react");
var React = __toESM(require("react"), 1);
var import_helpers = require("../../events/helpers");
const Feedback_feedbackFragment = import_react.graphql`
  fragment Feedback_feedbackFragment on Feedback {
    id
    message {
      text
    }
    doesViewerLike
  }
`;
const FeedbackComponent = (props) => {
  const [errorMessage, setErrorMessage] = React.useState(null);
  const feedback = (0, import_react.useFragment)(Feedback_feedbackFragment, props.feedback);
  const [like, isPending] = (0, import_react.useMutation)(import_react.graphql`
    mutation FeedbackComponent_LikeMutation($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)();
  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")
  );
};
