import { getDefaultLocaleByLanguage, language } from "@/locales";
import routes, { chatsPrefix } from "@/routes";
import { Helmet } from "react-helmet";

const routesPalmistry = [
  "/palmistry",
  routes.client.skipTrial(),
  routes.client.addConsultant(),
  routes.client.addGuides(),
];

const routesChats = [chatsPrefix];

const isRouteInclude = (url: string, routes: string[]) => {
  for (const route of routes) {
    if (url.includes(route)) {
      return true;
    }
  }
  return false;
};

const MetaPixel = () => {
  const locale = getDefaultLocaleByLanguage(language);
  const isPalmistry = isRouteInclude(window.location.pathname, routesPalmistry);
  const isChats = isRouteInclude(window.location.pathname, routesChats);

  // Palmistry
  const FBScriptPalmistry1 = `
!function(f,b,e,v,n,t,s) 
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? 
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; 
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; 
n.queue=[];t=b.createElement(e);t.async=!0; 
t.src=v;s=b.getElementsByTagName(e)[0]; 
s.parentNode.insertBefore(t,s)}(window, document,'script', 
'https://connect.facebook.net/en_US/fbevents.js'); 
fbq('init', '456294924152594'); 
fbq('track', 'PageView');`;

  const FBScriptPalmistry2 = `!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '1046770063554120');
fbq('track', 'PageView');`;

  const FBScriptPalmistry3 = `
!function(f,b,e,v,n,t,s) 
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? 
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; 
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; 
n.queue=[];t=b.createElement(e);t.async=!0; 
t.src=v;s=b.getElementsByTagName(e)[0]; 
s.parentNode.insertBefore(t,s)}(window, document,'script', 
'https://connect.facebook.net/en_US/fbevents.js'); 
fbq('init', '790043826570930'); 
fbq('track', 'PageView');`;

  const FBScriptPalmistryES1 = `
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '1088618406337518');
fbq('track', 'PageView');`;

  const FBScriptPalmistryES2 = `
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '2126185901145571');
fbq('track', 'PageView');`;

  const FBScriptPalmistryPT1 = `
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '3936071523339830');
fbq('track', 'PageView');`;

  const FBScriptPalmistryEN1 = `
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '3771681503093940');
fbq('track', 'PageView');`;

  const FBScriptPalmistryEN2 = `
!function(f,b,e,v,n,t,s) 
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? 
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; 
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; 
n.queue=[];t=b.createElement(e);t.async=!0; 
t.src=v;s=b.getElementsByTagName(e)[0]; 
s.parentNode.insertBefore(t,s)}(window, document,'script', 
'https://connect.facebook.net/en_US/fbevents.js'); 
fbq('init', '3756105751317099'); 
fbq('track', 'PageView');`;

  // Compatibility
  const FBScriptCompatibilityES = `
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '468790489561492');
fbq('track', 'PageView');`;

  const FBScriptCompatibilityEN = `
!function(f,b,e,v,n,t,s) 
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? 
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; 
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; 
n.queue=[];t=b.createElement(e);t.async=!0; 
t.src=v;s=b.getElementsByTagName(e)[0]; 
s.parentNode.insertBefore(t,s)}(window, document,'script', 
'https://connect.facebook.net/en_US/fbevents.js'); 
fbq('init', '1218510985903341'); 
fbq('track', 'PageView');`;

  // Chats
  const FBScriptChatsEN = `
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '1236760817439322');
fbq('track', 'PageView');`;

  return (
    <Helmet htmlAttributes={{ lang: language }}>
      {/* Palmistry */}
      {isPalmistry && <script>{FBScriptPalmistry1}</script>}
      {isPalmistry && <script>{FBScriptPalmistry2}</script>}
      {isPalmistry && <script>{FBScriptPalmistry3}</script>}
      {isPalmistry && locale === "es" && (
        <>
          <script>{FBScriptPalmistryES1}</script>
          <script>{FBScriptPalmistryES2}</script>
        </>
      )}
      {isPalmistry && locale?.includes("pt") && (
        <script>{FBScriptPalmistryPT1}</script>
      )}
      {isPalmistry && locale === "en" && (
        <>
          <script>{FBScriptPalmistryEN1}</script>
          <script>{FBScriptPalmistryEN2}</script>
        </>
      )}
      {/* Compatibility */}
      {!isPalmistry && !isChats && locale === "es" && (
        <script>{FBScriptCompatibilityES}</script>
      )}
      {!isPalmistry && !isChats && locale === "en" && (
        <script>{FBScriptCompatibilityEN}</script>
      )}
      {/* Chats */}
      {isChats && locale === "en" && <script>{FBScriptChatsEN}</script>}
    </Helmet>
  );
};

export default MetaPixel;
