import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { Fragment } from 'react';
import { SidebarMenuSubItem, SidebarMenuSubButton, SidebarGroup, SidebarMenuButton, SidebarGroupContent, SidebarMenu, SidebarMenuItem, SidebarMenuSub } from '@bigbinary/neeto-atoms/primitives';
import { isNotEmpty, isPresent, hyphenate } from '@bigbinary/neeto-cist';
import { hyphenize } from '@bigbinary/neeto-commons-frontend/v2/utils/general';
import { curry, is, includes, __, all, equals, omit } from 'ramda';
import { useNavigationCheckpoints } from '@bigbinary/neeto-commons-frontend/v2/react-utils/useRegisterNavigationCheckpoint';
import { NavLink, useLocation } from 'react-router-dom';
import { jsx, jsxs } from 'react/jsx-runtime';
import { Badge } from '@bigbinary/neeto-atoms';
import classnames from 'classnames';
import Segments from '@bigbinary/neeto-filters-frontend/v2/Segments';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { C as ChevronLeft } from './chevron-left-BTVCxPw8.js';

var _excluded$1 = ["checkpointKey", "to", "href", "children"];
function ownKeys$2(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var CheckPointNavLinks = function CheckPointNavLinks(_ref) {
  var checkpointKey = _ref.checkpointKey,
    to = _ref.to,
    href = _ref.href,
    children = _ref.children,
    others = _objectWithoutProperties(_ref, _excluded$1);
  var _useNavigationCheckpo = useNavigationCheckpoints(checkpointKey),
    checkpoint = _useNavigationCheckpo[checkpointKey];
  var LinkElement = href ? "a" : NavLink;
  var linkProps = href ? _objectSpread$2({
    href: href,
    key: href
  }, others) : _objectSpread$2({
    to: checkpoint || to,
    key: to
  }, others);
  return /*#__PURE__*/jsx(LinkElement, _objectSpread$2(_objectSpread$2({}, linkProps), {}, {
    children: children
  }));
};

var SUB_LINK_TYPES = {
  SYSTEM_VIEW: "system_view",
  SEGMENTS: "segments"
};
var SELECTED_NAV_LINK_ROUTE_STORAGE_KEY = "selectedNavLinkRoute";
var DEFAULT_HOME_PATH = "/admin";
var SIDEBAR_WIDTH_KEY = "neeto-molecules-sidebar-width";
var MIN_SIDEBAR_WIDTH = 12.5 * 16;
var MAX_SIDEBAR_WIDTH = 20 * 16;
var DEFAULT_SIDEBAR_WIDTH = 15 * 16;
var PINNED_MORE_NAV_LINKS_KEY = "pinnedMoreNavLinks";
var SPECIAL_APP_NAMES = {
  bugwatch: "BugWatch"
};

var isSubRouteActive = function isSubRouteActive(subRoute, location) {
  var currentBrowserUrl = new URL(location.pathname + location.search + location.hash, window.location.origin);
  var targetUrl = new URL(subRoute, window.location.origin);
  var targetSearchParams = targetUrl.searchParams;
  var targetSearchKeys = Array.from(targetSearchParams.keys());
  return all(function (key) {
    return currentBrowserUrl.searchParams.get(key) === targetSearchParams.get(key);
  }, targetSearchKeys) && equals(currentBrowserUrl.pathname, targetUrl.pathname);
};
var getSidebarStateLocalStorageKey = function getSidebarStateLocalStorageKey() {
  var _globalProps$user, _globalProps$user2;
  var user = ((_globalProps$user = globalProps.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.email) || ((_globalProps$user2 = globalProps.user) === null || _globalProps$user2 === void 0 ? void 0 : _globalProps$user2.phoneNumber);
  return "sidebarState-".concat(user);
};
var filterByPermissions = curry(function (_ref) {
  var permissions = _ref.permissions;
  if (permissions && isNotEmpty(permissions)) {
    return is(Array, permissions) ? permissions.some(includes(__, globalProps.permissions)) : globalProps.permissions.includes(permissions);
  }
  return true;
});
var getActiveConfigurePageLink = function getActiveConfigurePageLink(_ref2) {
  var navLinks = _ref2.navLinks,
    location = _ref2.location;
  return navLinks.find(function (link) {
    var _link$to;
    var _ref3 = ((_link$to = link.to) === null || _link$to === void 0 ? void 0 : _link$to.split("?")) || [],
      _ref4 = _slicedToArray(_ref3, 1),
      linkPathname = _ref4[0];
    return isPresent(link.isConfigureNavLink) && link.isConfigureNavLink && location.pathname.startsWith(linkPathname);
  });
};

function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var SubLinkItem = function SubLinkItem(_ref) {
  var to = _ref.to,
    href = _ref.href,
    onClick = _ref.onClick,
    label = _ref.label,
    className = _ref.className,
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? SUB_LINK_TYPES.SYSTEM_VIEW : _ref$type,
    count = _ref.count,
    isSectionHeader = _ref.isSectionHeader,
    isCountsLoading = _ref.isCountsLoading,
    dataTestid = _ref["data-testid"],
    _ref$entity = _ref.entity,
    entity = _ref$entity === void 0 ? "" : _ref$entity,
    _ref$columns = _ref.columns,
    columns = _ref$columns === void 0 ? [] : _ref$columns,
    baseUrl = _ref.baseUrl,
    isActive = _ref.isActive,
    tag = _ref.tag;
  var location = useLocation();
  if (type === SUB_LINK_TYPES.SEGMENTS) {
    return /*#__PURE__*/jsx(Segments, {
      baseUrl: baseUrl,
      columns: columns,
      entity: entity,
      isIndependent: false
    });
  }
  var dataTestidPrefix = dataTestid || hyphenate(label);
  var renderCount = function renderCount(count) {
    return count > 999 ? "999+" : count;
  };
  var isSubLinkActive = function isSubLinkActive() {
    return !isSectionHeader && (is(Function, isActive) ? isActive() : isSubRouteActive(to, location));
  };
  return /*#__PURE__*/jsx(SidebarMenuSubItem, {
    children: /*#__PURE__*/jsx(SidebarMenuSubButton, {
      asChild: true,
      isActive: isSubLinkActive(),
      size: "md",
      className: classnames("h-auto overflow-visible py-1.5 whitespace-normal", className),
      children: /*#__PURE__*/jsxs(CheckPointNavLinks, {
        href: href,
        onClick: onClick,
        to: to,
        activeClassName: "active",
        "data-testid": "".concat(dataTestidPrefix, "-sub-link"),
        isActive: isSubLinkActive,
        children: [/*#__PURE__*/jsxs("span", {
          className: "flex flex-grow items-center gap-2",
          "data-testid": "".concat(dataTestidPrefix, "-sub-link-label"),
          children: [label, tag && /*#__PURE__*/jsx(Badge, _objectSpread$1({}, tag))]
        }), /*#__PURE__*/jsx("span", {
          className: "text-sidebar-foreground/70 flex-shrink-0 text-xs",
          "data-testid": "".concat(dataTestidPrefix, "-sub-link-count"),
          children: isCountsLoading ? /*#__PURE__*/jsx("div", {
            className: "bg-sidebar-accent h-4 w-4 animate-pulse rounded"
          }) : renderCount(count)
        })]
      })
    })
  });
};

var _excluded = ["label", "items"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).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 (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var ConfigureView = function ConfigureView(_ref) {
  var _ref$navLink = _ref.navLink,
    navLink = _ref$navLink === void 0 ? {
      label: "",
      items: [],
      isConfigureNavLink: false
    } : _ref$navLink,
    handleGoBack = _ref.handleGoBack;
    _ref.isConfigureSidebar;
  var label = navLink.label,
    items = navLink.items,
    otherProps = _objectWithoutProperties(navLink, _excluded);
  var renderItems = function renderItems(itemList) {
    return /*#__PURE__*/jsx(SidebarMenu, {
      className: "gap-1",
      children: itemList.filter(filterByPermissions).map(function (subItem, subIndex) {
        var _subItem$to, _subItem$to2;
        return /*#__PURE__*/jsx(Fragment, {
          children: isPresent(subItem.items) ? /*#__PURE__*/jsxs(SidebarMenuItem, {
            children: [/*#__PURE__*/jsx(SidebarMenuButton, {
              asChild: true,
              className: "font-medium",
              children: /*#__PURE__*/jsx(CheckPointNavLinks, {
                href: subItem.href,
                to: (_subItem$to = subItem.to) !== null && _subItem$to !== void 0 ? _subItem$to : subItem.path,
                onClick: subItem.onClick,
                children: subItem.label
              })
            }), /*#__PURE__*/jsx(SidebarMenuSub, {
              children: subItem.items.filter(filterByPermissions).map(function (nestedItem, nestedIndex) {
                var _nestedItem$to;
                return /*#__PURE__*/jsx(SubLinkItem, _objectSpread(_objectSpread({}, _objectSpread({}, nestedItem)), {}, {
                  href: nestedItem.href,
                  isActive: nestedItem.isActive,
                  to: (_nestedItem$to = nestedItem.to) !== null && _nestedItem$to !== void 0 ? _nestedItem$to : nestedItem.path,
                  onClick: nestedItem.onClick
                }), nestedIndex);
              })
            })]
          }) : /*#__PURE__*/jsx(SubLinkItem, _objectSpread(_objectSpread({}, _objectSpread({}, subItem)), {}, {
            href: subItem.href,
            isActive: subItem.isActive,
            to: (_subItem$to2 = subItem.to) !== null && _subItem$to2 !== void 0 ? _subItem$to2 : subItem.path,
            onClick: subItem.onClick
          }))
        }, subIndex);
      })
    });
  };
  return /*#__PURE__*/jsxs(SidebarGroup, {
    "data-testid": "configure-nav-container",
    children: [/*#__PURE__*/jsxs(SidebarMenuButton, _objectSpread(_objectSpread({
      className: "mb-4 text-base font-semibold [&>svg]:size-5",
      "data-testid": "".concat(hyphenize(label), "-go-back-button"),
      onClick: handleGoBack
    }, _objectSpread({}, omit(["icon", "isConfigureNavLink"], otherProps))), {}, {
      children: [/*#__PURE__*/jsx(ChevronLeft, {}), /*#__PURE__*/jsx("span", {
        children: label
      })]
    })), /*#__PURE__*/jsx(SidebarGroupContent, {
      children: items && renderItems(items)
    })]
  });
};

export { ConfigureView as C, DEFAULT_HOME_PATH as D, MIN_SIDEBAR_WIDTH as M, PINNED_MORE_NAV_LINKS_KEY as P, SPECIAL_APP_NAMES as S, CheckPointNavLinks as a, SubLinkItem as b, SIDEBAR_WIDTH_KEY as c, DEFAULT_SIDEBAR_WIDTH as d, SELECTED_NAV_LINK_ROUTE_STORAGE_KEY as e, filterByPermissions as f, getSidebarStateLocalStorageKey as g, getActiveConfigurePageLink as h, MAX_SIDEBAR_WIDTH as i };
//# sourceMappingURL=ConfigureView-CG1M2-3b.js.map
