import {
  MemberListItem_default
} from "./chunk-G5RJZM6V.mjs";
import {
  MemberItem_default
} from "./chunk-AYIMOPIY.mjs";
import {
  MEMBER_STATUSES,
  NUMBER_OF_MEMBERS_TO_LOAD_NEXT
} from "./chunk-2WMKN4WI.mjs";
import {
  ProfileItemFragment
} from "./chunk-YTT7MYZX.mjs";
import {
  UserMembersListFragment
} from "./chunk-SRL7QHIT.mjs";

// tmp-babel/profiles/web/ProfileMembers/MembersList/index.js
import { useMemo, useTransition } from "react";
import { LoadingState as DefaultLoadingState } from "@baseapp-frontend/design-system/components/web/displays";
import { Searchbar } from "@baseapp-frontend/design-system/components/web/inputs";
import { Box, Typography } from "@mui/material";
import { useForm } from "react-hook-form";
import { useFragment, usePaginationFragment } from "react-relay";
import { Virtuoso } from "react-virtuoso";
import { jsx as _jsx, Fragment as _Fragment, 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 MembersList = function MembersList2(_ref) {
  var _data$members2, _ownerProfile$name;
  var userRef = _ref.userRef, _ref$MemberItem = _ref.MemberItem, MemberItem = _ref$MemberItem === void 0 ? MemberItem_default : _ref$MemberItem, _ref$MemberItemProps = _ref.MemberItemProps, MemberItemProps = _ref$MemberItemProps === void 0 ? {} : _ref$MemberItemProps, _ref$LoadingState = _ref.LoadingState, LoadingState = _ref$LoadingState === void 0 ? DefaultLoadingState : _ref$LoadingState, _ref$LoadingStateProp = _ref.LoadingStateProps, LoadingStateProps = _ref$LoadingStateProp === void 0 ? {} : _ref$LoadingStateProp, _ref$membersContainer = _ref.membersContainerHeight, membersContainerHeight = _ref$membersContainer === void 0 ? 400 : _ref$membersContainer;
  var _useTransition = useTransition(), _useTransition2 = _slicedToArray(_useTransition, 2), isPending = _useTransition2[0], startTransition = _useTransition2[1];
  var _useForm = useForm({
    defaultValues: {
      search: ""
    }
  }), control = _useForm.control, reset = _useForm.reset, watch = _useForm.watch;
  var _usePaginationFragmen = usePaginationFragment(UserMembersListFragment, userRef), data = _usePaginationFragmen.data, loadNext = _usePaginationFragmen.loadNext, hasNext = _usePaginationFragmen.hasNext, isLoadingNext = _usePaginationFragmen.isLoadingNext, refetch = _usePaginationFragmen.refetch;
  var ownerProfile = useFragment(ProfileItemFragment, data);
  var handleSearch = function handleSearch2(value) {
    startTransition(function() {
      refetch({
        q: value
      });
    });
  };
  var handleSearchClear = function handleSearchClear2() {
    startTransition(function() {
      reset();
      handleSearch("");
    });
  };
  var members = useMemo(function() {
    var _data$members;
    return (data === null || data === void 0 || (_data$members = data.members) === null || _data$members === void 0 ? void 0 : _data$members.edges.filter(function(edge) {
      return edge === null || edge === void 0 ? void 0 : edge.node;
    }).map(function(edge) {
      return edge === null || edge === void 0 ? void 0 : edge.node;
    })) || [];
  }, [data === null || data === void 0 || (_data$members2 = data.members) === null || _data$members2 === void 0 ? void 0 : _data$members2.edges]);
  var isOwnerVisible = ownerProfile === null || ownerProfile === void 0 || (_ownerProfile$name = ownerProfile.name) === null || _ownerProfile$name === void 0 ? void 0 : _ownerProfile$name.includes(watch("search"));
  var resultsCount = isOwnerVisible ? members.length + 1 : members.length;
  var renderLoadingState = function renderLoadingState2() {
    if (!isLoadingNext) return /* @__PURE__ */ _jsx(Box, {
      sx: {
        paddingTop: 3
      }
    });
    return /* @__PURE__ */ _jsx(LoadingState, _objectSpread({
      sx: {
        paddingTop: 3,
        paddingBottom: 1
      },
      CircularProgressProps: {
        size: 15
      }
    }, LoadingStateProps));
  };
  var renderMemberItem = function renderMemberItem2(member, index) {
    return /* @__PURE__ */ _jsx(MemberListItem_default, {
      member,
      data,
      prevMember: members[index - 1],
      nextMember: members[index + 1],
      MemberItemComponent: MemberItem,
      memberItemComponentProps: MemberItemProps,
      searchQuery: watch("search")
    });
  };
  if (members.length === 0) {
    return /* @__PURE__ */ _jsxs(_Fragment, {
      children: [/* @__PURE__ */ _jsx(Searchbar, {
        variant: "outlined",
        size: "small",
        isPending,
        onChange: function onChange(e) {
          return handleSearch(e.target.value);
        },
        onClear: function onClear() {
          return handleSearchClear();
        },
        name: "search",
        control,
        sx: {
          mb: 4
        }
      }), /* @__PURE__ */ _jsx(Typography, {
        variant: "subtitle2",
        mb: 4,
        children: resultsCount === 1 ? "".concat(resultsCount, " member") : "".concat(resultsCount, " members")
      }), /* @__PURE__ */ _jsx(MemberItem, {
        member: data,
        memberRole: "owner",
        status: MEMBER_STATUSES.active,
        searchQuery: watch("search")
      })]
    });
  }
  return /* @__PURE__ */ _jsxs(_Fragment, {
    children: [/* @__PURE__ */ _jsx(Searchbar, {
      variant: "outlined",
      size: "small",
      isPending,
      onChange: function onChange(e) {
        return handleSearch(e.target.value);
      },
      onClear: function onClear() {
        return handleSearchClear();
      },
      name: "search",
      control,
      sx: {
        mb: 4
      }
    }), /* @__PURE__ */ _jsx(Typography, {
      variant: "subtitle2",
      mb: 4,
      children: resultsCount === 1 ? "".concat(resultsCount, " member") : "".concat(resultsCount, " members")
    }), /* @__PURE__ */ _jsx(Virtuoso, {
      style: {
        height: membersContainerHeight
      },
      data: members,
      itemContent: function itemContent(_index, member) {
        return member && renderMemberItem(member, _index);
      },
      components: {
        Footer: renderLoadingState
      },
      endReached: function endReached() {
        if (hasNext) {
          loadNext(NUMBER_OF_MEMBERS_TO_LOAD_NEXT);
        }
      }
    })]
  });
};
var MembersList_default = MembersList;

export {
  MembersList_default
};
//# sourceMappingURL=chunk-3JYSN6ZG.mjs.map