"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 _coreData = require("@wordpress/core-data");
var _htmlEntities = require("@wordpress/html-entities");
var _editor = require("@wordpress/editor");
var _useTitle = _interopRequireDefault(require("../routes/use-title"));
var _constants = require("../../utils/constants");
var _lockUnlock = require("../../lock-unlock");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  getTemplateInfo
} = (0, _lockUnlock.unlock)(_editor.privateApis);
function useEditorTitle(postType, postId) {
  const {
    title,
    isLoaded
  } = (0, _data.useSelect)(select => {
    var _select$getEntityReco;
    const {
      getEditedEntityRecord,
      hasFinishedResolution
    } = select(_coreData.store);
    if (!postId) {
      return {
        isLoaded: false
      };
    }
    const _record = getEditedEntityRecord('postType', postType, postId);
    const {
      default_template_types: templateTypes = []
    } = (_select$getEntityReco = select(_coreData.store).getEntityRecord('root', '__unstableBase')) !== null && _select$getEntityReco !== void 0 ? _select$getEntityReco : {};
    const templateInfo = getTemplateInfo({
      template: _record,
      templateTypes
    });
    const _isLoaded = hasFinishedResolution('getEditedEntityRecord', ['postType', postType, postId]);
    return {
      title: templateInfo.title,
      isLoaded: _isLoaded
    };
  }, [postType, postId]);
  let editorTitle;
  if (isLoaded) {
    var _POST_TYPE_LABELS$pos;
    editorTitle = (0, _i18n.sprintf)(
    // translators: A breadcrumb trail for the Admin document title. 1: title of template being edited, 2: type of template (Template or Template Part).
    (0, _i18n._x)('%1$s ‹ %2$s', 'breadcrumb trail'), (0, _htmlEntities.decodeEntities)(title), (_POST_TYPE_LABELS$pos = _constants.POST_TYPE_LABELS[postType]) !== null && _POST_TYPE_LABELS$pos !== void 0 ? _POST_TYPE_LABELS$pos : _constants.POST_TYPE_LABELS[_constants.TEMPLATE_POST_TYPE]);
  }

  // Only announce the title once the editor is ready to prevent "Replace"
  // action in <URLQueryController> from double-announcing.
  (0, _useTitle.default)(isLoaded && editorTitle);
}
var _default = exports.default = useEditorTitle;
//# sourceMappingURL=use-editor-title.js.map