import React, {useContext, useEffect} from 'react';
import Head from 'src/components/core/head';
import {NextPage} from 'next';
import {LanguageContext} from 'context/languageContext';
import {Page} from 'src/components/layouts/containers';
import {getPagebySlug, getSearchEngineIndexing} from 'services/wpgraphql';
import {BlockArray} from 'src/components/core/blockArray';
import {ComponentRenderer} from 'utils/componentMap';
import PreintakeCta from 'src/components/blocks/preintakeCta/preintakeCta';
import {PreintakeCtaProps} from 'src/components/blocks/preintakeCta/preintakeCta';

import type { GetStaticProps } from 'next';

interface IndexPageProps {
  sections: Array<any>;
  showPreintake: boolean;
  preintakeData: PreintakeCtaProps;
  shouldIndex?: string;
}

/**
 * Loop through page section blocks and display them
 *
 * @since v1.0.0
 * @param IndexPageProps
 * @returns 
 */
const Index: NextPage<IndexPageProps> = ({sections, showPreintake, preintakeData, shouldIndex}) => {
  const {language} = useContext(LanguageContext);

  // Initialize TikTok Pixel on home page only
  useEffect(() => {
    if (typeof window !== 'undefined') {
      (function (w: any, d: Document, t: string) {
        w.TiktokAnalyticsObject = t;
        const ttq = w[t] = w[t] || [];
        ttq.methods = ["page", "track", "identify", "instances", "debug", "on", "off", "once", "ready", "alias", "group", "enableCookie", "disableCookie", "holdConsent", "revokeConsent", "grantConsent"];
        ttq.setAndDefer = function (t: any, e: string) {
          t[e] = function () {
            t.push([e].concat(Array.prototype.slice.call(arguments, 0)));
          };
        };
        for (let i = 0; i < ttq.methods.length; i++) ttq.setAndDefer(ttq, ttq.methods[i]);
        ttq.instance = function (t: string) {
          const e = ttq._i[t] || [];
          for (let n = 0; n < ttq.methods.length; n++) ttq.setAndDefer(e, ttq.methods[n]);
          return e;
        };
        ttq.load = function (e: string, n?: any) {
          const r = "https://analytics.tiktok.com/i18n/pixel/events.js";
          ttq._i = ttq._i || {};
          ttq._i[e] = [];
          ttq._i[e]._u = r;
          ttq._t = ttq._t || {};
          ttq._t[e] = +new Date();
          ttq._o = ttq._o || {};
          ttq._o[e] = n || {};
          const script = d.createElement("script");
          script.type = "text/javascript";
          script.async = true;
          script.src = r + "?sdkid=" + e + "&lib=" + t;
          const firstScript = d.getElementsByTagName("script")[0];
          firstScript.parentNode?.insertBefore(script, firstScript);
        };
        ttq.load('D4J1PQBC77U6TA8BN5T0');
        ttq.page();
      })(window, document, 'ttq');
    }
  }, []);


  return (
    <Page>
      <Head title="Home" shouldIndex={shouldIndex} />
      <BlockArray
        items={sections}
        renderItem={(item, index) => <React.Fragment key={index}> {ComponentRenderer(item, language)}</React.Fragment>}
      />
      {showPreintake && <PreintakeCta {...preintakeData} language={language} />}
    </Page>
  );
};

/*
export const getServerSideProps: GetServerSideProps = async () => {

  const {data} = await getPagebySlug('home-page');
  const {"data": {"additionalSettings": {"searchEngineIndexing": shouldIndex}}} = await getSearchEngineIndexing();

  return {
    props: {
      sections: data.page.sections.layout,
      showPreintake: data.page.sections.showPreintake,
      preintakeData: data.post.acf_preintakeCta,
      shouldIndex: shouldIndex
    },
  };
};
*/

export const getStaticProps: GetStaticProps = async() => {
  const {data} = await getPagebySlug('home-page');
  const {"data": {"additionalSettings": {"searchEngineIndexing": shouldIndex}}} = await getSearchEngineIndexing();

  return {
    props: {
      sections: data.page.sections.layout,
      showPreintake: data.page.sections.showPreintake,
      preintakeData: data.post.acf_preintakeCta,
      shouldIndex: shouldIndex
    },
    revalidate: 5,
  };
};

//export const dynamic = 'force-dynamic';
//export const revalidate = 0;

export default Index;
