"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSetAsHomepageAction = 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 SetAsHomepageModal = ({
  items,
  closeModal
}) => {
  const [item] = items;
  const pageTitle = (0, _getItemTitle.getItemTitle)(item);
  const {
    showOnFront,
    currentHomePage,
    isSaving
  } = (0, _data.useSelect)(select => {
    const {
      getEntityRecord,
      isSavingEntityRecord
    } = select(_coreData.store);
    const siteSettings = getEntityRecord('root', 'site');
    const currentHomePageItem = getEntityRecord('postType', 'page', siteSettings?.page_on_front);
    return {
      showOnFront: siteSettings?.show_on_front,
      currentHomePage: currentHomePageItem,
      isSaving: isSavingEntityRecord('root', 'site')
    };
  });
  const {
    saveEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    createSuccessNotice,
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  async function onSetPageAsHomepage(event) {
    event.preventDefault();
    try {
      await saveEntityRecord('root', 'site', {
        page_on_front: item.id,
        show_on_front: 'page'
      });
      createSuccessNotice((0, _i18n.__)('Homepage updated.'), {
        type: 'snackbar'
      });
    } catch (error) {
      const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while setting the homepage.');
      createErrorNotice(errorMessage, {
        type: 'snackbar'
      });
    } finally {
      closeModal?.();
    }
  }
  let modalWarning = '';
  if ('posts' === showOnFront) {
    modalWarning = (0, _i18n.__)('This will replace the current homepage which is set to display latest posts.');
  } else if (currentHomePage) {
    modalWarning = (0, _i18n.sprintf)(
    // translators: %s: title of the current home page.
    (0, _i18n.__)('This will replace the current homepage: "%s"'), (0, _getItemTitle.getItemTitle)(currentHomePage));
  }
  const modalText = (0, _i18n.sprintf)(
  // translators: %1$s: title of the page to be set as the homepage, %2$s: homepage replacement warning message.
  (0, _i18n.__)('Set "%1$s" as the site homepage? %2$s'), pageTitle, modalWarning).trim();

  // translators: Button label to confirm setting the specified page as the homepage.
  const modalButtonLabel = (0, _i18n.__)('Set homepage');
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("form", {
    onSubmit: onSetPageAsHomepage,
    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 useSetAsHomepageAction = () => {
  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-homepage',
    label: (0, _i18n.__)('Set as homepage'),
    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: SetAsHomepageModal
  }), [pageForPosts, pageOnFront]);
};
exports.useSetAsHomepageAction = useSetAsHomepageAction;
//# sourceMappingURL=set-as-homepage.js.map