import {useContext, useLayoutEffect, useRef, useState} from 'react';
import AppContext from '../../app-context';
import CloseButton from '../common/close-button';
import ActionButton from '../common/action-button';
import GiftCard from '../common/gift-card';
import InputField from '../common/input-field';
import LoadingPage from './loading-page';
import CheckmarkIcon from '../../images/icons/checkmark.svg?react';
import giftCardNoiseUrl from '../../images/gift-card-noise.webp';
import giftCardOrbUrl from '../../images/gift-card-orb.webp';
import {getAvailableProducts, getCurrencySymbol, formatNumber, getStripeAmount, isCookiesDisabled, getActiveInterval} from '../../utils/helpers';
import {getGiftDurationLabel} from '../../utils/gift-redemption-notification';
import {ValidateInputForm} from '../../utils/form';
import {t} from '../../utils/i18n';
import useCardTilt from '../../utils/use-card-tilt';

export const GiftPageStyles = `
@property --shine-angle {
    syntax: '<angle>';
    inherits: false;
    initial-value: 243.43deg;
}

.gh-portal-popup-container.full-size.gift,
.gh-portal-popup-container.full-size.giftSuccess,
.gh-portal-popup-container.full-size.giftRedemption {
    padding: 0;
}

.gh-portal-popup-container.full-size.gift .gh-portal-closeicon-container,
.gh-portal-popup-container.full-size.giftSuccess .gh-portal-closeicon-container,
.gh-portal-popup-container.full-size.giftRedemption .gh-portal-closeicon-container {
    top: 32px;
    right: 32px;
}

.gh-portal-popup-container.full-size.gift .gh-portal-closeicon,
.gh-portal-popup-container.full-size.giftSuccess .gh-portal-closeicon,
.gh-portal-popup-container.full-size.giftRedemption .gh-portal-closeicon {
    color: rgba(255, 255, 255, 0.5);
}

.gh-portal-popup-container.full-size.gift .gh-portal-closeicon:hover,
.gh-portal-popup-container.full-size.giftSuccess .gh-portal-closeicon:hover,
.gh-portal-popup-container.full-size.giftRedemption .gh-portal-closeicon:hover {
    color: rgba(255, 255, 255, 0.8);
}

.gh-portal-content.gift,
.gh-portal-content.giftSuccess,
.gh-portal-content.giftRedemption {
    position: relative;
    padding: 0;
    min-height: 100vh;
}

.gh-portal-gift-checkout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    min-height: 100vh;
    width: 100%;
}

.gh-portal-gift-checkout-left {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--white);
    padding: 64px 48px;
}

/* Selection page only: useLayoutEffect locks the inner's vertical
   position; flex-start lets that JS-applied margin-top do the centering. */
.gh-portal-content.gift .gh-portal-gift-checkout-left {
    align-items: flex-start;
}

.gh-portal-gift-checkout-bg {
    display: none;
}

.gh-portal-gift-checkout-inner {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 496px;
    display: flex;
    flex-direction: column;
}

.gh-portal-gift-checkout-header {
    margin-bottom: 16px;
}

.gh-portal-gift-checkout-header .gh-portal-main-title {
    text-align: start;
    margin: 0 0 12px;
}

.gh-portal-gift-checkout-subtitle {
    margin: 0;
    font-size: 1.6rem;
    line-height: 1.45em;
    color: var(--grey3);
    text-wrap: pretty;
}

.gh-portal-gift-checkout-section {
    margin-top: 24px;
}

.gh-portal-gift-checkout-label {
    font-size: 1.2rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--grey6);
    margin-bottom: 12px;
}

.gh-portal-gift-checkout .gh-portal-products-pricetoggle {
    margin: 0;
}

.gh-portal-gift-checkout-email .gh-portal-input-labelcontainer {
    margin-bottom: 12px;
}

.gh-portal-gift-checkout-email .gh-portal-input-label {
    font-size: 1.2rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--grey6);
    margin-bottom: 0;
}

.gh-portal-gift-checkout-email .gh-portal-input {
    height: 48px;
    margin-bottom: 0;
}

.gh-portal-gift-checkout-tiers {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.gh-portal-gift-checkout-tier-item {
    background: var(--white);
    border: 1px solid var(--grey11);
    border-radius: 10px;
    overflow: hidden;
    transition: border-color 0.2s ease, background-color 0.2s ease;
}

.gh-portal-gift-checkout-tier-item:hover {
    border-color: var(--grey9);
}

.gh-portal-gift-checkout-tiers.single .gh-portal-gift-checkout-tier-item:hover {
    border-color: var(--grey11);
}

.gh-portal-gift-checkout-tier-item.selected {
    border-color: var(--brandcolor);
    background: color-mix(in srgb, var(--brandcolor) 6%, var(--white));
    box-shadow: 0 0 0 1px var(--brandcolor) inset;
}

.gh-portal-gift-checkout-tier {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    width: 100%;
    background: transparent;
    border: none;
    padding: 16px 20px;
    cursor: pointer;
    text-align: start;
    font: inherit;
    color: inherit;
}

.gh-portal-gift-checkout-tiers.single .gh-portal-gift-checkout-tier {
    cursor: default;
}

.gh-portal-gift-checkout-tier-radio {
    flex-shrink: 0;
    width: 18px;
    height: 18px;
    margin-top: 3px;
    border-radius: 50%;
    border: 1.5px solid var(--grey9);
    background: var(--white);
    position: relative;
}

.gh-portal-gift-checkout-tier-item.selected .gh-portal-gift-checkout-tier-radio {
    border-color: var(--brandcolor);
    background: var(--brandcolor);
}

.gh-portal-gift-checkout-tier-item.selected .gh-portal-gift-checkout-tier-radio::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--white);
    transform: translate(-50%, -50%);
}

.gh-portal-gift-checkout-tier-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.gh-portal-gift-checkout-tier-heading {
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.gh-portal-gift-checkout-tier-name {
    flex: 1;
    font-size: 1.5rem;
    font-weight: 500;
    color: var(--grey0);
}

.gh-portal-gift-checkout-tier-price {
    font-size: 1.5rem;
    font-weight: 500;
    color: var(--grey0);
}

.gh-portal-gift-checkout-tier-description {
    margin: 0;
    margin-top: -2px;
    font-size: 1.4rem;
    line-height: 1.4;
    color: var(--grey4);
}

.gh-portal-gift-checkout-tier-benefits {
    display: grid;
    grid-template-rows: 0fr;
    transition: grid-template-rows 0.3s ease;
    /* overflow + padding-free inner: Chrome leaks the first benefit through
       a 0fr track if the grid item has padding (min-size includes padding). */
    overflow: hidden;
}

.gh-portal-gift-checkout-tier-benefits[data-open="true"] {
    grid-template-rows: 1fr;
}

.gh-portal-gift-checkout-tier-benefits-inner {
    min-height: 0;
    overflow: hidden;
}

.gh-portal-gift-checkout-tier-benefits-inner > .gh-portal-gift-checkout-benefits {
    padding: 0 20px 20px 22px;
}

.gh-portal-gift-checkout-benefits {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.gh-portal-gift-checkout-benefit {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    color: var(--grey1);
    font-size: 1.45rem;
    line-height: 1.4;
}

.gh-portal-gift-checkout-benefit svg {
    width: 14px;
    height: 14px;
    margin-top: 3px;
    color: var(--grey1);
    flex-shrink: 0;
}

.gh-portal-gift-checkout-right-panel .gh-portal-gift-checkout-benefit {
    color: rgba(255, 255, 255, 0.85);
}

/* Checkmark SVG hard-codes its stroke so it can't be themed via color. */
.gh-portal-gift-checkout-right-panel .gh-portal-gift-checkout-benefit svg path {
    stroke: rgba(255, 255, 255, 0.85);
}

.gh-portal-gift-checkout .gh-portal-btn-primary {
    border-radius: 999px;
}

.gh-portal-gift-checkout-cta-wrapper {
    position: sticky;
    bottom: 0;
    margin: 0 0 -64px;
    padding: 32px 0 64px;
    background: linear-gradient(0deg, rgba(var(--whitergb), 1) 60%, rgba(var(--whitergb), 0) 100%);
    z-index: 1;
}

.gh-portal-gift-checkout-cta {
    width: 100%;
    height: 48px;
    font-size: 1.5rem;
    font-weight: 600;
}

.gh-portal-gift-checkout-right {
    position: sticky;
    top: 0;
    align-self: start;
    height: 100vh;
    display: flex;
    padding: 12px 12px 12px 0;
    overflow-y: auto;
}

.gh-portal-gift-checkout-right-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    background:
        linear-gradient(180deg, rgba(0, 0, 0, 0.3) 0%, rgba(0, 0, 0, 0) 100%),
        var(--brandcolor);
    border-radius: 32px;
    padding: 64px 48px 64px;
    overflow-y: auto;
    min-height: 0;
}

.gh-portal-gift-checkout-card-stack {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 280px;
    margin-block: auto;
    flex-shrink: 0;
}

.gh-portal-gift-checkout-card-frame {
    width: 100%;
    transform-style: preserve-3d;
    perspective: 1200px;
    transition: transform 0.3s ease;
    position: sticky;
    top: 0;
    z-index: 1;
}

.gh-portal-gift-checkout-card-stack[data-revealing="true"] .gh-portal-gift-checkout-card-frame {
    transform: rotate(3deg);
}

.gh-portal-gift-checkout-details-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 24px;
    padding: 8px 12px;
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.7);
    font-size: 1.4rem;
    font-weight: 500;
    cursor: pointer;
    transition: color 0.15s ease;
}

.gh-portal-gift-checkout-details-toggle:hover {
    color: rgba(255, 255, 255, 0.95);
}

.gh-portal-gift-checkout-details-toggle svg {
    width: 12px;
    height: 12px;
    transition: transform 0.2s ease;
}

.gh-portal-gift-checkout-details-toggle.is-open svg {
    transform: rotate(-180deg);
}

.gh-portal-gift-checkout-details {
    width: 100%;
    display: grid;
    grid-template-rows: 0fr;
    transition: grid-template-rows 0.3s ease, margin-top 0.3s ease;
    margin-top: 0;
}

.gh-portal-gift-checkout-details[data-open="true"] {
    grid-template-rows: 1fr;
    margin-top: 32px;
}

.gh-portal-gift-checkout-details-inner {
    min-height: 0;
    overflow: hidden;
}

.gh-portal-gift-checkout-details-description {
    margin: 0 0 12px;
    font-size: 1.45rem;
    line-height: 1.4;
    color: rgba(255, 255, 255, 0.85);
}

.gh-portal-gift-checkout-details-description:last-child {
    margin-bottom: 0;
}

.gh-portal-gift-checkout-card {
    position: relative;
    width: 100%;
    max-width: 280px;
    aspect-ratio: 1 / 1.45;
    background:
        linear-gradient(var(--shine-angle, 243.43deg), rgba(255, 255, 255, 0) 3.94%, rgba(255, 255, 255, 0.31) 49.99%, rgba(255, 255, 255, 0) 95.16%),
        linear-gradient(0deg, rgba(255, 255, 255, 0.07), rgba(255, 255, 255, 0.07)),
        var(--brandcolor);
    border-radius: 24px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 24px 48px rgba(var(--blackrgb), 0.08), 0 4px 12px rgba(var(--blackrgb), 0.04);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    isolation: isolate;
    transform-style: preserve-3d;
    will-change: transform;
}

.gh-portal-gift-checkout-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url("${giftCardOrbUrl}");
    background-size: 120% auto;
    background-position: -60% -180%;
    background-repeat: no-repeat;
    pointer-events: none;
    z-index: 0;
    opacity: 0.2;
}

.gh-portal-gift-checkout-card::after {
    content: '';
    position: absolute;
    inset: 0;
    background-image: url("${giftCardNoiseUrl}");
    background-size: 192px 192px;
    background-position: 50% 50%;
    background-repeat: repeat;
    pointer-events: none;
    z-index: 2;
    opacity: 0.1;
}

.gh-portal-gift-checkout-card > * {
    position: relative;
}

.gh-portal-gift-checkout-card-notch {
    position: absolute;
    top: 20px;
    left: 50%;
    width: 56px;
    height: 12px;
    border-radius: 12px;
    background: #000;
    background: color-mix(in srgb, var(--brandcolor) 65%, #000 35%);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 0 rgba(255, 255, 255, 0.18);
    transform: translateX(-50%);
    pointer-events: none;
    z-index: 3;
}

.gh-portal-gift-checkout-card-meta {
    padding: 56px 28px 0;
    z-index: 3;
}

.gh-portal-gift-checkout-card-duration {
    font-size: 2.8rem;
    font-weight: 600;
    color: var(--white);
    letter-spacing: -0.01em;
    line-height: 1.1;
}

.gh-portal-gift-checkout-card-tier {
    margin-top: 6px;
    font-size: 1.5rem;
    color: var(--white);
    line-height: 1.3;
}

.gh-portal-gift-checkout-card-details {
    margin-top: auto;
    padding: 0 28px 24px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 3;
}

.gh-portal-gift-checkout-card-detail-label {
    font-size: 1.2rem;
    color: rgba(255, 255, 255, 0.8);
    margin-bottom: -5px;
}

.gh-portal-gift-checkout-card-detail-value {
    font-size: 1.3rem;
    font-weight: 500;
    color: var(--white);
}

.gh-portal-gift-checkout-card-site {
    padding: 16px 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.gh-portal-gift-checkout-card-site::before {
    content: '';
    position: absolute;
    inset: 0;
    background: var(--white);
    z-index: 1;
}

.gh-portal-gift-checkout-card-site-icon {
    width: 22px;
    height: 22px;
    object-fit: cover;
    position: relative;
    z-index: 3;
}

.gh-portal-gift-checkout-card-site-name {
    font-size: 1.4rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--grey0);
    position: relative;
    z-index: 3;
}


@media (max-width: 880px) {
    .gh-portal-popup-container.full-size.gift,
    .gh-portal-popup-container.full-size.giftSuccess,
    .gh-portal-popup-container.full-size.giftRedemption {
        padding: 0 !important;
    }

    .gh-portal-gift-checkout {
        grid-template-columns: 1fr;
        min-height: 0;
    }

    .gh-portal-gift-checkout-right {
        order: -1;
        position: static;
        height: auto;
        padding: 0;
        overflow: visible;
    }

    .gh-portal-gift-checkout-right-panel {
        padding: 56px 24px 32px;
        border-radius: 0 0 32px 32px;
    }

    .gh-portal-gift-checkout-left {
        padding: 32px 24px 0;
    }

    /* Screens without a sticky CTA wrapper need their own bottom padding. */
    .gh-portal-content.giftSuccess .gh-portal-gift-checkout-left,
    .gh-portal-content.giftRedemption .gh-portal-gift-checkout-left {
        padding-bottom: 24px;
    }

    .gh-portal-gift-checkout-card,
    .gh-portal-gift-checkout-card-stack {
        max-width: 240px;
    }

    .gh-portal-gift-checkout-cta-wrapper {
        bottom: 0;
        margin: 0;
        padding: 32px 0 24px;
    }
}

@media (max-width: 480px) {
    .gh-portal-gift-checkout-header .gh-portal-main-title {
        font-size: 2.6rem;
    }

    .gh-portal-gift-checkout-card-duration {
        font-size: 2rem;
    }
}
`;

function GiftPriceSwitch({selectedInterval, setSelectedInterval}) {
    const {site} = useContext(AppContext);
    const {portal_plans: portalPlans} = site;

    if (!portalPlans.includes('monthly') || !portalPlans.includes('yearly')) {
        return null;
    }

    return (
        <div className={'gh-portal-products-pricetoggle' + (selectedInterval === 'month' ? ' left' : '')}>
            <button
                data-test-button='switch-monthly'
                className={'gh-portal-btn' + (selectedInterval === 'month' ? ' active' : '')}
                onClick={() => setSelectedInterval('month')}
            >
                {t('1 month')}
            </button>
            <button
                data-test-button='switch-yearly'
                className={'gh-portal-btn' + (selectedInterval === 'year' ? ' active' : '')}
                onClick={() => setSelectedInterval('year')}
            >
                {t('1 year')}
            </button>
        </div>
    );
}

export function formatGiftValue(price) {
    const {amount, currency} = price ?? {};
    if (amount === null || amount === undefined || !currency) {
        return '';
    }
    return `${getCurrencySymbol(currency)}${formatNumber(getStripeAmount(amount))}`;
}

function getTierPriceLabel(product, selectedInterval) {
    const activePrice = selectedInterval === 'month' ? product.monthlyPrice : product.yearlyPrice;
    return formatGiftValue(activePrice);
}

const GiftPage = () => {
    const {site, member, brandColor, action, doAction} = useContext(AppContext);
    const [selectedInterval, setSelectedInterval] = useState(null);
    const [selectedProductId, setSelectedProductId] = useState(null);
    const [email, setEmail] = useState('');
    const [errors, setErrors] = useState({});
    const {cardRef, containerProps: cardTiltProps} = useCardTilt();
    const leftRef = useRef(null);
    const innerRef = useRef(null);

    // Vertically center the inner content within the left column by computing
    // the available space and pushing the inner down by half. We recompute on
    // window resize (otherwise the stale offset leaves the right panel
    // overlapping the text), but not on every render — so when benefits change
    // height on tier switch, only the bottom of the column (the CTA) shifts,
    // leaving the title and tier picker anchored.
    // Skipped on mobile (single-column stack) where natural top-aligned flow
    // is what we want; centering would push content under the sticky CTA.
    useLayoutEffect(() => {
        const inner = innerRef.current;
        const left = leftRef.current;
        if (!inner || !left) {
            return;
        }

        const recenter = () => {
            if (typeof window.matchMedia === 'function' && window.matchMedia('(max-width: 880px)').matches) {
                inner.style.marginTop = '';
                return;
            }
            // Reset first so the measurement reflects the natural inner height,
            // not the previously-applied offset.
            inner.style.marginTop = '';
            const leftRect = left.getBoundingClientRect();
            if (leftRect.height === 0) {
                return;
            }
            const leftStyle = window.getComputedStyle(left);
            const pTop = parseFloat(leftStyle.paddingTop);
            const pBottom = parseFloat(leftStyle.paddingBottom);
            const available = leftRect.height - pTop - pBottom;
            const space = available - inner.getBoundingClientRect().height;
            if (space > 0) {
                inner.style.marginTop = `${space / 2}px`;
            }
        };

        recenter();

        let rafId = null;
        const onResize = () => {
            if (rafId !== null) {
                return;
            }
            rafId = window.requestAnimationFrame(() => {
                rafId = null;
                recenter();
            });
        };

        window.addEventListener('resize', onResize, {passive: true});
        return () => {
            window.removeEventListener('resize', onResize);
            if (rafId !== null) {
                window.cancelAnimationFrame(rafId);
            }
        };
    }, []);

    if (!site) {
        return <LoadingPage />;
    }

    const {portal_plans: portalPlans, portal_default_plan: portalDefaultPlan} = site;
    const activeInterval = getActiveInterval({portalPlans, portalDefaultPlan, selectedInterval});
    const products = getAvailableProducts({site}).filter(p => p.type === 'paid');

    const siteIcon = site.icon;
    const siteTitle = site.title || '';

    if (products.length === 0) {
        return (
            <>
                <CloseButton />
                <div className='gh-portal-content gift'>
                    <div className='gh-portal-gift-checkout'>
                        <div className='gh-portal-gift-checkout-left'>
                            <div className='gh-portal-gift-checkout-bg' aria-hidden='true' />
                            <div className='gh-portal-gift-checkout-inner'>
                                <header className='gh-portal-gift-checkout-header'>
                                    <h1 className='gh-portal-main-title'>{t('Gift a membership')}</h1>
                                    <p className='gh-portal-gift-checkout-subtitle'>
                                        {t('Gift subscriptions are not available right now.')}
                                    </p>
                                </header>
                            </div>
                        </div>
                        <div className='gh-portal-gift-checkout-right' aria-hidden='true' />
                    </div>
                </div>
            </>
        );
    }

    const activeProduct = products.find(p => p.id === selectedProductId) || products[0];
    const isSingleTier = products.length === 1;
    const isPurchasing = action === 'checkoutGift:running';
    const hasErrors = Object.values(errors).some(errorMessage => !!errorMessage);
    const isDisabled = isCookiesDisabled() || isPurchasing || hasErrors;
    const isLoggedIn = !!member;

    const emailField = {
        type: 'email',
        value: email,
        placeholder: t('jamie@example.com'),
        label: t('Your email'),
        name: 'email',
        required: true,
        errorMessage: errors.email || ''
    };

    const handleEmailChange = (event) => {
        setErrors(currentErrors => ({
            ...currentErrors,
            email: ''
        }));
        setEmail(event.target.value);
    };

    const handleEmailKeyDown = (event) => {
        if (event.keyCode === 13 && !isPurchasing) {
            handlePurchase(event);
        }
    };

    const handlePurchase = (e) => {
        e.preventDefault();

        if (isPurchasing) {
            return;
        }

        const customerEmail = email.trim();

        if (!isLoggedIn) {
            const formErrors = ValidateInputForm({fields: [{...emailField, value: customerEmail}]});
            const formHasErrors = Object.values(formErrors).some(errorMessage => !!errorMessage);

            setErrors(formErrors);

            if (formHasErrors) {
                return;
            }
        }

        doAction('checkoutGift', {
            tierId: activeProduct.id,
            cadence: activeInterval,
            ...(!isLoggedIn ? {email: customerEmail} : {})
        });
    };

    return (
        <>
            <CloseButton />
            <div className='gh-portal-content gift'>
                <div className='gh-portal-gift-checkout'>
                    <div className='gh-portal-gift-checkout-left' ref={leftRef}>
                        <div className='gh-portal-gift-checkout-bg' aria-hidden='true' />
                        <div className='gh-portal-gift-checkout-inner' ref={innerRef}>
                            <header className='gh-portal-gift-checkout-header'>
                                <h1 className='gh-portal-main-title'>{t('Gift a membership')}</h1>
                                <p className='gh-portal-gift-checkout-subtitle'>
                                    {t('Share a full membership to {siteTitle} with a friend or colleague', {siteTitle})}
                                </p>
                            </header>

                            {!isLoggedIn && (
                                <div className='gh-portal-gift-checkout-section gh-portal-gift-checkout-email'>
                                    <InputField
                                        {...emailField}
                                        onChange={handleEmailChange}
                                        onKeyDown={handleEmailKeyDown}
                                    />
                                </div>
                            )}

                            <div className='gh-portal-gift-checkout-section'>
                                <div className='gh-portal-gift-checkout-label'>{isSingleTier ? t('Membership details') : t('Tier')}</div>
                                <GiftPriceSwitch
                                    selectedInterval={activeInterval}
                                    setSelectedInterval={setSelectedInterval}
                                />
                            </div>

                            <div className='gh-portal-gift-checkout-section'>
                                <div
                                    className={'gh-portal-gift-checkout-tiers' + (isSingleTier ? ' single' : '')}
                                    role={isSingleTier ? undefined : 'radiogroup'}
                                    aria-label={isSingleTier ? undefined : t('Tier')}
                                >
                                    {products.map((product) => {
                                        const isSelected = product.id === activeProduct.id;
                                        const benefits = product.benefits || [];
                                        return (
                                            <div
                                                key={product.id}
                                                className={'gh-portal-gift-checkout-tier-item' + (isSelected && !isSingleTier ? ' selected' : '')}
                                            >
                                                <button
                                                    type='button'
                                                    role={isSingleTier ? undefined : 'radio'}
                                                    aria-checked={isSingleTier ? undefined : isSelected}
                                                    className='gh-portal-gift-checkout-tier'
                                                    onClick={() => setSelectedProductId(product.id)}
                                                    data-test-tier={product.name}
                                                >
                                                    {!isSingleTier && (
                                                        <span className='gh-portal-gift-checkout-tier-radio' aria-hidden='true' />
                                                    )}
                                                    <div className='gh-portal-gift-checkout-tier-content'>
                                                        <div className='gh-portal-gift-checkout-tier-heading'>
                                                            <span className='gh-portal-gift-checkout-tier-name'>{product.name}</span>
                                                            <span className='gh-portal-gift-checkout-tier-price'>{getTierPriceLabel(product, activeInterval)}</span>
                                                        </div>
                                                        {product.description && (
                                                            <p className='gh-portal-gift-checkout-tier-description'>{product.description}</p>
                                                        )}
                                                    </div>
                                                </button>
                                                {benefits.length > 0 && (
                                                    <div
                                                        className='gh-portal-gift-checkout-tier-benefits'
                                                        data-open={isSelected}
                                                        aria-hidden={!isSelected}
                                                    >
                                                        <div className='gh-portal-gift-checkout-tier-benefits-inner'>
                                                            <div className='gh-portal-gift-checkout-benefits'>
                                                                {benefits.map((benefit, idx) => {
                                                                    const key = benefit?.id || `benefit-${idx}`;
                                                                    return (
                                                                        <div className='gh-portal-gift-checkout-benefit' key={key}>
                                                                            <CheckmarkIcon aria-hidden='true' focusable='false' />
                                                                            <span>{benefit.name}</span>
                                                                        </div>
                                                                    );
                                                                })}
                                                            </div>
                                                        </div>
                                                    </div>
                                                )}
                                            </div>
                                        );
                                    })}
                                </div>
                            </div>

                            <div className='gh-portal-gift-checkout-cta-wrapper'>
                                <ActionButton
                                    dataTestId='purchase-gift'
                                    label={t('Continue')}
                                    onClick={handlePurchase}
                                    disabled={isDisabled}
                                    isRunning={isPurchasing}
                                    brandColor={brandColor}
                                    classes='gh-portal-gift-checkout-cta'
                                    style={{width: '100%'}}
                                />
                            </div>
                        </div>
                    </div>

                    <div className='gh-portal-gift-checkout-right' {...cardTiltProps}>
                        <div className='gh-portal-gift-checkout-right-panel'>
                            <div className='gh-portal-gift-checkout-card-stack'>
                                <GiftCard
                                    cardRef={cardRef}
                                    duration={getGiftDurationLabel({cadence: activeInterval, duration: 1})}
                                    tierName={activeProduct.name}
                                    giftValue={getTierPriceLabel(activeProduct, activeInterval)}
                                    siteIcon={siteIcon}
                                    siteTitle={siteTitle}
                                />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </>
    );
};

export default GiftPage;
