"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = ConvertToTemplatePart;
var _data = require("@wordpress/data");
var _blockEditor = require("@wordpress/block-editor");
var _components = require("@wordpress/components");
var _blocks = require("@wordpress/blocks");
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _notices = require("@wordpress/notices");
var _icons = require("@wordpress/icons");
var _coreData = require("@wordpress/core-data");
var _fields = require("@wordpress/fields");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function ConvertToTemplatePart({
  clientIds,
  blocks
}) {
  const [isModalOpen, setIsModalOpen] = (0, _element.useState)(false);
  const {
    replaceBlocks
  } = (0, _data.useDispatch)(_blockEditor.store);
  const {
    createSuccessNotice
  } = (0, _data.useDispatch)(_notices.store);
  const {
    isBlockBasedTheme,
    canCreate
  } = (0, _data.useSelect)(select => {
    return {
      isBlockBasedTheme: select(_coreData.store).getCurrentTheme()?.is_block_theme,
      canCreate: select(_blockEditor.store).canInsertBlockType('core/template-part')
    };
  }, []);
  if (!isBlockBasedTheme || !canCreate) {
    return null;
  }
  const onConvert = async templatePart => {
    replaceBlocks(clientIds, (0, _blocks.createBlock)('core/template-part', {
      slug: templatePart.slug,
      theme: templatePart.theme
    }));
    createSuccessNotice((0, _i18n.__)('Template part created.'), {
      type: 'snackbar'
    });

    // The modal and this component will be unmounted because of `replaceBlocks` above,
    // so no need to call `closeModal` or `onClose`.
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
      icon: _icons.symbolFilled,
      onClick: () => {
        setIsModalOpen(true);
      },
      "aria-expanded": isModalOpen,
      "aria-haspopup": "dialog",
      children: (0, _i18n.__)('Create template part')
    }), isModalOpen && /*#__PURE__*/(0, _jsxRuntime.jsx)(_fields.CreateTemplatePartModal, {
      closeModal: () => {
        setIsModalOpen(false);
      },
      blocks: blocks,
      onCreate: onConvert
    })]
  });
}
//# sourceMappingURL=convert-to-template-part.js.map