<?php
/**
 * Cart conflict interstitial: Replace vs Merge.
 *
 * Rendered by URL_Handler::render_cart_conflict_prompt() via template_redirect when a
 * shopper opens a share/saved-cart link while their live cart already has items.
 * Environment-agnostic: it's its own themed page, never injected into the classic OR
 * block Cart render tree, so it behaves identically on FSE and non-FSE sites.
 *
 * Available variables:
 *   string $token        Share token.
 *   string $replace_url  Nonce-signed link that replaces the current cart.
 *   string $merge_url    Nonce-signed link that merges into the current cart.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// get_header() no-ops on FSE themes (no header.php there) — use our wrapper instead.
lean_csns_render_page_header();
?>

<div class="woocommerce">
    <div class="lean-csns-cart-conflict">

        <div class="lean-csns-cart-conflict__title">
            <?php esc_html_e( 'You have a saved cart', 'lean-cart-share-and-save' ); ?>
        </div>
        <div class="lean-csns-cart-conflict__desc">
            <?php esc_html_e( 'You left a few items in your cart last time, and this link brings a few more. Want to keep both together?', 'lean-cart-share-and-save' ); ?>
        </div>

        <div class="lean-csns-cart-conflict__actions">
            <div class="lean-csns-cart-conflict__choice">
                <a href="<?php echo esc_url( $merge_url ); ?>" class="button wp-element-button lean-csns-cart-conflict__btn">
                    <span><?php esc_html_e( 'Combine cart', 'lean-cart-share-and-save' ); ?></span>
                    <svg class="lean-csns-cart-conflict__arrow" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>
                </a>
                <span class="lean-csns-cart-conflict__caption">
                    <?php esc_html_e( 'Keeps everything you already picked.', 'lean-cart-share-and-save' ); ?>
                </span>
            </div>
            <div class="lean-csns-cart-conflict__choice">
                <a href="<?php echo esc_url( $replace_url ); ?>" class="button wp-element-button lean-csns-cart-conflict__btn">
                    <span><?php esc_html_e( 'Replace carts', 'lean-cart-share-and-save' ); ?></span>
                    <svg class="lean-csns-cart-conflict__arrow" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>
                </a>
                <span class="lean-csns-cart-conflict__caption">
                    <?php esc_html_e( 'Clears what\'s in your cart first.', 'lean-cart-share-and-save' ); ?>
                </span>
            </div>
        </div>

    </div>
</div>

<style>
/* Full-bleed escape so the prompt centers regardless of theme layout.
   NOTE: some FSE themes set overflow:hidden on a parent which can clip this. */
.woocommerce:has(.lean-csns-cart-conflict) {
    width: 100vw;
    position: relative;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 60vh;
    padding: 40px 0;
}
.lean-csns-cart-conflict { max-width: 560px; width: 100%; padding: 0 16px; text-align: center; }
.lean-csns-cart-conflict__title { font-size: 1.5rem; font-weight: 700; line-height: 1.3; margin-bottom: 8px; }
.lean-csns-cart-conflict__desc { color: #666; margin-bottom: 24px; }

/* Two buttons side by side (wrap to stacked on narrow screens); caption sits below each.
   No background/color set on the button — the theme's .button supplies those. */
.lean-csns-cart-conflict__actions { display: flex; flex-direction: row; flex-wrap: wrap; gap: 12px; align-items: flex-start; }
.lean-csns-cart-conflict__choice { flex: 1 1 0; min-width: 150px; display: flex; flex-direction: column; align-items: center; }
/* Extra ancestor classes raise specificity to beat the theme's own .button rules without !important. */
.lean-csns-cart-conflict .lean-csns-cart-conflict__choice .lean-csns-cart-conflict__btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    box-sizing: border-box;
    text-align: center;
    padding: 14px 18px;
    height: auto;
    font-weight: 700;
    font-size: 15px;
}
.lean-csns-cart-conflict__arrow { flex: 0 0 auto; display: block; }
.lean-csns-cart-conflict__caption { font-size: 12px; color: #6b7280; margin-top: 8px; line-height: 1.35; }
</style>

<?php lean_csns_render_page_footer(); ?>
