"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PostAuthor = PostAuthor;
exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _element = require("@wordpress/element");
var _htmlEntities = require("@wordpress/html-entities");
var _blockEditor = require("@wordpress/block-editor");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _check = _interopRequireDefault(require("./check"));
var _index = _interopRequireDefault(require("./index"));
var _postPanelRow = _interopRequireDefault(require("../post-panel-row"));
var _constants = require("./constants");
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function PostAuthorToggle({
  isOpen,
  onClick
}) {
  const {
    postAuthor
  } = (0, _data.useSelect)(select => {
    const id = select(_store.store).getEditedPostAttribute('author');
    return {
      postAuthor: select(_coreData.store).getUser(id, _constants.BASE_QUERY)
    };
  }, []);
  const authorName = (0, _htmlEntities.decodeEntities)(postAuthor?.name) || (0, _i18n.__)('(No author)');
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
    size: "compact",
    className: "editor-post-author__panel-toggle",
    variant: "tertiary",
    "aria-expanded": isOpen,
    "aria-label":
    // translators: %s: Author name.
    (0, _i18n.sprintf)((0, _i18n.__)('Change author: %s'), authorName),
    onClick: onClick,
    children: authorName
  });
}

/**
 * Renders the Post Author Panel component.
 *
 * @return {React.ReactNode} The rendered component.
 */
function PostAuthor() {
  // Use internal state instead of a ref to make sure that the component
  // re-renders when the popover's anchor updates.
  const [popoverAnchor, setPopoverAnchor] = (0, _element.useState)(null);
  // Memoize popoverProps to avoid returning a new object every time.
  const popoverProps = (0, _element.useMemo)(() => ({
    // Anchor the popover to the middle of the entire row so that it doesn't
    // move around when the label changes.
    anchor: popoverAnchor,
    placement: 'left-start',
    offset: 36,
    shift: true
  }), [popoverAnchor]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_check.default, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_postPanelRow.default, {
      label: (0, _i18n.__)('Author'),
      ref: setPopoverAnchor,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Dropdown, {
        popoverProps: popoverProps,
        contentClassName: "editor-post-author__panel-dialog",
        focusOnMount: true,
        renderToggle: ({
          isOpen,
          onToggle
        }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(PostAuthorToggle, {
          isOpen: isOpen,
          onClick: onToggle
        }),
        renderContent: ({
          onClose
        }) => /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "editor-post-author",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.__experimentalInspectorPopoverHeader, {
            title: (0, _i18n.__)('Author'),
            onClose: onClose
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_index.default, {
            onClose: onClose
          })]
        })
      })
    })
  });
}
var _default = exports.default = PostAuthor;
//# sourceMappingURL=panel.js.map