import {
  FollowToggleMutationQuery
} from "./chunk-TCVIUOGH.mjs";
import {
  FollowToggleUpdatableFragment
} from "./chunk-EGZ6ZTPM.mjs";

// tmp-babel/profiles/web/ProfileComponent/FollowToggleButton/index.js
import { Iconify } from "@baseapp-frontend/design-system/components/web/images";
import { useNotification } from "@baseapp-frontend/utils";
import { Button } from "@mui/material";
import { useMutation, useRelayEnvironment } from "react-relay";
import { commitLocalUpdate } from "relay-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
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 FollowToggleButton = function FollowToggleButton2(_ref) {
  var targetId = _ref.targetId, isFollowedByMe = _ref.isFollowedByMe, currentProfileId = _ref.currentProfileId, profileRef = _ref.profileRef;
  var _useMutation = useMutation(FollowToggleMutationQuery), _useMutation2 = _slicedToArray(_useMutation, 2), commitMutation = _useMutation2[0], isMutationInFlight = _useMutation2[1];
  var _useNotification = useNotification(), sendToast = _useNotification.sendToast;
  var environment = useRelayEnvironment();
  var toggleFollow = function toggleFollow2() {
    if (isMutationInFlight || !currentProfileId || !targetId) {
      return;
    }
    commitMutation({
      variables: {
        input: {
          targetObjectId: targetId,
          actorObjectId: currentProfileId
        }
      },
      onCompleted: function onCompleted(response, errors) {
        var _response$followToggl;
        errors === null || errors === void 0 || errors.forEach(function(error) {
          sendToast(error.message, {
            type: "error"
          });
        });
        if (errors) {
          return;
        }
        var follow = (_response$followToggl = response.followToggle) === null || _response$followToggl === void 0 ? void 0 : _response$followToggl.follow;
        commitLocalUpdate(environment, function(store) {
          var _follow$node$target$i, _follow$node, _follow$node$target$f, _follow$node2;
          if (profileRef == null) {
            return;
          }
          var _store$readUpdatableF = store.readUpdatableFragment(FollowToggleUpdatableFragment, profileRef), updatableData = _store$readUpdatableF.updatableData;
          var updatedIsFollowedByMe = (_follow$node$target$i = follow === null || follow === void 0 || (_follow$node = follow.node) === null || _follow$node === void 0 || (_follow$node = _follow$node.target) === null || _follow$node === void 0 ? void 0 : _follow$node.isFollowedByMe) !== null && _follow$node$target$i !== void 0 ? _follow$node$target$i : false;
          var updatedFollowersCount = (_follow$node$target$f = follow === null || follow === void 0 || (_follow$node2 = follow.node) === null || _follow$node2 === void 0 || (_follow$node2 = _follow$node2.target) === null || _follow$node2 === void 0 ? void 0 : _follow$node2.followersCount) !== null && _follow$node$target$f !== void 0 ? _follow$node$target$f : updatableData.followersCount - 1;
          updatableData.isFollowedByMe = updatedIsFollowedByMe;
          updatableData.followersCount = updatedFollowersCount;
        });
      }
    });
  };
  return /* @__PURE__ */ _jsx(Button, {
    onClick: toggleFollow,
    startIcon: isFollowedByMe ? /* @__PURE__ */ _jsx(Iconify, {
      icon: "ci:check-all"
    }) : /* @__PURE__ */ _jsx(Iconify, {
      icon: "mingcute:add-line"
    }),
    variant: isFollowedByMe ? "soft" : "contained",
    color: isFollowedByMe ? "inherit" : "primary",
    disabled: isMutationInFlight,
    size: "medium",
    children: isFollowedByMe ? "Following" : "Follow"
  });
};
var FollowToggleButton_default = FollowToggleButton;

export {
  FollowToggleButton_default
};
//# sourceMappingURL=chunk-OS5AHZX3.mjs.map