<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Hyva\Theme\ViewModel\ProductPage;

/** @var Escaper $escaper */
/** @var Template $block */
/** @var ViewModelRegistry $viewModels */

/** @var ProductPage $productViewModel */
$productViewModel = $viewModels->require(ProductPage::class);
/** @var Product $product */
$product = $productViewModel->getProduct();

if (!$product->getDescription()) {
    return;
}

$cssClasses = $block->getData('css_classes') ?? '';


$rawData = $product->getData('cd1');
$extracustomdescription = $product->getData('extracustomdescription');
$accordionItems = [];
?>




    <!--card w-full overflow-hidden p-4-->
    <div class=" w-full overflow-hidden p-4">
        <div class="prose max-w-none break-words overflow-hidden">
            <div class="a <?= $escaper->escapeHtmlAttr($cssClasses) ?>">
                <?= $productViewModel->productAttributeHtml($product, $product->getDescription(), 'description') ?>
            </div>
        </div>
    </div>


<?php
$rawData = $product->getData('cd1');
$accordionItems = [];

if ($rawData) {
    $lines = array_map('trim', explode("\n", $rawData));
    for ($i = 0; $i < count($lines); $i += 2) {
        $title = $lines[$i] ?? '';
        $desc = $lines[$i + 1] ?? '';
        if ($title || $desc) {
            $accordionItems[] = ['title' => $title, 'desc' => $desc];
        }
    }
}
?>

<?php if (!empty($accordionItems)): ?>



    <?php
    $rawContent = $product->getData('cd1');

// Normalize line endings
    $lines = preg_split('/\r\n|\r|\n/', $rawContent);

    $accordionItems = [];
    $currentTitle = null;
    $currentDesc = '';

    foreach ($lines as $line) {
        $trimmedLine = trim($line);

        // Check if line has NO HTML tags (title line)
        if ($trimmedLine !== '' && $trimmedLine === strip_tags($trimmedLine)) {
            // Save previous item
            if ($currentTitle !== null) {
                $accordionItems[] = [
                    'title' => $currentTitle,
                    'desc' => $currentDesc,
                ];
            }

            // Start new title
            $currentTitle = $trimmedLine;
            $currentDesc = '';
        } else {
            // HTML content line — append
            $currentDesc .= $line . "\n";
        }
    }

// Add last item if exists
    if ($currentTitle !== null) {
        $accordionItems[] = [
            'title' => $currentTitle,
            'desc' => $currentDesc,
        ];
    }
    ?>

    <!-- Alpine.js Accordion -->
    <!-- Alpine.js Accordion -->
    <div x-data="{ open: [] }" class="w-full mx-auto flex flex-col">
        <?php foreach ($accordionItems as $index => $item): ?>
            <?php $id = $index + 1; ?>
            <div class="overflow-hidden border-b border-gray-300">
                <!-- Accordion Header -->
                <div
                    @click="
                    const scrollY = window.scrollY;
                    if (open.includes(<?= $id ?>)) {
                        open = open.filter(i => i !== <?= $id ?>);
                    } else {
                        open.push(<?= $id ?>);
                    }
                    $nextTick(() => window.scrollTo({ top: scrollY, behavior: 'auto' }));
                "
                    class="p-2 flex items-center cursor-pointer bg-gray-100"
                >
                    <div class="w-6 text-lg font-bold text-gray-600">
                        <span x-text="open.includes(<?= $id ?>) ? '-' : '+'"></span>
                    </div>
                    <div class="font-bold"><?= htmlspecialchars($item['title']) ?></div>
                </div>

                <!-- Accordion Content -->
                <div
                    x-show="open.includes(<?= $id ?>)"
                    x-collapse
                    x-cloak
                    class="md:p-4 scroll-mt-24 transition-all duration-300 ease-in-out overflow-hidden"
                >
                    <div x-transition x-show="open.includes(<?= $id ?>)" class="p-4">
                        <?= $item['desc'] ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>


<?php endif; ?>
    <br/>

<?php

echo $extracustomdescription;
?>
