/* wp:polyfill */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _components = require("@wordpress/components");
var _coreData = require("@wordpress/core-data");
var _element = require("@wordpress/element");
var _hierarchicalTermSelector = _interopRequireDefault(require("../post-taxonomies/hierarchical-term-selector"));
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function MaybeCategoryPanel() {
  const hasNoCategory = (0, _data.useSelect)(select => {
    const postType = select(_store.store).getCurrentPostType();
    const {
      canUser,
      getEntityRecord,
      getTaxonomy
    } = select(_coreData.store);
    const categoriesTaxonomy = getTaxonomy('category');
    const defaultCategoryId = canUser('read', {
      kind: 'root',
      name: 'site'
    }) ? getEntityRecord('root', 'site')?.default_category : undefined;
    const defaultCategory = defaultCategoryId ? getEntityRecord('taxonomy', 'category', defaultCategoryId) : undefined;
    const postTypeSupportsCategories = categoriesTaxonomy && categoriesTaxonomy.types.some(type => type === postType);
    const categories = categoriesTaxonomy && select(_store.store).getEditedPostAttribute(categoriesTaxonomy.rest_base);

    // This boolean should return true if everything is loaded
    // ( categoriesTaxonomy, defaultCategory )
    // and the post has not been assigned a category different than "uncategorized".
    return !!categoriesTaxonomy && !!defaultCategory && postTypeSupportsCategories && (categories?.length === 0 || categories?.length === 1 && defaultCategory?.id === categories[0]);
  }, []);
  const [shouldShowPanel, setShouldShowPanel] = (0, _element.useState)(false);
  (0, _element.useEffect)(() => {
    // We use state to avoid hiding the panel if the user edits the categories
    // and adds one within the panel itself (while visible).
    if (hasNoCategory) {
      setShouldShowPanel(true);
    }
  }, [hasNoCategory]);
  if (!shouldShowPanel) {
    return null;
  }
  const panelBodyTitle = [(0, _i18n.__)('Suggestion:'), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
    className: "editor-post-publish-panel__link",
    children: (0, _i18n.__)('Assign a category')
  }, "label")];
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.PanelBody, {
    initialOpen: false,
    title: panelBodyTitle,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
      children: (0, _i18n.__)('Categories provide a helpful way to group related posts together and to quickly tell readers what a post is about.')
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_hierarchicalTermSelector.default, {
      slug: "category"
    })]
  });
}
var _default = exports.default = MaybeCategoryPanel;
//# sourceMappingURL=maybe-category-panel.js.map