"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = NavigationMenuContent;
var _blockEditor = require("@wordpress/block-editor");
var _data = require("@wordpress/data");
var _blocks = require("@wordpress/blocks");
var _element = require("@wordpress/element");
var _coreData = require("@wordpress/core-data");
var _lockUnlock = require("../../lock-unlock");
var _leafMoreMenu = _interopRequireDefault(require("./leaf-more-menu"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  PrivateListView
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);

// Needs to be kept in sync with the query used at packages/block-library/src/page-list/edit.js.
const MAX_PAGE_COUNT = 100;
const PAGES_QUERY = ['postType', 'page', {
  per_page: MAX_PAGE_COUNT,
  _fields: ['id', 'link', 'menu_order', 'parent', 'title', 'type'],
  // TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby
  // values is resolved, update 'orderby' to [ 'menu_order', 'post_title' ] to provide a consistent
  // sort.
  orderby: 'menu_order',
  order: 'asc'
}];
function NavigationMenuContent({
  rootClientId
}) {
  const {
    listViewRootClientId,
    isLoading
  } = (0, _data.useSelect)(select => {
    const {
      areInnerBlocksControlled,
      getBlockName,
      getBlockCount,
      getBlockOrder
    } = select(_blockEditor.store);
    const {
      isResolving
    } = select(_coreData.store);
    const blockClientIds = getBlockOrder(rootClientId);
    const hasOnlyPageListBlock = blockClientIds.length === 1 && getBlockName(blockClientIds[0]) === 'core/page-list';
    const pageListHasBlocks = hasOnlyPageListBlock && getBlockCount(blockClientIds[0]) > 0;
    const isLoadingPages = isResolving('getEntityRecords', PAGES_QUERY);
    return {
      listViewRootClientId: pageListHasBlocks ? blockClientIds[0] : rootClientId,
      // This is a small hack to wait for the navigation block
      // to actually load its inner blocks.
      isLoading: !areInnerBlocksControlled(rootClientId) || isLoadingPages
    };
  }, [rootClientId]);
  const {
    replaceBlock,
    __unstableMarkNextChangeAsNotPersistent
  } = (0, _data.useDispatch)(_blockEditor.store);
  const offCanvasOnselect = (0, _element.useCallback)(block => {
    if (block.name === 'core/navigation-link' && !block.attributes.url) {
      __unstableMarkNextChangeAsNotPersistent();
      replaceBlock(block.clientId, (0, _blocks.createBlock)('core/navigation-link', block.attributes));
    }
  }, [__unstableMarkNextChangeAsNotPersistent, replaceBlock]);

  // The hidden block is needed because it makes block edit side effects trigger.
  // For example a navigation page list load its items has an effect on edit to load its items.
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [!isLoading && /*#__PURE__*/(0, _jsxRuntime.jsx)(PrivateListView, {
      rootClientId: listViewRootClientId,
      onSelect: offCanvasOnselect,
      blockSettingsMenu: _leafMoreMenu.default,
      showAppender: false
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "edit-site-sidebar-navigation-screen-navigation-menus__helper-block-editor",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockList, {})
    })]
  });
}
//# sourceMappingURL=navigation-menu-content.js.map