import styles from "./styles.module.css";
import { useSchemeColorByElement } from "@/hooks/useSchemeColorByElement";
import { useRef } from "react";
import { Outlet, useLocation } from "react-router-dom";
import routes from "@/routes";
import Header from "@/components/CompatibilityV2/components/Header";
import { EUnleashFlags, useUnleash } from "@/hooks/ab/unleash/useUnleash";

const isBackButtonVisibleRoutes = [
  routes.client.compatibilityV2Birthdate(),
  routes.client.compatibilityV2PalmsInformation(),
  routes.client.compatibilityV2WhatAspects(),
  routes.client.compatibilityV2RelationshipStatus(),
  routes.client.compatibilityV2ElementResonates(),
  routes.client.compatibilityV2FavoriteColor(),
  routes.client.compatibilityV2HeadOrHeart(),
  routes.client.compatibilityV2RelateFollowing(),
  routes.client.compatibilityV2LetScan(),
  routes.client.compatibilityV2ScanInstruction(),
  routes.client.compatibilityV2ScannedPhoto(),
  routes.client.compatibilityV2Email(),
  routes.client.compatibilityV2TrialChoice(),
  routes.client.compatibilityV2TrialPayment(),
  routes.client.compatibilityV2TryApp(),
  routes.client.compatibilityV2Payment(),
  routes.client.compatibilityV2ScanHand(),
];

function Layout() {
  const location = useLocation();
  const mainRef = useRef<HTMLDivElement>(null);
  useSchemeColorByElement(mainRef.current, "section.page, .page, section", [
    location,
  ]);

  const { variant: relationshipStatusPagePlacement = "v0" } = useUnleash({
    flag: EUnleashFlags.v2CompatibilityRelationshipStatusPagePlacement,
  });

  const getIsBackButtonVisible = () => {
    if (
      location.pathname.includes(
        routes.client.compatibilityV2RelationshipStatusResult()
      )
    ) {
      return false;
    }
    if (
      relationshipStatusPagePlacement === "v1" &&
      location.pathname.includes(
        routes.client.compatibilityV2RelationshipStatus()
      )
    ) {
      return false;
    }
    if (
      relationshipStatusPagePlacement === "v1" &&
      location.pathname.includes(routes.client.compatibilityV2Gender())
    ) {
      return true;
    }

    for (const route of isBackButtonVisibleRoutes) {
      if (location.pathname.includes(route)) return true;
    }
    return false;
  };

  return (
    <>
      <main className={`${styles.main} content`} ref={mainRef}>
        <Header
          className={styles.header}
          classNameTitle={styles["header-title"]}
          isBackButtonVisible={getIsBackButtonVisible()}
        />
        {/* <Suspense fallback={<LoadingPage />}> */}
        <section className={styles.page}>
          <Outlet />
        </section>
        {/* </Suspense> */}
      </main>
    </>
  );
}

export default Layout;
