"use client";

import { useQuery } from "@tanstack/react-query";
import Image from "next/image";
import Link from "next/link";
import { useEffect, useMemo, useState } from "react";

import ImageWithFallback from "@/components/common/ImageWithFallback";
import { getPopularStores } from "@/services/store.service";
import config from "@/utils/config";
import { getExtensionDetails } from "@/utils/helper";

const RatingSVG = () => (
  <svg
    height="50"
    viewBox="-23 0 512 512.00003"
    width="50"
    xmlns="http://www.w3.org/2000/svg"
  >
    <path
      d="m356.851562 402.378906c.410157-12.632812-9.808593-23.125-22.378906-23.125h-.046875v-.007812h1.871094c12.203125-.011719 22.085937-9.914063 22.078125-22.117188s-9.910156-22.023437-22.113281-22.015625c-10.175781 0-81.125 0-91.9375 0-2.5625 0-4.378907-2.503906-3.582031-4.9375l8.058593-24.597656c6.859375-20.933594 5.640625-35.1875 5.640625-59.707031 0-14.421875-11.691406-26.109375-26.113281-26.109375-13.101563 0-24.175781 9.703125-25.890625 22.691406-1.894531 14.371094-3.175781 39.925781-26.929688 62.800781l-16.769531 16.140625c-9.339843 8.996094-21.800781 14.019531-34.765625 14.019531h-22.027344v144.945313c5.992188-.003906 18.03125-.003906 18.039063-.003906 32.011719-.019531 29.085937 31.679687 68.898437 31.644531h135.496094c12.1875 0 22.066406-9.882812 22.066406-22.070312 0-12.191407-9.878906-22.070313-22.066406-22.070313h3.886719c11.992187 0 22.234375-9.328125 22.632813-21.316406.417968-12.53125-9.617188-22.824219-22.054688-22.824219h5.367188c12.003906 0 22.253906-9.339844 22.640624-21.339844zm0 0"
      fill="#ffcdbf"
    ></path>
    <path
      d="m324.378906 486.242188h-135.496094c-39.8125.039062-36.886718-31.664063-68.898437-31.644532-.007813 0-12.046875.003906-18.035156.007813v25.753906c5.988281-.003906 18.027343-.003906 18.035156-.003906 32.011719-.019531 29.085937 31.679687 68.898437 31.644531h135.496094c12.1875 0 22.066406-9.882812 22.066406-22.070312 0-4.8125-1.554687-9.253907-4.171874-12.878907-4.003907 5.558594-10.519532 9.191407-17.894532 9.191407zm0 0"
      fill="#ffbeab"
    ></path>
    <path
      d="m95.679688 322.390625h-83.589844c-6.304688 0-11.417969 5.113281-11.417969 11.417969v150.992187c0 6.308594 5.109375 11.421875 11.417969 11.421875h83.589844c6.304687 0 11.417968-5.113281 11.417968-11.421875v-150.992187c0-6.304688-5.113281-11.417969-11.417968-11.417969zm0 0"
      fill="#54a0ff"
    ></path>
    <path
      d="m31.578125 484.804688v-150.996094c0-6.304688 5.113281-11.417969 11.421875-11.417969h-30.910156c-6.304688 0-11.417969 5.113281-11.417969 11.417969v150.992187c0 6.308594 5.109375 11.421875 11.417969 11.421875h30.90625c-6.304688 0-11.417969-5.113281-11.417969-11.417968zm0 0"
      fill="#338def"
    ></path>
    <path
      d="m121.082031 119.558594c3.117188-2.980469 1.398438-8.164063-2.910156-8.78125l-33.285156-4.734375c-1.710938-.246094-3.1875-1.296875-3.953125-2.816407l-14.886719-29.542968c-1.925781-3.824219-7.492187-3.824219-9.421875 0l-14.882812 29.542968c-.765626 1.519532-2.246094 2.574219-3.957032 2.816407l-33.28125 4.734375c-4.308594.613281-6.03125 5.800781-2.910156 8.78125l24.082031 22.996094c1.238281 1.179687 1.800781 2.882812 1.511719 4.554687l-5.6875 32.46875c-.734375 4.203125 3.769531 7.410156 7.625 5.425781l29.769531-15.332031c1.527344-.785156 3.359375-.785156 4.886719 0l29.769531 15.332031c3.855469 1.984375 8.359375-1.222656 7.625-5.425781l-5.6875-32.46875c-.292969-1.671875.273438-3.375 1.511719-4.554687zm0 0"
      fill="#feca57"
    ></path>
    <path
      d="m63.53125 141.164062c.292969-1.667968-.269531-3.371093-1.507812-4.554687l-24.085938-22.996094c-2.386719-2.277343-1.929688-5.847656.347656-7.671875-.164062.039063-.328125.074219-.5.097656l-33.28125 4.738282c-4.308594.613281-6.027344 5.800781-2.910156 8.777344l24.082031 22.996093c1.238281 1.183594 1.804688 2.886719 1.511719 4.554688l-5.683594 32.472656c-.738281 4.203125 3.765625 7.410156 7.621094 5.425781l29.382812-15.132812zm0 0"
      fill="#f4b537"
    ></path>
    <path
      d="m40.851562 104.835938 33.28125-4.738282c1.65625-.234375 3.089844-1.234375 3.875-2.675781l-11.957031-23.738281c-1.929687-3.824219-7.496093-3.824219-9.421875 0l-14.886718 29.542968c-.34375.679688-.835938 1.261719-1.421876 1.722657.171876-.042969.347657-.085938.53125-.113281zm0 0"
      fill="#f4b537"
    ></path>
    <path
      d="m463.882812 119.558594c3.117188-2.980469 1.394532-8.164063-2.914062-8.78125l-33.28125-4.734375c-1.710938-.246094-3.191406-1.296875-3.957031-2.816407l-14.882813-29.542968c-1.925781-3.824219-7.496094-3.824219-9.421875 0l-14.886719 29.542968c-.765624 1.519532-2.242187 2.574219-3.953124 2.816407l-33.28125 4.734375c-4.3125.613281-6.03125 5.800781-2.914063 8.78125l24.082031 22.996094c1.238282 1.179687 1.804688 2.882812 1.511719 4.554687l-5.683594 32.46875c-.738281 4.203125 3.769531 7.410156 7.621094 5.425781l29.769531-15.332031c1.53125-.785156 3.359375-.785156 4.890625 0l29.765625 15.332031c3.855469 1.984375 8.359375-1.222656 7.625-5.425781l-5.6875-32.46875c-.292968-1.671875.273438-3.375 1.511719-4.554687zm0 0"
      fill="#feca57"
    ></path>
    <path
      d="m406.332031 141.164062c.292969-1.667968-.273437-3.371093-1.511719-4.554687l-24.082031-22.996094c-2.386719-2.277343-1.929687-5.847656.34375-7.671875-.164062.039063-.328125.074219-.496093.097656l-33.28125 4.738282c-4.308594.613281-6.03125 5.800781-2.914063 8.777344l24.085937 22.996093c1.238282 1.183594 1.800782 2.886719 1.507813 4.554688l-5.683594 32.472656c-.734375 4.203125 3.769531 7.410156 7.625 5.425781l29.382813-15.132812zm0 0"
      fill="#f4b537"
    ></path>
    <path
      d="m383.648438 104.835938 33.28125-4.738282c1.660156-.234375 3.09375-1.234375 3.875-2.675781l-11.957032-23.738281c-1.925781-3.824219-7.496094-3.824219-9.421875 0l-14.882812 29.542968c-.34375.679688-.835938 1.261719-1.425781 1.722657.175781-.042969.347656-.085938.53125-.113281zm0 0"
      fill="#f4b537"
    ></path>
    <path
      d="m329.339844 78.8125c5.042968-4.816406 2.257812-13.203125-4.707032-14.195312l-53.816406-7.660157c-2.765625-.394531-5.160156-2.09375-6.394531-4.550781l-24.066406-47.769531c-3.117188-6.183594-12.121094-6.183594-15.234375 0l-24.066406 47.769531c-1.238282 2.457031-3.628907 4.15625-6.398438 4.550781l-53.8125 7.660157c-6.96875.992187-9.75 9.378906-4.710938 14.195312l38.941407 37.179688c2.003906 1.914062 2.917969 4.667968 2.445312 7.367187l-9.195312 52.5c-1.1875 6.796875 6.097656 11.980469 12.328125 8.773437l48.132812-24.789062c2.476563-1.273438 5.429688-1.273438 7.90625 0l48.132813 24.789062c6.230469 3.207032 13.515625-1.976562 12.324219-8.773437l-9.191407-52.5c-.472656-2.699219.441407-5.453125 2.441407-7.367187zm0 0"
      fill="#feca57"
    ></path>
    <path
      d="m231.003906 51.015625c2.765625-.394531 5.15625-2.097656 6.394532-4.550781l12.015624-23.847656-9.058593-17.980469c-3.117188-6.183594-12.121094-6.183594-15.234375 0l-24.066406 47.769531c-.695313 1.371094-1.753907 2.5-3.03125 3.304688zm0 0"
      fill="#f4b537"
    ></path>
    <path
      d="m204.671875 169.917969 9.191406-52.503907c.472657-2.699218-.441406-5.453124-2.441406-7.363281l-38.941406-37.183593c-4.101563-3.914063-3.023438-10.191407 1.265625-12.933594l-32.902344 4.683594c-6.96875.992187-9.75 9.378906-4.710938 14.195312l38.941407 37.179688c2.003906 1.914062 2.917969 4.667968 2.445312 7.367187l-9.195312 52.5c-1.1875 6.796875 6.09375 11.980469 12.328125 8.773437l23.949218-12.335937c-.089843-.761719-.074218-1.558594.070313-2.378906zm0 0"
      fill="#f4b537"
    ></path>
    <g fill="#ffbeab">
      <path d="m289.019531 413.414062c0 5.691407 4.613281 10.304688 10.300781 10.304688h34.890626c11.757812 0 21.804687-8.964844 22.582031-20.605469h-57.46875c-5.691407 0-10.304688 4.613281-10.304688 10.300781zm0 0"></path>
      <path d="m289.019531 368.941406c0 5.691406 4.613281 10.304688 10.300781 10.304688h36.972657c11.695312-.011719 21.234375-9.109375 22.007812-20.605469h-58.980469c-5.6875 0-10.300781 4.613281-10.300781 10.300781zm0 0"></path>
      <path d="m350.839844 447.253906h-51.519532c-5.6875 0-10.300781 4.613282-10.300781 10.300782 0 5.691406 4.613281 10.304687 10.300781 10.304687h28.945313c11.753906 0 21.796875-8.964844 22.574219-20.605469zm0 0"></path>
    </g>
  </svg>
);

const SavingsSVG = () => (
  <svg
    height="50"
    width="50"
    version="1.1"
    xmlns="http://www.w3.org/2000/svg"
    x="0px"
    y="0px"
    viewBox="0 0 512.035 512.035"
    xmlSpace="preserve"
  >
    <path
      style={{ fill: "#F44336" }}
      d="M488.502,256.035l22.464-58.592c2.464-6.464,0.544-13.792-4.864-18.176l-48.704-39.488l-9.856-61.984
      c-1.088-6.848-6.464-12.192-13.312-13.28l-61.984-9.856L332.79,5.923c-4.352-5.408-11.84-7.328-18.144-4.864l-58.624,22.496
      L197.43,1.091c-6.496-2.496-13.76-0.512-18.144,4.864l-39.488,48.736l-61.984,9.856c-6.816,1.088-12.192,6.464-13.28,13.28
      l-9.856,61.984L5.942,179.299c-5.376,4.352-7.328,11.68-4.864,18.144l22.464,58.592L1.078,314.627
      c-2.496,6.464-0.512,13.792,4.864,18.144l48.736,39.456l9.856,61.984c1.088,6.848,6.432,12.224,13.28,13.312l61.984,9.856
      l39.488,48.704c4.384,5.44,11.712,7.36,18.176,4.864l58.56-22.432l58.592,22.464c1.856,0.704,3.776,1.056,5.728,1.056
      c4.704,0,9.344-2.08,12.448-5.952l39.456-48.704l61.984-9.856c6.848-1.088,12.224-6.464,13.312-13.312l9.856-61.984l48.704-39.456
      c5.408-4.384,7.328-11.68,4.864-18.144L488.502,256.035z"
    />
    <g>
      <path
        style={{ fill: "#FAFAFA" }}
        d="M208.022,224.035c-26.464,0-48-21.536-48-48s21.536-48,48-48s48,21.536,48,48
        S234.486,224.035,208.022,224.035z M208.022,160.035c-8.832,0-16,7.168-16,16s7.168,16,16,16s16-7.168,16-16
        S216.854,160.035,208.022,160.035z"
      />
      <path
        style={{ fill: "#FAFAFA" }}
        d="M304.022,384.035c-26.464,0-48-21.536-48-48s21.536-48,48-48s48,21.536,48,48
        S330.486,384.035,304.022,384.035z M304.022,320.035c-8.8,0-16,7.2-16,16s7.2,16,16,16s16-7.2,16-16
        S312.822,320.035,304.022,320.035z"
      />
      <path
        style={{ fill: "#FAFAFA" }}
        d="M176.022,384.035c-3.232,0-6.464-0.96-9.28-2.976c-7.2-5.152-8.864-15.136-3.712-22.336l160-224
        c5.152-7.2,15.136-8.864,22.336-3.712c7.2,5.12,8.832,15.136,3.712,22.304l-160,224
        C185.91,381.699,181.014,384.035,176.022,384.035z"
      />
    </g>
  </svg>
);

const ExtensionSVG = () => (
  <svg
    height="50"
    viewBox="0 0 512 512"
    width="50"
    xmlns="http://www.w3.org/2000/svg"
  >
    <g id="Layer_2_00000070830924676292862900000009638051286753829777_">
      <g id="extension">
        <path
          d="m511 288.2c0 17.5-5.6 32.2-16.7 44-11.2 11.8-25.5 20.1-43 25v115.5c0 10.5-3.9 19.7-11.8 27.6s-17 11.8-27.5 11.8h-115.6c-2.2-21-10-38-23.3-50.9s-29-19.4-46.9-19.4-33.6 6.5-46.9 19.4-21.2 29.8-23.4 50.8h-115.5c-10.5 0-19.7-3.9-27.6-11.8-7.9-7.9-11.8-17.1-11.8-27.6v-115.5c19.3-2.2 35.8-9.7 49.6-22.6s20.7-28.8 20.7-47.6-6.9-34.7-20.7-47.6-30.4-20.5-49.6-22.7v-115.5c0-10.5 3.9-19.7 11.8-27.6s17.1-11.8 27.6-11.8h116.2c4.8-17.5 13.5-32.2 25.9-44 12.4-11.8 27.4-17.7 44.9-17.7s32.5 5.9 45 17.7 21.1 26.5 25.9 44h113.5c10.5 0 19.7 3.9 27.6 11.8s11.8 17.1 11.8 27.6v113.5c17.5 4.8 31.8 13.9 43 27.2 11.1 13 17 29.4 16.8 46.4z"
          fill="#4ad5ff"
        />
      </g>
    </g>
  </svg>
);

const POINTS_DATA = [
  {
    Icon: RatingSVG,
    title: "30K+",
    subtitle: "5-Star Ratings",
  },
  {
    Icon: SavingsSVG,
    title: "$600+",
    subtitle: "Saved Per Year",
  },
  {
    Icon: ExtensionSVG,
    title: "Top-Rated",
    subtitle: " Shopping Extension",
  },
];

export default function HomePage() {
  const { browser, href } = getExtensionDetails();

  const { data } = useQuery({
    queryKey: ["popular-stores"],
    queryFn: getPopularStores,
  });
  const POPULAR_STORES = Object.values(data?.data || {}) || [];

  // Modal open AFTER 2 seconds
  useEffect(() => {
    const timer = setTimeout(() => {
      const modalEl = document.getElementById("coupon-modal");
      if (modalEl && (window as any).bootstrap) {
        const modal = new (window as any).bootstrap.Modal(modalEl);
        modal.show();
      }
    }, 2000);

    return () => clearTimeout(timer);
  }, []);

  const SectionHero = useMemo(
    () => (
      <section className="py-5 extension-banner ext-ld-extension-banner light-banner ext-ld-light-banner">
        <div className="container webContent ext-ld-webContent">
          <div className="row justify-content-between align-items-center">
            <div className="col-xl-8 col-lg-8 col-md-7 col-sm-12 col-12 d-flex flex-column gap-3 align-items-start">
              <h1 className="mainHeading ext-ld-mainHeading fw-bold">
                Turn Every Purchase Into Savings With Cash Back & Coupons at
                10,000+ Stores
              </h1>

              <div>
                <p className="subHeading ext-ld-subHeading">
                  Rebates.com finds coupons, activates Cash Back, and helps you
                  pay less without extra effort.
                </p>
                <p className="subHeading ext-ld-subHeading">
                  Join now and earn a $10 bonus.*
                </p>
              </div>

              <Link
                href={href}
                target="_blank"
                className="button btn-first ext-ld-button ext-ld-btn-first fs-5"
                rel="noopener noreferrer"
              >
                Get It Free for Your Browser
              </Link>
            </div>

            <div className="col-xl-4 col-lg-4 col-md-5 col-sm-12 col-12">
              <Image
                src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/banner-img.png`}
                alt=""
                width={500}
                height={500}
                className="w-100"
                priority
              />
            </div>
          </div>
        </div>
      </section>
    ),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [],
  );

  const SectionHowItWorks = useMemo(
    () => (
      <section className="py-5 ext-ld-howitworks-section">
        <div className="container ext-ld-webContent">
          <div className="row justify-content-center">
            <div className="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12 text-center">
              <h3 className="ext-ld-mainHeading mb-lg-0 mb-3">
                Start Saving in Three Easy Steps
              </h3>
            </div>
          </div>

          <div className="row justify-content-between align-items-center">
            <div className="col-xl-5 col-lg-5 col-md-5 col-sm-12 col-12">
              <div className="tab-content" id="ext-ld-works-tabContent">
                <div
                  className="tab-pane fade show active"
                  id="ext-ld-works-1"
                  role="tabpanel"
                >
                  <Image
                    src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/steps/1.png`}
                    alt=""
                    width={600}
                    height={400}
                    className="w-100"
                  />
                </div>

                <div
                  className="tab-pane fade"
                  id="ext-ld-works-2"
                  role="tabpanel"
                >
                  <Image
                    src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/steps/2.png`}
                    alt=""
                    width={600}
                    height={400}
                    className="w-100"
                  />
                </div>

                <div
                  className="tab-pane fade"
                  id="ext-ld-works-3"
                  role="tabpanel"
                >
                  <Image
                    src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/steps/3.png`}
                    alt=""
                    width={600}
                    height={400}
                    className="w-100"
                  />
                </div>
              </div>
            </div>

            <div className="col-xl-6 col-lg-6 col-md-7 col-sm-12 col-12">
              <div
                className="nav flex-column nav-pills gap-3 me-3"
                id="ext-ld-works-tab"
                role="tablist"
              >
                <button
                  className="nav-link text-start active"
                  id="ext-ld-works-1-tab"
                  data-bs-toggle="pill"
                  data-bs-target="#ext-ld-works-1"
                  type="button"
                  role="tab"
                >
                  <h3 className="ext-ld-subHeading ext-ld-color-black fw-600">
                    Quick Setup
                  </h3>
                  <p className="mb-0">
                    Add it to your computer in seconds. No cost, no hassle.
                  </p>
                </button>

                <button
                  className="nav-link text-start"
                  id="ext-ld-works-2-tab"
                  data-bs-toggle="pill"
                  data-bs-target="#ext-ld-works-2"
                  type="button"
                  role="tab"
                >
                  <h3 className="ext-ld-subHeading ext-ld-color-black fw-600">
                    Shop as Usual
                  </h3>
                  <p className="mb-0">
                    We automatically find available codes on supported sites.
                  </p>
                </button>

                <button
                  className="nav-link text-start"
                  id="ext-ld-works-3-tab"
                  data-bs-toggle="pill"
                  data-bs-target="#ext-ld-works-3"
                  type="button"
                  role="tab"
                >
                  <h3 className="ext-ld-subHeading ext-ld-color-black fw-600">
                    Get Cash Back
                  </h3>
                  <p className="mb-0">
                    Every purchase adds Cash Back straight to your account.
                  </p>
                </button>
              </div>
            </div>
          </div>
        </div>
      </section>
    ),
    [],
  );

  const SectionCenterBanner = useMemo(
    () => (
      <section className="py-5 ext-ld-center-banner-section ext-ld-abstract-banner-section">
        <div className="container ext-ld-webContent">
          <div className="row justify-content-between align-items-center g-0">
            <div className="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-12 d-flex flex-column align-items-center gap-3 text-center">
              <h3 className="ext-ld-mainHeading">
                $4.6 Billion Paid Out to 17 Million Members. Don’t Miss Out.
              </h3>

              <p className="ext-ld-largeHeading ext-ld-color-black">
                Your go-to stores are even better when Cash Back is built in.
              </p>

              <Link
                target="_blank"
                rel="noopener noreferrer"
                href={href}
                className="ext-ld-button ext-ld-btn-second px-5 fs-5"
              >
                Add To Browser
              </Link>
            </div>

            <div className="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-12">
              <Image
                src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/extension_img.png`}
                alt=""
                width={500}
                height={500}
                className="img-fluid"
              />
            </div>
          </div>
        </div>
      </section>
    ),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [],
  );

  const SectionFeatures = useMemo(
    () => (
      <section className="py-5 bg-white">
        <div className="container ext-ld-webContent">
          <div className="row text-center">
            <div className="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
              <h3 className="ext-ld-mainHeading">Don’t Let Savings Slip By</h3>
              <p className="ext-ld-subHeading">
                The Rebates.com browser extension helps you save automatically
                while you shop.
              </p>
            </div>
          </div>

          {/* Feature 1 */}
          <div className="row g-2 justify-content-between align-items-center">
            <div className="col-xl-5 col-lg-5 col-md-7 col-sm-12 col-12 order-md-1 order-2">
              <h5 className="ext-ld-largeHeading ext-ld-color-black">
                Cash Back Alerts, Right on Store Sites
              </h5>
              <p className="ext-ld-subHeading ext-ld-color-black">
                Just shop as usual. The extension shows Cash Back offers as you
                browse.
              </p>
            </div>

            <div className="col-xl-6 col-lg-6 col-md-5 col-sm-12 col-12 order-md-2 order-1">
              <Image
                src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/extension_img_2.png`}
                alt=""
                width={600}
                height={400}
                className="w-100"
              />
            </div>
          </div>

          {/* Feature 2 */}
          <div className="row justify-content-between align-items-center">
            <div className="col-xl-5 col-lg-5 col-md-7 col-sm-12 col-12 order-md-2 order-2">
              <h5 className="ext-ld-largeHeading ext-ld-color-black">
                No More Coupon Chasing
              </h5>
              <p className="ext-ld-subHeading ext-ld-color-black">
                We apply available codes so you don’t have to.
              </p>
            </div>

            <div className="col-xl-6 col-lg-6 col-md-5 col-sm-12 col-12 order-md-1 order-1">
              <Image
                src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/extension_img_3.png`}
                alt=""
                width={600}
                height={400}
                className="w-100"
              />
            </div>
          </div>

          {/* Feature 3 */}
          <div className="row justify-content-between align-items-center">
            <div className="col-xl-5 col-lg-5 col-md-7 col-sm-12 col-12 order-md-1 order-2">
              <h5 className="ext-ld-largeHeading ext-ld-color-black">
                We Track Down the Better Deal
              </h5>
              <p className="ext-ld-subHeading ext-ld-color-black">
                Feel good knowing you paid a solid price on top of earning Cash
                Back.
              </p>
            </div>

            <div className="col-xl-6 col-lg-6 col-md-5 col-sm-12 col-12 order-md-2 order-1">
              <Image
                src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/extension_img_4.png`}
                alt=""
                width={600}
                height={400}
                className="w-100"
              />
            </div>
          </div>
        </div>
      </section>
    ),
    [],
  );

  const SectionVideoCTA = useMemo(
    () => (
      <section className="py-5 ext-ld-light-banner">
        <div className="container ext-ld-webContent">
          <div className="row justify-content-center">
            <div className="col-xl-8 col-lg-8 col-md-10 col-sm-12 col-12 d-flex flex-column align-items-center gap-3 text-center">
              <div>
                <h4 className="ext-ld-mainHeading ext-ld-color-black mb-2">
                  Stop Overpaying. Start Saving Smarter.
                </h4>

                <p className="ext-ld-subHeading my-3">
                  Join 8 million users choosing a better way to save!
                </p>

                <a
                  href={href}
                  className="ext-ld-button ext-ld-btn-first"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  Add to {browser}. It’s Free
                </a>
              </div>

              <div className="ext-ld-video-section mt-3">
                <video
                  autoPlay
                  loop
                  muted
                  playsInline
                  disablePictureInPicture
                  preload="metadata"
                  controls={false}
                  aria-hidden="true"
                  onContextMenu={(e) => e.preventDefault()}
                  className="rounded-4"
                  src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/video.mp4`}
                />
              </div>
            </div>
          </div>
        </div>
      </section>
    ),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [],
  );

  const PopularStoresSection = ({ POPULAR_STORES }: any) => {
    return (
      <section className="py-md-5 py-4 ext-ld-popularStoresSection bg-light">
        <div className="container ext-ld-webContent">
          <div className="row justify-content-center">
            <div className="col-12 d-flex flex-column align-items-center gap-3 text-center">
              <h3 className="ext-ld-mainHeading fw-bold mb-0">
                More Than 30,000 Stores
              </h3>

              <p className="ext-ld-subHeading">
                From everyday buys to big trips, we check for coupons across
                thousands of shopping sites.
              </p>

              <ul className="ext-ld-popularStoresList ps-0 mt-4 d-flex flex-wrap justify-content-center gap-sm-2 gap-1">
                {POPULAR_STORES.map((store: any) => (
                  <li className="ext-ld-store-item" key={store?.brand_id}>
                    <Link
                      href={
                        store?.store ? `/coupons/${store.store}` : "/stores"
                      }
                    >
                      <ImageWithFallback
                        alt={store?.brand_name}
                        className="img-fluid"
                        src={store?.image_url}
                        width={100}
                        height={60}
                        unoptimized
                        fallback={store?.txt_img}
                      />
                    </Link>
                  </li>
                ))}
              </ul>
            </div>
          </div>
        </div>
      </section>
    );
  };

  const PointsSection = ({ POINTS_DATA }: any) => {
    return (
      <section className="ext-ld-points-section py-4">
        <div className="container ext-ld-webContent">
          <div className="ext-ld-pointer-block p-md-4 px-2 py-3 shadow-sm rounded-3">
            <div className="row justify-content-center align-items-center mb-4">
              <div className="col-12 text-center">
                <h2 className="ext-ld-largeHeading mb-2">
                  Shop Through Our Desktop Extension and Get More Value From
                  Every Purchase
                </h2>
              </div>
            </div>

            <div className="row g-3 justify-content-center">
              {POINTS_DATA.map(
                ({ Icon, title, subtitle }: any, index: number) => (
                  <div className="col-lg-4 col-sm-6 col-xs-12" key={index}>
                    <div className="ext-ld-point-option bg-white border border-3 shadow-sm p-md-4 p-2 rounded-3 text-center d-flex flex-column align-items-center gap-2">
                      <Icon />
                      <h3 className="ext-ld-mainHeading ext-ld-color-black fw-bold mb-0">
                        {title}
                      </h3>
                      <p className="ext-ld-subHeading">{subtitle}</p>
                    </div>
                  </div>
                ),
              )}
            </div>
          </div>
        </div>
      </section>
    );
  };

  function ReadMoreText({
    title,
    shortText,
    fullText,
  }: {
    title: string;
    shortText: string;
    fullText: string;
  }) {
    const [expanded, setExpanded] = useState(false);

    const text = useMemo(
      () => (expanded ? fullText : shortText),
      [expanded, shortText, fullText],
    );

    return (
      <section className="py-5 bg-light">
        <div className="container ext-ld-webContent">
          <div className="row justify-content-center">
            <div className="col-12 d-flex flex-column text-start gap-2">
              <h3 className="ext-ld-subHeading fw-bold mb-0">{title}</h3>

              <p className="mb-0">
                {text}
                {!expanded && <span className="dots">...</span>}
              </p>

              <button
                type="button"
                className="ext-ld-toggle-content-btn bg-transparent p-0 border-0 text-start"
                onClick={() => setExpanded((v) => !v)}
              >
                <span className="fw-600 color-first">
                  {expanded ? "Show less" : "Show more"}
                </span>{" "}
                <i
                  className={`fa fw-600 color-first ${
                    expanded ? "fa-angle-up" : "fa-angle-down"
                  }`}
                />
              </button>
            </div>
          </div>
        </div>
      </section>
    );
  }

  const FinalCTASection = useMemo(
    () => (
      <section className="py-5 ext-ld-abstract-banner-section">
        <div className="container ext-ld-webContent">
          <div className="row justify-content-center">
            <div className="col-xl-8 col-lg-8 col-md-12 col-sm-12 col-12 d-flex flex-column align-items-center gap-3 text-center">
              <h3 className="ext-ld-mainHeading fw-bold">
                Start Earning Cash Back With One Click
              </h3>

              <a
                href={href}
                className="ext-ld-button ext-ld-btn-second px-5 fs-5"
                target="_blank"
                rel="noopener noreferrer"
              >
                Add to {browser} and Save
              </a>
            </div>
          </div>
        </div>
      </section>
    ),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [],
  );

  return (
    <>
      <header className="header sticky-top bg-white shadow-sm" id="header">
        <div className="container">
          <div className="row py-2 g-lg-0 g-2 justify-content-between align-items-center">
            <div className="col-xxl-4 col-xl-4 col-lg-4 col-md-4 col-5">
              <Link href="https://rebates.com/" className="navbar-brand me-0">
                <Image
                  src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/logo.svg`}
                  alt="Rebates.com"
                  width={180}
                  height={40}
                  className="logo"
                  unoptimized
                />
              </Link>
            </div>

            <div className="col-xxl-8 col-xl-8 col-lg-8 col-md-8 col-7 d-flex justify-content-end">
              <Link
                href={href}
                target="_blank"
                rel="noopener noreferrer"
                className="button btn-first d-md-block d-none"
              >
                Add To Your Browser - It&apos;s Free
              </Link>
              <Link
                target="_blank"
                rel="noopener noreferrer"
                href={href}
                className="button btn-first d-md-none d-block"
              >
                Add To Browser
              </Link>
            </div>
          </div>
        </div>
      </header>
      {/* 1 */}
      {SectionHero}
      {/* 2 */}
      {SectionHowItWorks}
      {/* 3 */}
      {SectionCenterBanner}
      {/* 4 */}
      {SectionFeatures}
      {/* 5 */}
      {SectionVideoCTA}
      {/* 6 */}
      <PopularStoresSection POPULAR_STORES={POPULAR_STORES} />
      {/* 7 */}
      <PointsSection POINTS_DATA={POINTS_DATA} />
      {/* 8 */}
      <ReadMoreText
        title="Rebates.com $10 Bonus Offer Cash Back Terms"
        shortText="Offer subject to change or cancellation at any time. Available to new members only and not combinable with other offers, including the Referral Sign Up Bonus. One Welcome Bonus per eligible new member. To qualify, sign up as a new member when the Welcome Bonus is offered and make Qualifying Purchase(s) totaling at least $25."
        fullText="Offer subject to change or cancellation at any time. Available to new members only and not combinable with other offers, including the Referral Sign Up Bonus. One Welcome Bonus per eligible new member. To qualify, sign up as a new member when the Welcome Bonus is offered and make Qualifying Purchase(s) totaling at least $25 within 90 days of sign up. A Qualifying Purchase is a purchase that earns Cash Back or points through Rakuten.com, the Rakuten App, or the Rakuten browser extension. Purchases made on Amazon.com, except those made through https://music.amazon.com/, do not qualify. The Welcome Bonus will be added to your next Cash Back payment or points transfer."
      />
      {/* 9 */}
      {FinalCTASection}
      {/* MODAL */}
      <div
        className="modal fade"
        id="coupon-modal"
        data-bs-backdrop="static"
        data-bs-keyboard="false"
        tabIndex={-1}
      >
        <div className="modal-dialog modal-dialog-centered">
          <div className="modal-content ext-ld-webContent">
            <div className="modal-header pt-0 border-0">
              <Image
                src={`${config.ENV_VARS.CDN_URL}/assets/img/landing4/deal.png`}
                alt="Special deal"
                width={220}
                height={220}
                className="mx-auto ext-ld-modal-deal-img"
                unoptimized
              />
            </div>

            <div className="modal-body text-center pt-0">
              <h3 className="ext-ld-mainHeading fw-bold">Hold On!</h3>

              <h5 className="ext-ld-largeHeading ext-ld-color-black">
                You Almost Missed Easy Savings
              </h5>

              <p className="mb-0">
                Enable free coupons in two clicks and save at 200,000+ stores.
              </p>
            </div>

            <div className="modal-footer d-flex align-items-center flex-column gap-0 border-0">
              <a href={href} target="_blank" rel="noopener noreferrer">
                <button
                  type="button"
                  className="ext-ld-button ext-ld-btn-first"
                >
                  Turn On Free Coupons
                </button>
              </a>

              <button
                type="button"
                className="btn btn-secondary border-0 bg-transparent ext-ld-color-black p-0"
                data-bs-dismiss="modal"
              >
                No Thanks, Skip Savings
              </button>
            </div>
          </div>
        </div>
      </div>
    </>
  );
}
