"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = SiteExport;
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
var _icons = require("@wordpress/icons");
var _data = require("@wordpress/data");
var _blob = require("@wordpress/blob");
var _notices = require("@wordpress/notices");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

function SiteExport() {
  const {
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  async function handleExport() {
    try {
      const response = await (0, _apiFetch.default)({
        path: '/wp-block-editor/v1/export',
        parse: false,
        headers: {
          Accept: 'application/zip'
        }
      });
      const blob = await response.blob();
      const contentDisposition = response.headers.get('content-disposition');
      const contentDispositionMatches = contentDisposition.match(/=(.+)\.zip/);
      const fileName = contentDispositionMatches[1] ? contentDispositionMatches[1] : 'edit-site-export';
      (0, _blob.downloadBlob)(fileName + '.zip', blob, 'application/zip');
    } catch (errorResponse) {
      let error = {};
      try {
        error = await errorResponse.json();
      } catch (e) {}
      const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while creating the site export.');
      createErrorNotice(errorMessage, {
        type: 'snackbar'
      });
    }
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
    role: "menuitem",
    icon: _icons.download,
    onClick: handleExport,
    info: (0, _i18n.__)('Download your theme with updated templates and styles.'),
    children: (0, _i18n._x)('Export', 'site exporter menu item')
  });
}
//# sourceMappingURL=site-export.js.map