// import { formatImageUrl } from "@/utils/helpers";

// import { apiEndpoints } from "@/server_api/config/api.endpoints";
// import { blockReferences, websiteSetup } from "@/constants/setups/website-setup";
// import FetchAPIData from "@/server_api/apifunctions/apifetch";
// import { find, get, isEmpty, trim } from "lodash";
// import { fetchAPIData } from "@/server_api/apifunctions/api-fetch-fetch";
// import { routes } from "@/config/routes";
// import { Metadata } from "next";
// import { dynamicPage } from "@/constants/pages";
// import { SitemapEntry } from "@/types/sitemap/sitemap-types";

// const googleSiteVerification = "";

// export const getLayoutSeo = async (): Promise<Metadata> => {
//     try {
//         const [retVal, homeRetVal]: [any, any] = await Promise.all([
//             FetchAPIData.fetchAPIData({
//                 apiEndpoint: apiEndpoints.common.websiteSetups,
//                 block: websiteSetup.basicSettings,
//                 blockReference: `${blockReferences.websiteSettings},${blockReferences.basicDetailsSettings},${blockReferences.marketingScripts}`,
//             }),
//             FetchAPIData.fetchAPIData({
//                 apiEndpoint: apiEndpoints.pages.pageBySlug(dynamicPage.home),
//             }),
//         ]);

//         const requestedData = get(retVal, "requestedData", []) as any[];
//         const marketingAnalytics = find(requestedData, (data: any) => data.blockReference === blockReferences.marketingScripts)?.blockValues;
//         const websiteSettings = find(requestedData, (data: any) => data.blockReference === blockReferences.websiteSettings)?.blockValues;
//         const homeData = get(homeRetVal, "requestedData");
//         const ogImage = trim(get(homeData, 'ogImageUrl')) !== "" ? (get(homeData, 'ogImageUrl')) : (websiteSettings?.websiteLogoUrl);
//         const favIcon = websiteSettings?.favIconUrl;

//         if (homeData) {
//             return mapMetadata(homeData, {
//                 favIcon: formatImageUrl(favIcon),
//                 ogImage: formatImageUrl(ogImage),
//                 verification: extractVerification(marketingAnalytics),
//             });
//         }

//         return getDefaultFallbackSeo();
//     } catch (error) {
//         console.error("Error in getLayoutSeo:", error);
//         return getDefaultFallbackSeo();
//     }
// };

// export const getProductListingSeo = async ({ category }: { category: any }): Promise<Metadata> => {
//     try {
//         const homeRetVal: any = await fetchAPIData(null, apiEndpoints.products.findCategory(category));
//         const categoryData = get(homeRetVal, "requestedData");
//         const ogImage = trim(get(categoryData, 'ogImageUrl')) !== "" ? get(categoryData, 'ogImageUrl') : get(categoryData, 'categoryImageUrl');

//         if (categoryData) {
//             return mapMetadata(categoryData, {
//                 slug: routes.product.productsListing(category),
//                 canonical: routes.product.productsListing(category),
//                 ogImage: formatImageUrl(ogImage),
//             });
//         }

//         return getDefaultFallbackSeo(routes.product.productsListing(category), "Product Listing");
//     } catch (error) {
//         console.error("Error in getProductListingSeo:", error);
//         return getDefaultFallbackSeo(routes.product.productsListing(category), "Product Listing");
//     }
// };

// // export const getBlogSeo = async ({ slug }: { slug: any }): Promise<Metadata> => {
// //     try {
// //         const retVal = await fetchAPIData({ slug, getseo: "1" }, apiEndpoints.common.blogs);
// //         const blogData = get(retVal, "requestedData.blogDetails");

// //         if (blogData) {
// //             return mapMetadata({ ...blogData, ogImageUrl: blogData?.ogImageUrl || blogData?.blogImageUrl }, {
// //                 slug: routes.pages.blogDetails(slug),
// //                 canonical: routes.pages.blogDetails(slug),
// //             });
// //         }

// //         return getDefaultFallbackSeo(routes.pages.blogDetails(slug), "Blog Details");
// //     } catch (error) {
// //         console.error("Error in getBlogSeo:", error);
// //         return getDefaultFallbackSeo(routes.pages.blogDetails(slug), "Blog Details");
// //     }
// // };

// // export const getProjectsSeo = async ({ slug }: { slug: any }): Promise<Metadata> => {
// //     try {
// //         const retVal = await fetchAPIData({ slug, getseo: "1" }, apiEndpoints.common.projects);
// //         const projectData = get(retVal, "requestedData");

// //         if (projectData) {
// //             return mapMetadata({ ...projectData, ogImageUrl: projectData?.ogImageUrl || projectData?.projectImageUrl }, {
// //                 slug: routes.pages.projectDetails(slug),
// //                 canonical: routes.pages.projectDetails(slug),
// //             });
// //         }

// //         return getDefaultFallbackSeo(routes.pages.projectDetails(slug), "Project Details");
// //     } catch (error) {
// //         console.error("Error in getProjectsSeo:", error);
// //         return getDefaultFallbackSeo(routes.pages.projectDetails(slug), "Project Details");
// //     }
// // };

// export const getDefaultFallbackSeo = (canonical?: string, title?: string): Metadata => {
//     const baseUrl = process.env.NEXT_PUBLIC_APP_BASE_URL || "";
//     return {
//         title: title || process.env.NEXT_PUBLIC_APP_NAME,
//         description: "",
//         icons: {
//             icon: `${process.env.NEXT_PUBLIC_APP_BASE_URL}/images/logo/logo.png`,
//         },
//         alternates: {
//             canonical: canonical ? baseUrl + (canonical.startsWith("/") ? canonical : "/" + canonical) : baseUrl,
//         },
//         other: {
//             "google-site-verification": googleSiteVerification,
//         },
//     };
// };

// export const getProductDetailsSeo = async ({ slug }: { slug: any }): Promise<Metadata> => {
//     try {
//         const retVal: any = await FetchAPIData.fetchAPIData({
//             apiEndpoint: apiEndpoints.products.productDetailSeo(slug),
//         });

//         const productData = get(retVal, "requestedData");
//         const ogImageUrl = trim(get(productData, "ogImageUrl")) !== "" ? get(productData, "ogImageUrl") : get(productData, "productImageUrl");

//         if (productData) {
//             return mapMetadata(productData, {
//                 slug: routes.product.productsDetail(slug),
//                 canonical: routes.product.productsDetail(slug),
//                 ogImage: formatImageUrl(ogImageUrl)
//             });
//         }

//         return getDefaultFallbackSeo(routes.product.productsDetail(slug), "Product Details");
//     } catch (error) {
//         console.error("Error in getProductDetailsSeo:", error);
//         return getDefaultFallbackSeo(routes.product.productsDetail(slug), "Product Details");
//     }
// };

// export const getDynamicPageSeo = async ({ slug, externalCanonical, fallbackPageTitle }: { slug: any, externalCanonical?: string, fallbackPageTitle?: string }): Promise<any> => {
//     const finalCanonical = externalCanonical ? externalCanonical : slug;

//     try {
//         const apiEndpoint = apiEndpoints.pages.pageBySlug(slug)
//         const retVal = await fetchAPIData(null, apiEndpoint);
//         const pageData = get(retVal, "requestedData");

//         if (pageData) {
//             return mapMetadata(pageData, {
//                 slug: slug,
//                 canonical: finalCanonical,
//             });
//         }

//         return { ...getDefaultFallbackSeo(finalCanonical, fallbackPageTitle), pageNotFound: true };
//     } catch (error) {
//         console.error("Error in getDynamicPageSeo:", error);
//         return getDefaultFallbackSeo(finalCanonical, fallbackPageTitle);
//     }
// };

// // export const getPuckTemplateSeo = async (
// //     page: any
// // ): Promise<Metadata> => {

// //     try {
// //         const pageData: any = await FetchAPIData.fetchAPIData({ apiEndpoint: apiEndpoints.common.puckTemplatePage, pageEndPoint: page, getseo: 1 });

// //         if (isEmpty(get(pageData, "requestedData"))) {
// //             return { title: "Page Not Found" };
// //         }

// //         const seoContent = mapMetadata(get(pageData, "requestedData"), {
// //             canonical: `/${page}`,
// //         });

// //         return seoContent;
// //     } catch (error) {
// //         console.error("Metadata Error:", error);
// //         return { title: "Page Not Found" };
// //     }
// // };

// export const mapMetadata = (
//     data: any,
//     options: {
//         slug?: string;
//         canonical?: string;
//         ogImage?: string;
//         favIcon?: string;
//         verification?: any;
//     } = {}
// ): Metadata => {
//     const { slug, canonical, ogImage, favIcon, verification } = options;

//     const baseUrl = process.env.NEXT_PUBLIC_APP_BASE_URL || "";
//     const finalCanonical = canonical ? `${baseUrl}${canonical.startsWith("/") ? canonical : "/" + canonical}` : `${baseUrl}`;

//     const title = data?.metaTitle || data?.productTitle || data?.title || data?.blogTitle || data?.projectTitle || process.env.NEXT_PUBLIC_APP_NAME;
//     const description = data?.metaDescription || data?.description || data?.blogDescription || data?.projectDescription || "";
//     const keywords = data?.metaKeywords || "";

//     const finalOgImage = ogImage || formatImageUrl(data?.ogImageUrl);
//     const finalTwitterImage = formatImageUrl(data?.twitterImage) || finalOgImage;

//     return {
//         title,
//         description,
//         keywords,
//         icons: {
//             icon: favIcon || "/favicon.ico",
//         },
//         openGraph: {
//             title: data?.ogTitle || title,
//             description: data?.ogDescription || description,
//             url: finalCanonical,
//             siteName: process.env.NEXT_PUBLIC_APP_NAME,
//             images: finalOgImage ? [{ url: finalOgImage, width: 800, height: 600 }] : [],
//             locale: "en_US",
//             type: "website",
//         },
//         twitter: {
//             card: "summary_large_image",
//             title: data?.twitterTitle || data?.ogTitle || title,
//             description: data?.twitterDescription || data?.ogDescription || description,
//             images: finalTwitterImage ? [finalTwitterImage] : [],
//         },
//         alternates: {
//             canonical: finalCanonical,
//         },
//         other: {
//             "google-site-verification": googleSiteVerification,
//         },
//         verification: verification,
//     };
// };

// export const mapSeoFromBlocks = (
//     requestedData: any[],
//     blockReference: string,
//     options: {
//         slug?: string;
//         canonical?: string;
//         ogImage?: string;
//         favIcon?: string;
//         verification?: any;
//     } = {}
// ): Metadata => {
//     const data = find(requestedData, (d: any) => d.blockReference === blockReference)?.blockValues;
//     return mapMetadata(data, options);
// };

// export const extractVerification = (marketingAnalytics: any) => {
//     const otherMetaVerification: any = {};

//     if (marketingAnalytics?.otherHeadScripts) {
//         Object.entries(marketingAnalytics.otherHeadScripts).forEach(([key, value]: any) => {
//             if (value?.type === "meta" && value?.code) {
//                 otherMetaVerification[key] = value.code;
//             }
//         });
//     }

//     return {
//         google: marketingAnalytics?.enableGoogleSiteVerification ? marketingAnalytics.googleSiteVerification : undefined,
//         other: otherMetaVerification,
//     };
// };

// export function generateSitemapXml(sitemapEntries: SitemapEntry[]): string {
//     const xml = `<?xml version="1.0" encoding="UTF-8"?>
//     <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
//       ${sitemapEntries
//             .map(
//                 (entry) => `
//         <url>
//           <loc>${entry.url}</loc>
//             <lastmod>${new Date().toISOString()}</lastmod>
//           <priority>${entry.priority}</priority>
//         </url>
//       `,
//             )
//             .join('')}
//     </urlset>`
//     return xml
// }