import {
  BLOCK_UNBLOCK_DIALOG_TEXTS
} from "./chunk-2TRHFMGU.mjs";
import {
  ActionButton
} from "./chunk-PI4QS43C.mjs";
import {
  BlockToggleMutationQuery
} from "./chunk-CAEQSMEK.mjs";
import {
  BlockToggleFragment
} from "./chunk-VAF3AVMV.mjs";

// tmp-babel/profiles/web/ProfileComponent/BlockButtonWithDialog/index.js
import { useState } from "react";
import { ConfirmDialog } from "@baseapp-frontend/design-system/components/web/dialogs";
import { BlockIcon, UnblockIcon } from "@baseapp-frontend/design-system/components/web/icons";
import { useNotification } from "@baseapp-frontend/utils";
import { Button, CircularProgress, MenuItem, Typography } from "@mui/material";
import { useFragment, useMutation } from "react-relay";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } 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 BlockButtonWithDialog = function BlockButtonWithDialog2(_ref) {
  var targetRef = _ref.target, isMenu = _ref.isMenu, handleError = _ref.handleError, handleCloseMenu = _ref.handleCloseMenu, currentProfileId = _ref.currentProfileId;
  var target = useFragment(BlockToggleFragment, targetRef);
  var _useMutation = useMutation(BlockToggleMutationQuery), _useMutation2 = _slicedToArray(_useMutation, 2), commitMutation = _useMutation2[0], isMutationInFlight = _useMutation2[1];
  var _useNotification = useNotification(), sendToast = _useNotification.sendToast;
  var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), open = _useState2[0], setOpen = _useState2[1];
  var isBlockedByMe = target === null || target === void 0 ? void 0 : target.isBlockedByMe;
  var handleOpen = function handleOpen2() {
    setOpen(true);
  };
  var handleClose = function handleClose2() {
    setOpen(false);
  };
  var handleSuccess = function handleSuccess2() {
    setOpen(false);
    handleCloseMenu === null || handleCloseMenu === void 0 || handleCloseMenu();
  };
  var handleBlock = function handleBlock2() {
    if (isMutationInFlight || !currentProfileId || !target) {
      return;
    }
    commitMutation({
      variables: {
        input: {
          targetObjectId: target.id,
          actorObjectId: currentProfileId
        }
      },
      onCompleted: function onCompleted(response, errors) {
        var _target$name, _response$blockToggle;
        errors === null || errors === void 0 || errors.forEach(function(error) {
          sendToast(error.message, {
            type: "error"
          });
        });
        handleSuccess();
        sendToast("".concat((_target$name = target.name) !== null && _target$name !== void 0 ? _target$name : "", " is ").concat(response !== null && response !== void 0 && (_response$blockToggle = response.blockToggle) !== null && _response$blockToggle !== void 0 && (_response$blockToggle = _response$blockToggle.target) !== null && _response$blockToggle !== void 0 && _response$blockToggle.isBlockedByMe ? "blocked" : "unblocked"), {
          type: "info"
        });
      },
      onError: function onError() {
        handleError === null || handleError === void 0 || handleError();
      }
    });
  };
  return /* @__PURE__ */ _jsxs(_Fragment, {
    children: [isMenu ? /* @__PURE__ */ _jsx(MenuItem, {
      onClick: handleOpen,
      disableRipple: true,
      children: /* @__PURE__ */ _jsx(Typography, {
        variant: "body2",
        color: "error.main",
        noWrap: true,
        children: isBlockedByMe ? /* @__PURE__ */ _jsxs(_Fragment, {
          children: [/* @__PURE__ */ _jsx(UnblockIcon, {
            sx: {
              color: "error.main",
              marginRight: "5px"
            }
          }), "Unblock profile"]
        }) : /* @__PURE__ */ _jsxs(_Fragment, {
          children: [/* @__PURE__ */ _jsx(BlockIcon, {
            sx: {
              color: "error.main",
              marginRight: "5px"
            }
          }), "Block profile"]
        })
      })
    }) : /* @__PURE__ */ _jsx(Button, {
      variant: "contained",
      onClick: handleOpen,
      sx: {
        justifyContent: "center"
      },
      size: "medium",
      children: /* @__PURE__ */ _jsx(Typography, {
        variant: "body2",
        color: "inherit",
        noWrap: true,
        children: isBlockedByMe ? /* @__PURE__ */ _jsxs(_Fragment, {
          children: [/* @__PURE__ */ _jsx(UnblockIcon, {
            sx: {
              color: "inherit",
              marginRight: "5px"
            }
          }), "Unblock"]
        }) : /* @__PURE__ */ _jsxs(_Fragment, {
          children: [/* @__PURE__ */ _jsx(BlockIcon, {
            sx: {
              color: "error.main",
              marginRight: "5px"
            }
          }), "Block profile"]
        })
      })
    }), /* @__PURE__ */ _jsx(ConfirmDialog, {
      open,
      title: /* @__PURE__ */ _jsxs("div", {
        className: "grid grid-cols-[min-content_1fr] gap-4 text-text-primary responsive-h6",
        children: [isBlockedByMe ? /* @__PURE__ */ _jsx(UnblockIcon, {}) : /* @__PURE__ */ _jsx(BlockIcon, {}), "".concat(isBlockedByMe ? BLOCK_UNBLOCK_DIALOG_TEXTS.unblock.title : BLOCK_UNBLOCK_DIALOG_TEXTS.block.title, " ").concat(target.name, "?")]
      }),
      content: isBlockedByMe ? BLOCK_UNBLOCK_DIALOG_TEXTS.unblock.content : BLOCK_UNBLOCK_DIALOG_TEXTS.block.content,
      onClose: handleClose,
      action: /* @__PURE__ */ _jsxs(ActionButton, {
        onClick: handleBlock,
        isBlocked: isBlockedByMe,
        disabled: isMutationInFlight,
        children: [isBlockedByMe ? BLOCK_UNBLOCK_DIALOG_TEXTS.unblock.action : BLOCK_UNBLOCK_DIALOG_TEXTS.block.action, isMutationInFlight && /* @__PURE__ */ _jsx(CircularProgress, {
          size: 16,
          sx: {
            marginLeft: "5px"
          }
        })]
      })
    })]
  });
};
var BlockButtonWithDialog_default = BlockButtonWithDialog;

export {
  BlockButtonWithDialog_default
};
//# sourceMappingURL=chunk-IQVE7KZK.mjs.map