import React from "react";
import { useDevVerticalEmbedItem, useDevVerticalEmbedItems } from "./../../ricos-context/index.js";
import { useRequestService } from "./../../_shared/request-service-provider/request-service-provider.js";
const convertToItemType = (type) => {
  switch (type) {
    case "product":
      return "PRODUCT";
    case "event":
      return "EVENT";
    case "booking":
      return "BOOKING";
    default:
      return "UNKNOWN_ITEM_TYPE";
  }
};
const baseUrl = typeof location !== "undefined" && location.hostname === "editor.wix.com" ? "https://editor.wix.com/_api/ricos-embeddables/v1/embeddable-items/" : "https://www.wixapis.com/ricos/v1/embeddable-items";
const buildListEmbeddableItemsUrl = () => {
  return `${baseUrl}/list`;
};
const buildGetEmbeddableItemUrl = (itemId, itemType) => {
  return `${baseUrl}/${itemId}?itemType=${itemType}`;
};
const useVerticalEmbedRequestService = () => {
  const { makeRequest } = useRequestService();
  const devVerticalEmbedService = useDevVerticalEmbedItems();
  const devVerticalEmbedItemService = useDevVerticalEmbedItem();
  const fetchVerticalEmbedItems = async (type, query = "") => {
    if (devVerticalEmbedService) {
      return devVerticalEmbedService(type, query);
    }
    const itemType = convertToItemType(type);
    const requestBody = {
      itemType,
      limit: 50
    };
    if (query) {
      requestBody.query = query;
    }
    const url = buildListEmbeddableItemsUrl();
    try {
      const response = await makeRequest(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify(requestBody)
      });
      if (!response.ok) {
        const error = new Error(`Error while fetching vertical embed items: ${response.status}`);
        error.status = response.status;
        throw error;
      }
      const data = await response.json();
      return data.items;
    } catch (error) {
      console.error("Failed to fetch vertical embed items:", error);
      throw error;
    }
  };
  const fetchVerticalEmbedItem = async (type, embedId) => {
    if (devVerticalEmbedItemService) {
      return devVerticalEmbedItemService(type, embedId);
    }
    const url = buildGetEmbeddableItemUrl(embedId, convertToItemType(type));
    try {
      const response = await makeRequest(url, {
        method: "GET",
        headers: {
          "Content-Type": "application/json"
        }
      });
      if (!response.ok) {
        const error = new Error(`Error while fetching vertical embed item: ${response.status}`);
        error.status = response.status;
        throw error;
      }
      const data = await response.json();
      return data.embeddableItem;
    } catch (error) {
      console.error("Failed to fetch vertical embed item:", error);
      throw error;
    }
  };
  return React.useMemo(() => {
    return {
      fetchVerticalEmbedItems,
      fetchVerticalEmbedItem
    };
  }, [makeRequest]);
};
export {
  useVerticalEmbedRequestService
};
//# sourceMappingURL=fetch-vertical-embed-data.js.map
