"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useActivateTheme = useActivateTheme;
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _router = require("@wordpress/router");
var _url = require("@wordpress/url");
var _lockUnlock = require("../lock-unlock");
var _isPreviewingTheme = require("./is-previewing-theme");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useHistory,
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);

/**
 * This should be refactored to use the REST API, once the REST API can activate themes.
 *
 * @return {Function} A function that activates the theme.
 */
function useActivateTheme() {
  const history = useHistory();
  const {
    path
  } = useLocation();
  const {
    startResolution,
    finishResolution
  } = (0, _data.useDispatch)(_coreData.store);
  return async () => {
    if ((0, _isPreviewingTheme.isPreviewingTheme)()) {
      const activationURL = 'themes.php?action=activate&stylesheet=' + (0, _isPreviewingTheme.currentlyPreviewingTheme)() + '&_wpnonce=' + window.WP_BLOCK_THEME_ACTIVATE_NONCE;
      startResolution('activateTheme');
      await window.fetch(activationURL);
      finishResolution('activateTheme');
      // Remove the wp_theme_preview query param: we've finished activating
      // the queue and are switching to normal Site Editor.
      history.navigate((0, _url.addQueryArgs)(path, {
        wp_theme_preview: ''
      }));
    }
  };
}
//# sourceMappingURL=use-activate-theme.js.map