<?php
/**
 * Maho
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Core_Block_Template $this */

$_product = $this->getProduct();

if (Mage::helper('configurableswatches')->isEnabled() && $_product && $_product->getId()
    && ($_attrValues = $_product->getListSwatchAttrValues()) && count($_attrValues) > 0):
    $_attrStockValues = $_product->getListSwatchAttrStockValues();
    $_swatchAttribute = Mage::helper('configurableswatches/productlist')->getSwatchAttribute();

    $_dimHelper = Mage::helper('configurableswatches/swatchdimensions');
    $_swatchInnerWidth = $_dimHelper->getInnerWidth(Mage_ConfigurableSwatches_Helper_Swatchdimensions::AREA_LISTING);
    $_swatchInnerHeight = $_dimHelper->getInnerHeight(Mage_ConfigurableSwatches_Helper_Swatchdimensions::AREA_LISTING);
    $_swatchOuterWidth = $_dimHelper->getOuterWidth(Mage_ConfigurableSwatches_Helper_Swatchdimensions::AREA_LISTING);
    $_swatchOuterHeight = $_dimHelper->getOuterHeight(Mage_ConfigurableSwatches_Helper_Swatchdimensions::AREA_LISTING);
?>
<ul class="configurable-swatch-list configurable-swatch-<?= $_swatchAttribute->getAttributeCode() ?> clearfix">
    <?php foreach ($_attrValues as $_optionValue => $_optionLabel): ?>
        <?php
        $_optionCode = Mage::helper('configurableswatches')->getHyphenatedString($_optionLabel);
        $_swatchUrl = Mage::helper('configurableswatches/productimg')->getSwatchUrl($_product, $_optionLabel, $_swatchInnerWidth, $_swatchInnerHeight, $_swatchType);
        $_hasImage = !empty($_swatchUrl);
        $_liClasses = [];
        $_aClass = 'swatch-link swatch-link-' . $_swatchAttribute->getId();
        if ($_hasImage) {
            if ($_swatchType == 'media') {
                $_liClasses[] = 'is-media';
            }
            $_aClass .= ' has-image';
        } elseif (strlen($_optionLabel) > 3) {
            $_liClasses[] = 'wide-swatch';
        }
        if (Mage::helper('configurableswatches/productlist')->swatchMatchesFilter($_optionValue)) {
            $_liClasses[] = 'filter-match';
        }
        $_liClass = (!empty($_liClasses)) ? ' ' . implode(' ', $_liClasses) : '';
        ?>
    <li class="option-<?= $_optionCode ?><?= $_liClass ?>" data-product-id="<?= $_product->getId() ?>" data-option-label="<?= $_optionLabel ?>">
        <a href="javascript:void(0)" name="<?= $_optionCode ?>" class="<?= $_aClass ?>" title="<?= $_optionLabel ?>"
               style="height: <?= $_swatchOuterHeight ?>px; <?php if (!$_hasImage): ?>min-<?php endif ?>width: <?= $_swatchOuterWidth ?>px;">
            <span class="swatch-label" style="height: <?= $_swatchInnerHeight ?>px; <?php if (!$_hasImage): ?>min-<?php endif ?>width: <?= $_swatchInnerWidth ?>px; line-height: <?= $_swatchInnerHeight ?>px;">
            <?php if ($_hasImage): ?>
                <img src="<?= $_swatchUrl ?>" alt="<?= $_optionLabel ?>" width="<?= $_swatchInnerWidth ?>" height="<?= $_swatchInnerHeight ?>" />
            <?php else: ?>
                <?= $_optionLabel ?>
            <?php endif ?>
            </span>
            <?php if (isset($_attrStockValues[$_optionValue]) && !$_attrStockValues[$_optionValue]): ?>
                <span class="x" style="display: none"></span>
            <?php endif ?>
        </a>
    </li>
    <?php endforeach ?>
</ul>
<?php endif ?>
