"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSetAsPostsPageAction = void 0;
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _notices = require("@wordpress/notices");
var _getItemTitle = require("../../utils/get-item-title");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const SetAsPostsPageModal = ({
  items,
  closeModal
}) => {
  const [item] = items;
  const pageTitle = (0, _getItemTitle.getItemTitle)(item);
  const {
    currentPostsPage,
    isPageForPostsSet,
    isSaving
  } = (0, _data.useSelect)(select => {
    const {
      getEntityRecord,
      isSavingEntityRecord
    } = select(_coreData.store);
    const siteSettings = getEntityRecord('root', 'site');
    const currentPostsPageItem = getEntityRecord('postType', 'page', siteSettings?.page_for_posts);
    return {
      currentPostsPage: currentPostsPageItem,
      isPageForPostsSet: siteSettings?.page_for_posts !== 0,
      isSaving: isSavingEntityRecord('root', 'site')
    };
  });
  const {
    saveEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    createSuccessNotice,
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  async function onSetPageAsPostsPage(event) {
    event.preventDefault();
    try {
      await saveEntityRecord('root', 'site', {
        page_for_posts: item.id,
        show_on_front: 'page'
      });
      createSuccessNotice((0, _i18n.__)('Posts page updated.'), {
        type: 'snackbar'
      });
    } catch (error) {
      const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while setting the posts page.');
      createErrorNotice(errorMessage, {
        type: 'snackbar'
      });
    } finally {
      closeModal?.();
    }
  }
  const modalWarning = isPageForPostsSet && currentPostsPage ? (0, _i18n.sprintf)(
  // translators: %s: title of the current posts page.
  (0, _i18n.__)('This will replace the current posts page: "%s"'), (0, _getItemTitle.getItemTitle)(currentPostsPage)) : (0, _i18n.__)('This page will show the latest posts.');
  const modalText = (0, _i18n.sprintf)(
  // translators: %1$s: title of the page to be set as the posts page, %2$s: posts page replacement warning message.
  (0, _i18n.__)('Set "%1$s" as the posts page? %2$s'), pageTitle, modalWarning);

  // translators: Button label to confirm setting the specified page as the posts page.
  const modalButtonLabel = (0, _i18n.__)('Set posts page');
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("form", {
    onSubmit: onSetPageAsPostsPage,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
      spacing: "5",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
        children: modalText
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
        justify: "right",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          variant: "tertiary",
          onClick: () => {
            closeModal?.();
          },
          disabled: isSaving,
          accessibleWhenDisabled: true,
          children: (0, _i18n.__)('Cancel')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          variant: "primary",
          type: "submit",
          disabled: isSaving,
          accessibleWhenDisabled: true,
          children: modalButtonLabel
        })]
      })]
    })
  });
};
const useSetAsPostsPageAction = () => {
  const {
    pageOnFront,
    pageForPosts
  } = (0, _data.useSelect)(select => {
    const {
      getEntityRecord,
      canUser
    } = select(_coreData.store);
    const siteSettings = canUser('read', {
      kind: 'root',
      name: 'site'
    }) ? getEntityRecord('root', 'site') : undefined;
    return {
      pageOnFront: siteSettings?.page_on_front,
      pageForPosts: siteSettings?.page_for_posts
    };
  });
  return (0, _element.useMemo)(() => ({
    id: 'set-as-posts-page',
    label: (0, _i18n.__)('Set as posts page'),
    isEligible(post) {
      if (post.status !== 'publish') {
        return false;
      }
      if (post.type !== 'page') {
        return false;
      }

      // Don't show the action if the page is already set as the homepage.
      if (pageOnFront === post.id) {
        return false;
      }

      // Don't show the action if the page is already set as the page for posts.
      if (pageForPosts === post.id) {
        return false;
      }
      return true;
    },
    RenderModal: SetAsPostsPageModal
  }), [pageForPosts, pageOnFront]);
};
exports.useSetAsPostsPageAction = useSetAsPostsPageAction;
//# sourceMappingURL=set-as-posts-page.js.map