"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useResolveEditedEntity = useResolveEditedEntity;
exports.useSyncDeprecatedEntityIntoState = useSyncDeprecatedEntityIntoState;
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _router = require("@wordpress/router");
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _constants = require("../../utils/constants");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
const postTypesWithoutParentTemplate = [_constants.TEMPLATE_POST_TYPE, _constants.TEMPLATE_PART_POST_TYPE, _constants.NAVIGATION_POST_TYPE, _constants.PATTERN_TYPES.user];
const authorizedPostTypes = ['page', 'post'];
function useResolveEditedEntity() {
  const {
    name,
    params = {},
    query
  } = useLocation();
  const {
    postId = query?.postId
  } = params; // Fallback to query param for postId for list view routes.
  let postType;
  if (name === 'navigation-item') {
    postType = _constants.NAVIGATION_POST_TYPE;
  } else if (name === 'pattern-item') {
    postType = _constants.PATTERN_TYPES.user;
  } else if (name === 'template-part-item') {
    postType = _constants.TEMPLATE_PART_POST_TYPE;
  } else if (name === 'template-item' || name === 'templates') {
    postType = _constants.TEMPLATE_POST_TYPE;
  } else if (name === 'page-item' || name === 'pages') {
    postType = 'page';
  } else if (name === 'post-item' || name === 'posts') {
    postType = 'post';
  }
  const homePage = (0, _data.useSelect)(select => {
    const {
      getHomePage
    } = (0, _lockUnlock.unlock)(select(_coreData.store));
    return getHomePage();
  }, []);

  /**
   * This is a hook that recreates the logic to resolve a template for a given WordPress postID postTypeId
   * in order to match the frontend as closely as possible in the site editor.
   *
   * It is not possible to rely on the server logic because there maybe unsaved changes that impact the template resolution.
   */
  const resolvedTemplateId = (0, _data.useSelect)(select => {
    // If we're rendering a post type that doesn't have a template
    // no need to resolve its template.
    if (postTypesWithoutParentTemplate.includes(postType) && postId) {
      return;
    }

    // Don't trigger resolution for multi-selected posts.
    if (postId && postId.includes(',')) {
      return;
    }
    const {
      getTemplateId
    } = (0, _lockUnlock.unlock)(select(_coreData.store));

    // If we're rendering a specific page, we need to resolve its template.
    // The site editor only supports pages for now, not other CPTs.
    if (postType && postId && authorizedPostTypes.includes(postType)) {
      return getTemplateId(postType, postId);
    }

    // If we're rendering the home page, and we have a static home page, resolve its template.
    if (homePage?.postType === 'page') {
      return getTemplateId('page', homePage?.postId);
    }
    if (homePage?.postType === 'wp_template') {
      return homePage?.postId;
    }
  }, [homePage, postId, postType]);
  const context = (0, _element.useMemo)(() => {
    if (postTypesWithoutParentTemplate.includes(postType) && postId) {
      return {};
    }
    if (postType && postId && authorizedPostTypes.includes(postType)) {
      return {
        postType,
        postId
      };
    }
    // TODO: for post types lists we should probably not render the front page, but maybe a placeholder
    // with a message like "Select a page" or something similar.
    if (homePage?.postType === 'page') {
      return {
        postType: 'page',
        postId: homePage?.postId
      };
    }
    return {};
  }, [homePage, postType, postId]);
  if (postTypesWithoutParentTemplate.includes(postType) && postId) {
    return {
      isReady: true,
      postType,
      postId,
      context
    };
  }
  if (!!homePage) {
    return {
      isReady: resolvedTemplateId !== undefined,
      postType: _constants.TEMPLATE_POST_TYPE,
      postId: resolvedTemplateId,
      context
    };
  }
  return {
    isReady: false
  };
}
function useSyncDeprecatedEntityIntoState({
  postType,
  postId,
  context,
  isReady
}) {
  const {
    setEditedEntity
  } = (0, _data.useDispatch)(_store.store);
  (0, _element.useEffect)(() => {
    if (isReady) {
      setEditedEntity(postType, postId, context);
    }
  }, [isReady, postType, postId, context, setEditedEntity]);
}
//# sourceMappingURL=use-resolve-edited-entity.js.map