"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = AddNewPostModal;
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _coreData = require("@wordpress/core-data");
var _notices = require("@wordpress/notices");
var _htmlEntities = require("@wordpress/html-entities");
var _blocks = require("@wordpress/blocks");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

function AddNewPostModal({
  postType,
  onSave,
  onClose
}) {
  const labels = (0, _data.useSelect)(select => select(_coreData.store).getPostType(postType)?.labels, [postType]);
  const [isCreatingPost, setIsCreatingPost] = (0, _element.useState)(false);
  const [title, setTitle] = (0, _element.useState)('');
  const {
    saveEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    createErrorNotice,
    createSuccessNotice
  } = (0, _data.useDispatch)(_notices.store);
  const {
    resolveSelect
  } = (0, _data.useRegistry)();
  async function createPost(event) {
    event.preventDefault();
    if (isCreatingPost) {
      return;
    }
    setIsCreatingPost(true);
    try {
      const postTypeObject = await resolveSelect(_coreData.store).getPostType(postType);
      const newPage = await saveEntityRecord('postType', postType, {
        status: 'draft',
        title,
        slug: title !== null && title !== void 0 ? title : undefined,
        content: !!postTypeObject.template && postTypeObject.template.length ? (0, _blocks.serialize)((0, _blocks.synchronizeBlocksWithTemplate)([], postTypeObject.template)) : undefined
      }, {
        throwOnError: true
      });
      onSave(newPage);
      createSuccessNotice((0, _i18n.sprintf)(
      // translators: %s: Title of the created post or template, e.g: "Hello world".
      (0, _i18n.__)('"%s" successfully created.'), (0, _htmlEntities.decodeEntities)(newPage.title?.rendered || title)), {
        type: 'snackbar'
      });
    } catch (error) {
      const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while creating the item.');
      createErrorNotice(errorMessage, {
        type: 'snackbar'
      });
    } finally {
      setIsCreatingPost(false);
    }
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Modal, {
    title:
    // translators: %s: post type singular_name label e.g: "Page".
    (0, _i18n.sprintf)((0, _i18n.__)('Draft new: %s'), labels?.singular_name),
    onRequestClose: onClose,
    focusOnMount: "firstContentElement",
    size: "small",
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("form", {
      onSubmit: createPost,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
        spacing: 4,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
          __next40pxDefaultSize: true,
          __nextHasNoMarginBottom: true,
          label: (0, _i18n.__)('Title'),
          onChange: setTitle,
          placeholder: (0, _i18n.__)('No title'),
          value: title
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
          spacing: 2,
          justify: "end",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            __next40pxDefaultSize: true,
            variant: "tertiary",
            onClick: onClose,
            children: (0, _i18n.__)('Cancel')
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            __next40pxDefaultSize: true,
            variant: "primary",
            type: "submit",
            isBusy: isCreatingPost,
            "aria-disabled": isCreatingPost,
            children: (0, _i18n.__)('Create draft')
          })]
        })]
      })
    })
  });
}
//# sourceMappingURL=index.js.map