"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useNavigationMenuHandlers;
var _coreData = require("@wordpress/core-data");
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _notices = require("@wordpress/notices");
var _router = require("@wordpress/router");
var _ = require(".");
var _constants = require("../../utils/constants");
var _lockUnlock = require("../../lock-unlock");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
function useDeleteNavigationMenu() {
  const {
    deleteEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    createSuccessNotice,
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  const history = useHistory();
  const handleDelete = async navigationMenu => {
    const postId = navigationMenu?.id;
    try {
      await deleteEntityRecord('postType', _.postType, postId, {
        force: true
      }, {
        throwOnError: true
      });
      createSuccessNotice((0, _i18n.__)('Navigation Menu successfully deleted.'), {
        type: 'snackbar'
      });
      history.navigate('/navigation');
    } catch (error) {
      createErrorNotice((0, _i18n.sprintf)(/* translators: %s: error message describing why the navigation menu could not be deleted. */
      (0, _i18n.__)(`Unable to delete Navigation Menu (%s).`), error?.message), {
        type: 'snackbar'
      });
    }
  };
  return handleDelete;
}
function useSaveNavigationMenu() {
  const {
    getEditedEntityRecord
  } = (0, _data.useSelect)(select => {
    const {
      getEditedEntityRecord: getEditedEntityRecordSelector
    } = select(_coreData.store);
    return {
      getEditedEntityRecord: getEditedEntityRecordSelector
    };
  }, []);
  const {
    editEntityRecord,
    __experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    createSuccessNotice,
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  const handleSave = async (navigationMenu, edits) => {
    if (!edits) {
      return;
    }
    const postId = navigationMenu?.id;
    // Prepare for revert in case of error.
    const originalRecord = getEditedEntityRecord('postType', _constants.NAVIGATION_POST_TYPE, postId);

    // Apply the edits.
    editEntityRecord('postType', _.postType, postId, edits);
    const recordPropertiesToSave = Object.keys(edits);

    // Attempt to persist.
    try {
      await saveSpecifiedEntityEdits('postType', _.postType, postId, recordPropertiesToSave, {
        throwOnError: true
      });
      createSuccessNotice((0, _i18n.__)('Renamed Navigation Menu'), {
        type: 'snackbar'
      });
    } catch (error) {
      // Revert to original in case of error.
      editEntityRecord('postType', _.postType, postId, originalRecord);
      createErrorNotice((0, _i18n.sprintf)(/* translators: %s: error message describing why the navigation menu could not be renamed. */
      (0, _i18n.__)(`Unable to rename Navigation Menu (%s).`), error?.message), {
        type: 'snackbar'
      });
    }
  };
  return handleSave;
}
function useDuplicateNavigationMenu() {
  const history = useHistory();
  const {
    saveEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    createSuccessNotice,
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  const handleDuplicate = async navigationMenu => {
    const menuTitle = navigationMenu?.title?.rendered || navigationMenu?.slug;
    try {
      const savedRecord = await saveEntityRecord('postType', _.postType, {
        title: (0, _i18n.sprintf)(/* translators: %s: Navigation menu title */
        (0, _i18n._x)('%s (Copy)', 'navigation menu'), menuTitle),
        content: navigationMenu?.content?.raw,
        status: 'publish'
      }, {
        throwOnError: true
      });
      if (savedRecord) {
        createSuccessNotice((0, _i18n.__)('Duplicated Navigation Menu'), {
          type: 'snackbar'
        });
        history.navigate(`/wp_navigation/${savedRecord.id}`);
      }
    } catch (error) {
      createErrorNotice((0, _i18n.sprintf)(/* translators: %s: error message describing why the navigation menu could not be deleted. */
      (0, _i18n.__)(`Unable to duplicate Navigation Menu (%s).`), error?.message), {
        type: 'snackbar'
      });
    }
  };
  return handleDuplicate;
}
function useNavigationMenuHandlers() {
  return {
    handleDelete: useDeleteNavigationMenu(),
    handleSave: useSaveNavigationMenu(),
    handleDuplicate: useDuplicateNavigationMenu()
  };
}
//# sourceMappingURL=use-navigation-menu-handlers.js.map