"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = SidebarNavigationScreenNavigationMenu;
exports.postType = void 0;
var _coreData = require("@wordpress/core-data");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _htmlEntities = require("@wordpress/html-entities");
var _router = require("@wordpress/router");
var _sidebarNavigationScreenNavigationMenus = require("../sidebar-navigation-screen-navigation-menus");
var _moreMenu = _interopRequireDefault(require("./more-menu"));
var _singleNavigationMenu = _interopRequireDefault(require("./single-navigation-menu"));
var _useNavigationMenuHandlers = _interopRequireDefault(require("./use-navigation-menu-handlers"));
var _buildNavigationLabel = _interopRequireDefault(require("../sidebar-navigation-screen-navigation-menus/build-navigation-label"));
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
const postType = exports.postType = `wp_navigation`;
function SidebarNavigationScreenNavigationMenu({
  backPath
}) {
  const {
    params: {
      postId
    }
  } = useLocation();
  const {
    record: navigationMenu,
    isResolving
  } = (0, _coreData.useEntityRecord)('postType', postType, postId);
  const {
    isSaving,
    isDeleting
  } = (0, _data.useSelect)(select => {
    const {
      isSavingEntityRecord,
      isDeletingEntityRecord
    } = select(_coreData.store);
    return {
      isSaving: isSavingEntityRecord('postType', postType, postId),
      isDeleting: isDeletingEntityRecord('postType', postType, postId)
    };
  }, [postId]);
  const isLoading = isResolving || isSaving || isDeleting;
  const menuTitle = navigationMenu?.title?.rendered || navigationMenu?.slug;
  const {
    handleSave,
    handleDelete,
    handleDuplicate
  } = (0, _useNavigationMenuHandlers.default)();
  const _handleDelete = () => handleDelete(navigationMenu);
  const _handleSave = edits => handleSave(navigationMenu, edits);
  const _handleDuplicate = () => handleDuplicate(navigationMenu);
  if (isLoading) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenNavigationMenus.SidebarNavigationScreenWrapper, {
      description: (0, _i18n.__)('Navigation Menus are a curated collection of blocks that allow visitors to get around your site.'),
      backPath: backPath,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Spinner, {
        className: "edit-site-sidebar-navigation-screen-navigation-menus__loading"
      })
    });
  }
  if (!isLoading && !navigationMenu) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenNavigationMenus.SidebarNavigationScreenWrapper, {
      description: (0, _i18n.__)('Navigation Menu missing.'),
      backPath: backPath
    });
  }
  if (!navigationMenu?.content?.raw) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenNavigationMenus.SidebarNavigationScreenWrapper, {
      actions: /*#__PURE__*/(0, _jsxRuntime.jsx)(_moreMenu.default, {
        menuId: navigationMenu?.id,
        menuTitle: (0, _htmlEntities.decodeEntities)(menuTitle),
        onDelete: _handleDelete,
        onSave: _handleSave,
        onDuplicate: _handleDuplicate
      }),
      backPath: backPath,
      title: (0, _buildNavigationLabel.default)(navigationMenu?.title, navigationMenu?.id, navigationMenu?.status),
      description: (0, _i18n.__)('This Navigation Menu is empty.')
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_singleNavigationMenu.default, {
    navigationMenu: navigationMenu,
    backPath: backPath,
    handleDelete: _handleDelete,
    handleSave: _handleSave,
    handleDuplicate: _handleDuplicate
  });
}
//# sourceMappingURL=index.js.map