"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = PostCardPanel;
var _components = require("@wordpress/components");
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _dom = require("@wordpress/dom");
var _store = require("../../store");
var _constants = require("../../store/constants");
var _lockUnlock = require("../../lock-unlock");
var _postActions = _interopRequireDefault(require("../post-actions"));
var _pageTypeBadge = _interopRequireDefault(require("../../utils/pageTypeBadge"));
var _getTemplateInfo = require("../../utils/get-template-info");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  Badge
} = (0, _lockUnlock.unlock)(_components.privateApis);

/**
 * Renders a title of the post type and the available quick actions available within a 3-dot dropdown.
 *
 * @param {Object}          props                     - Component props.
 * @param {string}          [props.postType]          - The post type string.
 * @param {string|string[]} [props.postId]            - The post id or list of post ids.
 * @param {Function}        [props.onActionPerformed] - A callback function for when a quick action is performed.
 * @return {React.ReactNode} The rendered component.
 */
function PostCardPanel({
  postType,
  postId,
  onActionPerformed
}) {
  const postIds = (0, _element.useMemo)(() => Array.isArray(postId) ? postId : [postId], [postId]);
  const {
    postTitle,
    icon,
    labels
  } = (0, _data.useSelect)(select => {
    const {
      getEditedEntityRecord,
      getCurrentTheme,
      getPostType
    } = select(_coreData.store);
    const {
      getPostIcon
    } = (0, _lockUnlock.unlock)(select(_store.store));
    let _title = '';
    const _record = getEditedEntityRecord('postType', postType, postIds[0]);
    if (postIds.length === 1) {
      var _getCurrentTheme;
      const {
        default_template_types: templateTypes = []
      } = (_getCurrentTheme = getCurrentTheme()) !== null && _getCurrentTheme !== void 0 ? _getCurrentTheme : {};
      const _templateInfo = [_constants.TEMPLATE_POST_TYPE, _constants.TEMPLATE_PART_POST_TYPE].includes(postType) ? (0, _getTemplateInfo.getTemplateInfo)({
        template: _record,
        templateTypes
      }) : {};
      _title = _templateInfo?.title || _record?.title;
    }
    return {
      postTitle: _title,
      icon: getPostIcon(postType, {
        area: _record?.area
      }),
      labels: getPostType(postType)?.labels
    };
  }, [postIds, postType]);
  const pageTypeBadge = (0, _pageTypeBadge.default)(postId);
  let title = (0, _i18n.__)('No title');
  if (labels?.name && postIds.length > 1) {
    title = (0, _i18n.sprintf)(
    // translators: %1$d number of selected items %2$s: Name of the plural post type e.g: "Posts".
    (0, _i18n.__)('%1$d %2$s'), postIds.length, labels?.name);
  } else if (postTitle) {
    title = (0, _dom.__unstableStripHTML)(postTitle);
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
    spacing: 1,
    className: "editor-post-card-panel",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
      spacing: 2,
      className: "editor-post-card-panel__header",
      align: "flex-start",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Icon, {
        className: "editor-post-card-panel__icon",
        icon: icon
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalText, {
        numberOfLines: 2,
        truncate: true,
        className: "editor-post-card-panel__title",
        as: "h2",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: "editor-post-card-panel__title-name",
          children: title
        }), pageTypeBadge && postIds.length === 1 && /*#__PURE__*/(0, _jsxRuntime.jsx)(Badge, {
          children: pageTypeBadge
        })]
      }), postIds.length === 1 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_postActions.default, {
        postType: postType,
        postId: postIds[0],
        onActionPerformed: onActionPerformed
      })]
    }), postIds.length > 1 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
      className: "editor-post-card-panel__description",
      children: (0, _i18n.sprintf)(
      // translators: %s: Name of the plural post type e.g: "Posts".
      (0, _i18n.__)('Changes will be applied to all selected %s.'), labels?.name.toLowerCase())
    })]
  });
}
//# sourceMappingURL=index.js.map