import {
  LoadingState_default
} from "./chunk-GWBWJXK6.mjs";
import {
  ProfileMenuItem_default
} from "./chunk-54XLWLYF.mjs";
import {
  EmptyState_default
} from "./chunk-JZ5V5KP3.mjs";
import {
  CancelMenuItem,
  StyledList
} from "./chunk-FVZR576Y.mjs";
import {
  ProfilesListQuery
} from "./chunk-5GFBJV4K.mjs";
import {
  ProfilesListFragment
} from "./chunk-WRNIPYHZ.mjs";

// tmp-babel/profiles/web/profile-popover/ProfilesList/index.js
import { Suspense, useMemo } from "react";
import { useCurrentProfile } from "@baseapp-frontend/authentication";
import { ChevronIcon } from "@baseapp-frontend/design-system/components/web/icons";
import { useNotification } from "@baseapp-frontend/utils";
import { Box, ButtonBase, Divider, Slide } from "@mui/material";
import { useLazyLoadQuery, usePaginationFragment } from "react-relay";
import { Virtuoso } from "react-virtuoso";
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);
}
var ProfilesList = function ProfilesList2(_ref) {
  var _data$profiles2;
  var handleCloseSubmenu = _ref.handleCloseSubmenu, MenuItemProps = _ref.MenuItemProps, _ref$LoadingStateProp = _ref.LoadingStateProps, LoadingStateProps = _ref$LoadingStateProp === void 0 ? {} : _ref$LoadingStateProp, _ref$listMaxHeight = _ref.listMaxHeight, listMaxHeight = _ref$listMaxHeight === void 0 ? 300 : _ref$listMaxHeight;
  var options = {
    count: 10
  };
  var _useLazyLoadQuery = useLazyLoadQuery(ProfilesListQuery, options, {
    fetchPolicy: "store-and-network"
  }), me = _useLazyLoadQuery.me;
  var _useNotification = useNotification(), sendToast = _useNotification.sendToast;
  var _useCurrentProfile = useCurrentProfile(), currentProfile = _useCurrentProfile.currentProfile, setCurrentProfile = _useCurrentProfile.setCurrentProfile;
  var handleProfileChange = function handleProfileChange2(profile) {
    if ((currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id) !== profile.id) {
      var _process$env$NEXT_PUB, _profile$name, _profile$urlPath$path, _profile$urlPath;
      var baseUrl = (_process$env$NEXT_PUB = process.env.NEXT_PUBLIC_API_BASE_URL) === null || _process$env$NEXT_PUB === void 0 ? void 0 : _process$env$NEXT_PUB.replace("/v1", "");
      var absoluteImagePath = null;
      if (profile.image) {
        absoluteImagePath = profile.image.url.startsWith("http") ? profile.image.url : "".concat(baseUrl).concat(profile.image.url);
      }
      setCurrentProfile({
        id: profile.id,
        name: (_profile$name = profile.name) !== null && _profile$name !== void 0 ? _profile$name : null,
        image: absoluteImagePath,
        urlPath: (_profile$urlPath$path = (_profile$urlPath = profile.urlPath) === null || _profile$urlPath === void 0 ? void 0 : _profile$urlPath.path) !== null && _profile$urlPath$path !== void 0 ? _profile$urlPath$path : null
      });
      sendToast("Switched to ".concat(profile.name));
      handleCloseSubmenu();
    }
  };
  var _usePaginationFragmen = usePaginationFragment(ProfilesListFragment, me), data = _usePaginationFragmen.data, loadNext = _usePaginationFragmen.loadNext, isLoadingNext = _usePaginationFragmen.isLoadingNext, hasNext = _usePaginationFragmen.hasNext;
  var profiles = useMemo(function() {
    var _data$profiles;
    return (data === null || data === void 0 || (_data$profiles = data.profiles) === null || _data$profiles === void 0 ? void 0 : _data$profiles.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$profiles2 = data.profiles) === null || _data$profiles2 === void 0 ? void 0 : _data$profiles2.edges]);
  var renderVirtuosoLoadingState = function renderVirtuosoLoadingState2() {
    if (!isLoadingNext) return /* @__PURE__ */ _jsx(Box, {
      sx: {
        paddingTop: 3
      }
    });
    return /* @__PURE__ */ _jsx(LoadingState_default, _objectSpread({
      sx: {
        paddingTop: 3
      },
      CircularProgressProps: {
        size: 15
      }
    }, LoadingStateProps));
  };
  var renderProfileItem = function renderProfileItem2(profile, index) {
    if (!profile) return null;
    return /* @__PURE__ */ _jsx(ProfileMenuItem_default, _objectSpread({
      profileRef: profile,
      onProfileChange: handleProfileChange
    }, MenuItemProps), "profile-".concat(index));
  };
  var renderContent = function renderContent2() {
    if (profiles.length === 0) return /* @__PURE__ */ _jsx(EmptyState_default, {});
    var minHeight = profiles.length * 56 > listMaxHeight ? listMaxHeight : profiles.length * 56;
    return /* @__PURE__ */ _jsx(Box, {
      sx: {
        backgroundColor: "common.white"
      },
      children: /* @__PURE__ */ _jsx(Virtuoso, {
        data: profiles,
        style: {
          height: "auto",
          minHeight
        },
        itemContent: function itemContent(index, profile) {
          return renderProfileItem(profile, index);
        },
        components: {
          Footer: renderVirtuosoLoadingState
        },
        endReached: function endReached() {
          if (hasNext) {
            loadNext(10);
          }
        }
      })
    });
  };
  return /* @__PURE__ */ _jsx(Box, {
    children: /* @__PURE__ */ _jsx(StyledList, {
      disablePadding: true,
      maxHeight: listMaxHeight,
      "aria-label": "List of available profiles",
      children: renderContent()
    })
  });
};
var ProfilesListSuspended = function ProfilesListSuspended2(props) {
  var openSubmenu = props.openSubmenu, handleCloseSubmenu = props.handleCloseSubmenu, _props$cancelLabel = props.cancelLabel, cancelLabel = _props$cancelLabel === void 0 ? "Cancel" : _props$cancelLabel;
  return /* @__PURE__ */ _jsx(Slide, {
    direction: openSubmenu ? "left" : "right",
    "in": openSubmenu,
    mountOnEnter: true,
    unmountOnExit: true,
    children: /* @__PURE__ */ _jsxs(Box, {
      children: [/* @__PURE__ */ _jsx(Box, {
        sx: {
          p: 1
        },
        children: /* @__PURE__ */ _jsxs(CancelMenuItem, {
          tabIndex: 0,
          component: ButtonBase,
          onClick: function onClick() {
            return handleCloseSubmenu();
          },
          children: [/* @__PURE__ */ _jsx(ChevronIcon, {
            position: "left",
            color: "action"
          }), cancelLabel]
        })
      }), /* @__PURE__ */ _jsx(Divider, {
        sx: {
          borderStyle: "solid"
        }
      }), /* @__PURE__ */ _jsx(Box, {
        sx: {
          p: 1
        },
        children: /* @__PURE__ */ _jsx(Suspense, {
          fallback: /* @__PURE__ */ _jsx(LoadingState_default, {}),
          children: /* @__PURE__ */ _jsx(ProfilesList, _objectSpread({}, props))
        })
      })]
    })
  });
};
var ProfilesList_default = ProfilesListSuspended;

export {
  ProfilesList_default
};
//# sourceMappingURL=chunk-4M7SXVJD.mjs.map