import {
  MemberItemContainer,
  MemberPersonalInformation,
  Select
} from "./chunk-CRCMG66U.mjs";
import {
  useRemoveMemberMutation
} from "./chunk-PBI3V5RV.mjs";
import {
  MEMBER_ACTIONS,
  MEMBER_ROLES,
  MEMBER_STATUSES,
  roleOptions
} from "./chunk-2WMKN4WI.mjs";
import {
  capitalizeFirstLetter
} from "./chunk-TJOKWONA.mjs";
import {
  ProfileItemFragment
} from "./chunk-YTT7MYZX.mjs";
import {
  useChangeUserRoleMutation
} from "./chunk-HQUK3DGL.mjs";

// tmp-babel/profiles/web/ProfileMembers/MemberItem/index.js
import { useState } from "react";
import { useCurrentProfile } from "@baseapp-frontend/authentication";
import { AvatarWithPlaceholder } from "@baseapp-frontend/design-system/components/web/avatars";
import { ConfirmDialog } from "@baseapp-frontend/design-system/components/web/dialogs";
import { Box, Button, MenuItem, Typography, useTheme } from "@mui/material";
import { useFragment } from "react-relay";
import { jsx as _jsx, jsxs as _jsxs } 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 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 = useTheme();
  var memberProfile = useFragment(ProfileItemFragment, member);
  var _useCurrentProfile = useCurrentProfile(), currentProfile = _useCurrentProfile.currentProfile;
  var _useChangeUserRoleMut = useChangeUserRoleMutation(), _useChangeUserRoleMut2 = _slicedToArray(_useChangeUserRoleMut, 2), changeUserRole = _useChangeUserRoleMut2[0], isChangingUserRole = _useChangeUserRoleMut2[1];
  var _useRemoveMemberMutat = useRemoveMemberMutation(), _useRemoveMemberMutat2 = _slicedToArray(_useRemoveMemberMutat, 2), removeMember = _useRemoveMemberMutat2[0], isRemovingMember = _useRemoveMemberMutat2[1];
  var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), openConfirmChangeMember = _useState2[0], setOpenConfirmChangeMember = _useState2[1];
  var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), openConfirmRemoveMember = _useState4[0], setOpenConfirmRemoveMember = _useState4[1];
  if (!memberProfile) return null;
  var shouldRenderChangeRoleSelect = status === 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 === 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(MEMBER_ROLES.admin);
    }
    setOpenConfirmChangeMember(false);
  };
  var renderRoleButton = function renderRoleButton2() {
    if (shouldRenderChangeRoleSelect) {
      return /* @__PURE__ */ _jsx(Box, {
        children: /* @__PURE__ */ _jsx(Select, {
          value: memberRole,
          onChange: function onChange(event) {
            var value = event.target.value;
            if (value === MEMBER_ACTIONS.remove) {
              handleRemoveMemberDialog();
            } else {
              handleRoleChange(event);
            }
          },
          displayEmpty: true,
          variant: "filled",
          size: "small",
          disabled: isChangingUserRole || isRemovingMember,
          children: roleOptions.map(function(_ref2) {
            var value = _ref2.value, label = _ref2.label;
            return /* @__PURE__ */ _jsx(MenuItem, {
              value,
              sx: {
                color: value === MEMBER_ACTIONS.remove ? theme.palette.error.main : "inherit"
              },
              children: label
            }, value);
          })
        })
      });
    }
    if (haveMemberRoleAndStatus) {
      return /* @__PURE__ */ _jsx(Box, {
        children: /* @__PURE__ */ _jsx(Button, {
          variant: "outlined",
          color: "inherit",
          sx: {
            pointerEvents: "none"
          },
          children: status === MEMBER_STATUSES.active ? capitalizeFirstLetter(memberRole) : capitalizeFirstLetter(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__ */ _jsxs(MemberItemContainer, {
    children: [/* @__PURE__ */ _jsx(ConfirmDialog, {
      title: "Change user permissions?",
      open: openConfirmChangeMember,
      action: /* @__PURE__ */ _jsx(Button, {
        variant: "contained",
        color: "inherit",
        onClick: confirmChangeRole,
        children: "Confirm"
      }),
      onClose: cancelChangeRole,
      content: /* @__PURE__ */ _jsx(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__ */ _jsx(ConfirmDialog, {
      title: "Remove member",
      open: openConfirmRemoveMember,
      onClose: handleRemoveMemberDialog,
      content: /* @__PURE__ */ _jsx(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__ */ _jsx(Button, {
        variant: "contained",
        color: "error",
        onClick: confirmRemoveProfileMember,
        children: "Remove"
      })
    }), /* @__PURE__ */ _jsxs(MemberPersonalInformation, {
      isActive: status === MEMBER_STATUSES.active || false,
      children: [/* @__PURE__ */ _jsx(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__ */ _jsxs(Box, {
        children: [/* @__PURE__ */ _jsx(Typography, {
          variant: "subtitle2",
          children: memberProfile.name
        }), /* @__PURE__ */ _jsx(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;

export {
  MemberItem_default
};
//# sourceMappingURL=chunk-AYIMOPIY.mjs.map