import {
  useCommentReply_default
} from "./chunk-VHHO3WFH.mjs";
import {
  useCommentCreateMutation
} from "./chunk-4ABLTSXD.mjs";
import {
  SocialInput_default
} from "./chunk-KTIBUWBK.mjs";
import {
  DEFAULT_SOCIAL_UPSERT_FORM_VALUES,
  SOCIAL_UPSERT_FORM_VALIDATION_SCHEMA
} from "./chunk-X7KR3Z6W.mjs";

// tmp-babel/comments/web/CommentCreate/index.js
import { forwardRef } from "react";
import { useCurrentProfile } from "@baseapp-frontend/authentication";
import { setFormRelayErrors } from "@baseapp-frontend/utils";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { ConnectionHandler } from "react-relay";
import { jsx as _jsx } from "react/jsx-runtime";
function _typeof(o) {
  "@babel/helpers - typeof";
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o2) {
    return typeof o2;
  } : function(o2) {
    return o2 && "function" == typeof Symbol && o2.constructor === Symbol && o2 !== Symbol.prototype ? "symbol" : typeof o2;
  }, _typeof(o);
}
function ownKeys(e, r) {
  var t = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var o = Object.getOwnPropertySymbols(e);
    r && (o = o.filter(function(r2) {
      return Object.getOwnPropertyDescriptor(e, r2).enumerable;
    })), t.push.apply(t, o);
  }
  return t;
}
function _objectSpread(e) {
  for (var r = 1; r < arguments.length; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2 ? ownKeys(Object(t), true).forEach(function(r2) {
      _defineProperty(e, r2, t[r2]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r2) {
      Object.defineProperty(e, r2, Object.getOwnPropertyDescriptor(t, r2));
    });
  }
  return e;
}
function _defineProperty(e, r, t) {
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
}
function _toPropertyKey(t) {
  var i = _toPrimitive(t, "string");
  return "symbol" == _typeof(i) ? i : i + "";
}
function _toPrimitive(t, r) {
  if ("object" != _typeof(t) || !t) return t;
  var e = t[Symbol.toPrimitive];
  if (void 0 !== e) {
    var i = e.call(t, r || "default");
    if ("object" != _typeof(i)) return i;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return ("string" === r ? String : Number)(t);
}
function _slicedToArray(r, e) {
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray(r, a) {
  if (r) {
    if ("string" == typeof r) return _arrayLikeToArray(r, a);
    var t = {}.toString.call(r).slice(8, -1);
    return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
  }
}
function _arrayLikeToArray(r, a) {
  (null == a || a > r.length) && (a = r.length);
  for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  return n;
}
function _iterableToArrayLimit(r, l) {
  var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  if (null != t) {
    var e, n, i, u, a = [], f = true, o = false;
    try {
      if (i = (t = t.call(r)).next, 0 === l) {
        if (Object(t) !== t) return;
        f = false;
      } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = true) ;
    } catch (r2) {
      o = true, n = r2;
    } finally {
      try {
        if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return;
      } finally {
        if (o) throw n;
      }
    }
    return a;
  }
}
function _arrayWithHoles(r) {
  if (Array.isArray(r)) return r;
}
var nextClientMutationId = 0;
var CommentCreate = /* @__PURE__ */ forwardRef(function(_ref, ref) {
  var targetObjectId = _ref.targetObjectId, autoFocusInput = _ref.autoFocusInput, _ref$SocialInput = _ref.SocialInput, SocialInput = _ref$SocialInput === void 0 ? SocialInput_default : _ref$SocialInput, _ref$SocialInputProps = _ref.SocialInputProps, SocialInputProps = _ref$SocialInputProps === void 0 ? {} : _ref$SocialInputProps;
  var _useCurrentProfile = useCurrentProfile(), currentProfile = _useCurrentProfile.currentProfile;
  var commentReply = useCommentReply_default();
  var isReply = !!commentReply.inReplyToId;
  var form = useForm({
    defaultValues: DEFAULT_SOCIAL_UPSERT_FORM_VALUES,
    resolver: zodResolver(SOCIAL_UPSERT_FORM_VALIDATION_SCHEMA)
  });
  var _useCommentCreateMuta = useCommentCreateMutation(), _useCommentCreateMuta2 = _slicedToArray(_useCommentCreateMuta, 2), commitMutation = _useCommentCreateMuta2[0], isMutationInFlight = _useCommentCreateMuta2[1];
  var onSubmit = function onSubmit2(data) {
    var _commentReply$inReply;
    if (isMutationInFlight) return;
    nextClientMutationId += 1;
    var clientMutationId = nextClientMutationId.toString();
    var connectionID = ConnectionHandler.getConnectionID((_commentReply$inReply = commentReply.inReplyToId) !== null && _commentReply$inReply !== void 0 ? _commentReply$inReply : targetObjectId, "CommentsList_comments");
    commitMutation({
      variables: {
        input: {
          body: data.body,
          targetObjectId,
          inReplyToId: commentReply.inReplyToId,
          profileId: currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id,
          clientMutationId
        },
        connections: [connectionID]
      },
      onCompleted: function onCompleted(response, errors) {
        var _response$commentCrea;
        if (errors) {
          console.error(errors);
          return;
        }
        var mutationErrors = response === null || response === void 0 || (_response$commentCrea = response.commentCreate) === null || _response$commentCrea === void 0 ? void 0 : _response$commentCrea.errors;
        setFormRelayErrors(form, mutationErrors);
        if (!(mutationErrors !== null && mutationErrors !== void 0 && mutationErrors.length)) {
          var _commentReply$comment;
          commentReply.resetCommentReply();
          form.reset();
          if ((_commentReply$comment = commentReply.commentItemRef) !== null && _commentReply$comment !== void 0 && _commentReply$comment.current) {
            commentReply.commentItemRef.current.scrollIntoView({
              block: "nearest",
              inline: "start",
              behavior: "smooth"
            });
          }
        }
      },
      // TODO: handle errors
      // eslint-disable-next-line no-console
      onError: console.error
    });
  };
  return /* @__PURE__ */ _jsx(SocialInput, _objectSpread({
    ref,
    placeholder: "Comment...",
    autoFocusInput,
    form,
    formId: "comment-create",
    submit: onSubmit,
    isLoading: isMutationInFlight,
    isReply,
    replyTargetName: commentReply.name,
    onCancelReply: commentReply.resetCommentReply,
    SubmitActionsProps: {
      ariaLabel: "create comment"
    }
  }, SocialInputProps));
});
var CommentCreate_default = CommentCreate;

export {
  CommentCreate_default
};
//# sourceMappingURL=chunk-ZABLFFWZ.mjs.map