"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useTitle;
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _i18n = require("@wordpress/i18n");
var _a11y = require("@wordpress/a11y");
var _htmlEntities = require("@wordpress/html-entities");
var _router = require("@wordpress/router");
var _lockUnlock = require("../../lock-unlock");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
function useTitle(title) {
  const location = useLocation();
  const siteTitle = (0, _data.useSelect)(select => select(_coreData.store).getEntityRecord('root', 'site')?.title, []);
  const isInitialLocationRef = (0, _element.useRef)(true);
  (0, _element.useEffect)(() => {
    isInitialLocationRef.current = false;
  }, [location]);
  (0, _element.useEffect)(() => {
    // Don't update or announce the title for initial page load.
    if (isInitialLocationRef.current) {
      return;
    }
    if (title && siteTitle) {
      // @see https://github.com/WordPress/wordpress-develop/blob/94849898192d271d533e09756007e176feb80697/src/wp-admin/admin-header.php#L67-L68
      const formattedTitle = (0, _i18n.sprintf)(/* translators: Admin document title. 1: Admin screen name, 2: Network or site name. */
      (0, _i18n.__)('%1$s ‹ %2$s ‹ Editor — WordPress'), (0, _htmlEntities.decodeEntities)(title), (0, _htmlEntities.decodeEntities)(siteTitle));
      document.title = formattedTitle;

      // Announce title on route change for screen readers.
      (0, _a11y.speak)(title, 'assertive');
    }
  }, [title, siteTitle, location]);
}
//# sourceMappingURL=use-title.js.map