"use strict";Object.defineProperty(exports, "__esModule", {value: true});



var _chunkU4562NBVjs = require('./chunk-U4562NBV.js');


var _chunkLSBPNAMRjs = require('./chunk-LSBPNAMR.js');





var _chunkA7WNUADFjs = require('./chunk-A7WNUADF.js');


var _chunk7YVPJB5Vjs = require('./chunk-7YVPJB5V.js');


var _chunk5V5EREDJjs = require('./chunk-5V5EREDJ.js');


var _chunkLV34ENL5js = require('./chunk-LV34ENL5.js');

// tmp-babel/profiles/web/ProfileMembers/MemberItem/index.js
var _react = require('react');
var _authentication = require('@baseapp-frontend/authentication');
var _avatars = require('@baseapp-frontend/design-system/components/web/avatars');
var _dialogs = require('@baseapp-frontend/design-system/components/web/dialogs');
var _material = require('@mui/material');
var _reactrelay = require('react-relay');
var _jsxruntime = require('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 MemberItem = function MemberItem2(_ref) {
  var _memberProfile$name, _memberProfile$image$, _memberProfile$image, _memberProfile$urlPat;
  var member = _ref.member, memberRole = _ref.memberRole, status = _ref.status, _ref$avatarProps = _ref.avatarProps, avatarProps = _ref$avatarProps === void 0 ? {} : _ref$avatarProps, _ref$avatarWidth = _ref.avatarWidth, avatarWidth = _ref$avatarWidth === void 0 ? 40 : _ref$avatarWidth, _ref$avatarHeight = _ref.avatarHeight, avatarHeight = _ref$avatarHeight === void 0 ? 40 : _ref$avatarHeight, _ref$canChangeMember = _ref.canChangeMember, canChangeMember = _ref$canChangeMember === void 0 ? false : _ref$canChangeMember, userId = _ref.userId, searchQuery = _ref.searchQuery;
  var theme = _material.useTheme.call(void 0, );
  var memberProfile = _reactrelay.useFragment.call(void 0, _chunk5V5EREDJjs.ProfileItemFragment, member);
  var _useCurrentProfile = _authentication.useCurrentProfile.call(void 0, ), currentProfile = _useCurrentProfile.currentProfile;
  var _useChangeUserRoleMut = _chunkLV34ENL5js.useChangeUserRoleMutation.call(void 0, ), _useChangeUserRoleMut2 = _slicedToArray(_useChangeUserRoleMut, 2), changeUserRole = _useChangeUserRoleMut2[0], isChangingUserRole = _useChangeUserRoleMut2[1];
  var _useRemoveMemberMutat = _chunkLSBPNAMRjs.useRemoveMemberMutation.call(void 0, ), _useRemoveMemberMutat2 = _slicedToArray(_useRemoveMemberMutat, 2), removeMember = _useRemoveMemberMutat2[0], isRemovingMember = _useRemoveMemberMutat2[1];
  var _useState = _react.useState.call(void 0, false), _useState2 = _slicedToArray(_useState, 2), openConfirmChangeMember = _useState2[0], setOpenConfirmChangeMember = _useState2[1];
  var _useState3 = _react.useState.call(void 0, false), _useState4 = _slicedToArray(_useState3, 2), openConfirmRemoveMember = _useState4[0], setOpenConfirmRemoveMember = _useState4[1];
  if (!memberProfile) return null;
  var shouldRenderChangeRoleSelect = status === _chunkA7WNUADFjs.MEMBER_STATUSES.active && memberRole !== "owner" && canChangeMember;
  var haveMemberRoleAndStatus = memberRole && status;
  var removeProfileMember = function removeProfileMember2() {
    if (currentProfile !== null && currentProfile !== void 0 && currentProfile.id && userId) {
      removeMember({
        variables: {
          input: {
            profileId: currentProfile.id,
            userId
          }
        }
      });
    }
  };
  var confirmRemoveProfileMember = function confirmRemoveProfileMember2() {
    if (currentProfile !== null && currentProfile !== void 0 && currentProfile.id && userId) {
      removeProfileMember();
    }
    setOpenConfirmRemoveMember(false);
  };
  var handleRemoveMemberDialog = function handleRemoveMemberDialog2() {
    setOpenConfirmRemoveMember(!openConfirmRemoveMember);
  };
  var changeRole = function changeRole2(roleType) {
    if (currentProfile !== null && currentProfile !== void 0 && currentProfile.id && userId) {
      changeUserRole({
        variables: {
          input: {
            profileId: currentProfile.id,
            userId,
            roleType
          }
        }
      });
    }
  };
  var handleRoleChange = function handleRoleChange2(event) {
    if (event.target.value === _chunkA7WNUADFjs.MEMBER_ROLES.admin) {
      setOpenConfirmChangeMember(true);
      return;
    }
    if (currentProfile !== null && currentProfile !== void 0 && currentProfile.id && userId) {
      changeRole(event.target.value);
    }
  };
  var cancelChangeRole = function cancelChangeRole2() {
    setOpenConfirmChangeMember(false);
  };
  var confirmChangeRole = function confirmChangeRole2() {
    if (currentProfile !== null && currentProfile !== void 0 && currentProfile.id && userId) {
      changeRole(_chunkA7WNUADFjs.MEMBER_ROLES.admin);
    }
    setOpenConfirmChangeMember(false);
  };
  var renderRoleButton = function renderRoleButton2() {
    if (shouldRenderChangeRoleSelect) {
      return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Box, {
        children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkU4562NBVjs.Select, {
          value: memberRole,
          onChange: function onChange(event) {
            var value = event.target.value;
            if (value === _chunkA7WNUADFjs.MEMBER_ACTIONS.remove) {
              handleRemoveMemberDialog();
            } else {
              handleRoleChange(event);
            }
          },
          displayEmpty: true,
          variant: "filled",
          size: "small",
          disabled: isChangingUserRole || isRemovingMember,
          children: _chunkA7WNUADFjs.roleOptions.map(function(_ref2) {
            var value = _ref2.value, label = _ref2.label;
            return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.MenuItem, {
              value,
              sx: {
                color: value === _chunkA7WNUADFjs.MEMBER_ACTIONS.remove ? theme.palette.error.main : "inherit"
              },
              children: label
            }, value);
          })
        })
      });
    }
    if (haveMemberRoleAndStatus) {
      return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Box, {
        children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Button, {
          variant: "outlined",
          color: "inherit",
          sx: {
            pointerEvents: "none"
          },
          children: status === _chunkA7WNUADFjs.MEMBER_STATUSES.active ? _chunk7YVPJB5Vjs.capitalizeFirstLetter.call(void 0, memberRole) : _chunk7YVPJB5Vjs.capitalizeFirstLetter.call(void 0, status)
        })
      });
    }
    return null;
  };
  if (searchQuery && !(memberProfile !== null && memberProfile !== void 0 && (_memberProfile$name = memberProfile.name) !== null && _memberProfile$name !== void 0 && _memberProfile$name.toLowerCase().includes(searchQuery.toLowerCase()))) {
    return null;
  }
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkU4562NBVjs.MemberItemContainer, {
    children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _dialogs.ConfirmDialog, {
      title: "Change user permissions?",
      open: openConfirmChangeMember,
      action: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Button, {
        variant: "contained",
        color: "inherit",
        onClick: confirmChangeRole,
        children: "Confirm"
      }),
      onClose: cancelChangeRole,
      content: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
        variant: "body1",
        children: "Are you sure you want to promote this member to an admin? They will have full administrative rights, including the ability to manage members and settings."
      }),
      cancelText: "Back"
    }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _dialogs.ConfirmDialog, {
      title: "Remove member",
      open: openConfirmRemoveMember,
      onClose: handleRemoveMemberDialog,
      content: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
        variant: "body1",
        children: "Are you sure you want to remove this member? This action will revoke their access to the organization profile."
      }),
      cancelText: "Back",
      action: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Button, {
        variant: "contained",
        color: "error",
        onClick: confirmRemoveProfileMember,
        children: "Remove"
      })
    }), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkU4562NBVjs.MemberPersonalInformation, {
      isActive: status === _chunkA7WNUADFjs.MEMBER_STATUSES.active || false,
      children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _avatars.AvatarWithPlaceholder, _objectSpread({
        width: avatarWidth,
        height: avatarHeight,
        src: (_memberProfile$image$ = memberProfile === null || memberProfile === void 0 || (_memberProfile$image = memberProfile.image) === null || _memberProfile$image === void 0 ? void 0 : _memberProfile$image.url) !== null && _memberProfile$image$ !== void 0 ? _memberProfile$image$ : "",
        alt: "Profile avatar",
        color: "secondary"
      }, avatarProps)), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
        children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
          variant: "subtitle2",
          children: memberProfile.name
        }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
          variant: "caption",
          children: memberProfile === null || memberProfile === void 0 || (_memberProfile$urlPat = memberProfile.urlPath) === null || _memberProfile$urlPat === void 0 ? void 0 : _memberProfile$urlPat.path
        })]
      })]
    }), renderRoleButton()]
  });
};
var MemberItem_default = MemberItem;



exports.MemberItem_default = MemberItem_default;
//# sourceMappingURL=chunk-2Q6OZLQM.js.map