/**
 * WordPress dependencies
 */
import { __, _x } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { download } from '@wordpress/icons';
import { useDispatch, useSelect } from '@wordpress/data';
import { downloadBlob } from '@wordpress/blob';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { jsx as _jsx } from "react/jsx-runtime";
export default function SiteExport() {
  const canExport = useSelect(select => {
    var _select$getCurrentThe;
    const targetHints = (_select$getCurrentThe = select(coreStore).getCurrentTheme()?._links?.['wp:export-theme']?.[0]?.targetHints) !== null && _select$getCurrentThe !== void 0 ? _select$getCurrentThe : {};
    return !!targetHints.allow?.includes('GET');
  }, []);
  const {
    createErrorNotice
  } = useDispatch(noticesStore);
  if (!canExport) {
    return null;
  }
  async function handleExport() {
    try {
      const response = await apiFetch({
        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';
      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 : __('An error occurred while creating the site export.');
      createErrorNotice(errorMessage, {
        type: 'snackbar'
      });
    }
  }
  return /*#__PURE__*/_jsx(MenuItem, {
    role: "menuitem",
    icon: download,
    onClick: handleExport,
    info: __('Download your theme with updated templates and styles.'),
    children: _x('Export', 'site exporter menu item')
  });
}
//# sourceMappingURL=site-export.js.map