import { useCallback } from 'react';
import { useShopifyUrl } from 'src/contexts/shopifyUrlContext/shopifyUrlContext';
import { IdExtractor } from 'src/contexts/types';

export const useShopifyUrlOperations = () => {
  const { getTrimmedPathName, getPlpOrPdpId, isOnPdpPage, isOnPlpPage, isReady } = useShopifyUrl();

  return {
    getTrimmedPathName,
    getPlpOrPdpId,
    isOnPdpPage,
    isOnPlpPage,
    isReady,
  };
};

export const useCurrentPageType = () => {
  const { isOnPdpPage, isOnPlpPage, isReady } = useShopifyUrl();

  const getPageType = useCallback(() => {
    if (!isReady) return 'unknown';
    if (isOnPdpPage()) return 'pdp';
    if (isOnPlpPage()) return 'plp';
    return 'other';
  }, [isReady, isOnPdpPage, isOnPlpPage]);

  return { getPageType, isReady };
};

export const useProductId = (extractor: IdExtractor) => {
  const { getPlpOrPdpId, isReady } = useShopifyUrl();

  const productId = useCallback(() => {
    if (!isReady) return null;
    return getPlpOrPdpId(extractor);
  }, [isReady, getPlpOrPdpId, extractor]);

  return { productId, isReady };
};
