"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = PageListItemEdit;
var _clsx = _interopRequireDefault(require("clsx"));
var _blockEditor = require("@wordpress/block-editor");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _htmlEntities = require("@wordpress/html-entities");
var _icons = require("../navigation-link/icons");
var _utils = require("../navigation/edit/utils");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function useFrontPageId() {
  return (0, _data.useSelect)(select => {
    const canReadSettings = select(_coreData.store).canUser('read', {
      kind: 'root',
      name: 'site'
    });
    if (!canReadSettings) {
      return undefined;
    }
    const site = select(_coreData.store).getEntityRecord('root', 'site');
    return site?.show_on_front === 'page' && site?.page_on_front;
  }, []);
}
function PageListItemEdit({
  context,
  attributes
}) {
  const {
    id,
    label,
    link,
    hasChildren,
    title
  } = attributes;
  const isNavigationChild = ('showSubmenuIcon' in context);
  const frontPageId = useFrontPageId();
  const innerBlocksColors = (0, _utils.getColors)(context, true);
  const navigationChildBlockProps = (0, _utils.getNavigationChildBlockProps)(innerBlocksColors);
  const blockProps = (0, _blockEditor.useBlockProps)(navigationChildBlockProps, {
    className: 'wp-block-pages-list__item'
  });
  const innerBlocksProps = (0, _blockEditor.useInnerBlocksProps)(blockProps);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("li", {
    className: (0, _clsx.default)('wp-block-pages-list__item', {
      'has-child': hasChildren,
      'wp-block-navigation-item': isNavigationChild,
      'open-on-click': context.openSubmenusOnClick,
      'open-on-hover-click': !context.openSubmenusOnClick && context.showSubmenuIcon,
      'menu-item-home': id === frontPageId
    }),
    children: [hasChildren && context.openSubmenusOnClick ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
        type: "button",
        className: "wp-block-navigation-item__content wp-block-navigation-submenu__toggle",
        "aria-expanded": "false",
        children: (0, _htmlEntities.decodeEntities)(label)
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "wp-block-page-list__submenu-icon wp-block-navigation__submenu-icon",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.ItemSubmenuIcon, {})
      })]
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
      className: (0, _clsx.default)('wp-block-pages-list__item__link', {
        'wp-block-navigation-item__content': isNavigationChild
      }),
      href: link,
      children: (0, _htmlEntities.decodeEntities)(title)
    }), hasChildren && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [!context.openSubmenusOnClick && context.showSubmenuIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
        className: "wp-block-navigation-item__content wp-block-navigation-submenu__toggle wp-block-page-list__submenu-icon wp-block-navigation__submenu-icon",
        "aria-expanded": "false",
        type: "button",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.ItemSubmenuIcon, {})
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
        ...innerBlocksProps
      })]
    })]
  }, id);
}
//# sourceMappingURL=edit.js.map