import { useEffect, useMemo } from "@wordpress/element";
import { useSelect, useDispatch } from "@wordpress/data";
import { store as coreDataStore } from "@wordpress/core-data";
import { privateApis as routerPrivateApis } from "@wordpress/router";
import { store as editSiteStore } from "../../store";
import { unlock } from "../../lock-unlock";
import {
  TEMPLATE_POST_TYPE,
  TEMPLATE_PART_POST_TYPE,
  NAVIGATION_POST_TYPE,
  PATTERN_TYPES
} from "../../utils/constants";
const { useLocation } = unlock(routerPrivateApis);
const postTypesWithoutParentTemplate = [
  TEMPLATE_POST_TYPE,
  TEMPLATE_PART_POST_TYPE,
  NAVIGATION_POST_TYPE,
  PATTERN_TYPES.user
];
const authorizedPostTypes = ["page", "post"];
function getPostType(name) {
  let postType;
  if (name === "navigation-item") {
    postType = NAVIGATION_POST_TYPE;
  } else if (name === "pattern-item") {
    postType = PATTERN_TYPES.user;
  } else if (name === "template-part-item") {
    postType = TEMPLATE_PART_POST_TYPE;
  } else if (name === "templates") {
    postType = TEMPLATE_POST_TYPE;
  } else if (name === "template-item") {
    postType = TEMPLATE_POST_TYPE;
  } else if (name === "page-item" || name === "pages") {
    postType = "page";
  } else if (name === "post-item" || name === "posts") {
    postType = "post";
  }
  return postType;
}
function useResolveEditedEntity() {
  const { name, params = {}, query } = useLocation();
  const { postId = query?.postId } = params;
  const postType = getPostType(name, postId) ?? query?.postType;
  const homePage = useSelect((select) => {
    const { getHomePage } = unlock(select(coreDataStore));
    return getHomePage();
  }, []);
  const resolvedTemplateId = useSelect(
    (select) => {
      if (postTypesWithoutParentTemplate.includes(postType) && postId) {
        return;
      }
      if (postId && postId.includes(",")) {
        return;
      }
      const { getTemplateId } = unlock(select(coreDataStore));
      if (postType && postId && authorizedPostTypes.includes(postType)) {
        return getTemplateId(postType, postId);
      }
      if (homePage?.postType === "page") {
        return getTemplateId("page", homePage?.postId);
      }
      if (homePage?.postType === "wp_template") {
        return homePage?.postId;
      }
    },
    [homePage, postId, postType]
  );
  const context = useMemo(() => {
    if (postTypesWithoutParentTemplate.includes(postType) && postId) {
      return {};
    }
    if (postType && postId && authorizedPostTypes.includes(postType)) {
      return { postType, postId };
    }
    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 !== void 0,
      postType: TEMPLATE_POST_TYPE,
      postId: resolvedTemplateId,
      context
    };
  }
  return { isReady: false };
}
function useSyncDeprecatedEntityIntoState({
  postType,
  postId,
  context,
  isReady
}) {
  const { setEditedEntity } = useDispatch(editSiteStore);
  useEffect(() => {
    if (isReady) {
      setEditedEntity(postType, String(postId), context);
    }
  }, [isReady, postType, postId, context, setEditedEntity]);
}
export {
  useResolveEditedEntity,
  useSyncDeprecatedEntityIntoState
};
//# sourceMappingURL=use-resolve-edited-entity.js.map
