<?php
declare(strict_types=1);

namespace Autogedal\Labels\Plugin\Catalog\Model;

use Autogedal\Labels\Model\Attribute\ActiveAttributeCodesProvider;
use Magento\Catalog\Model\Config as CatalogConfig;

class ConfigPlugin
{
    private ActiveAttributeCodesProvider $activeAttributeCodesProvider;

    public function __construct(ActiveAttributeCodesProvider $activeAttributeCodesProvider)
    {
        $this->activeAttributeCodesProvider = $activeAttributeCodesProvider;
    }

    /**
     * Add only the custom product attributes actually used by active labels.
     *
     * @param CatalogConfig $subject
     * @param string[] $result
     * @return string[]
     */
    public function afterGetProductAttributes(CatalogConfig $subject, array $result): array
    {
        $customAttributes = $this->activeAttributeCodesProvider->getAttributeCodes();
        if ($customAttributes === []) {
            return $result;
        }

        return array_values(array_unique(array_merge($result, $customAttributes)));
    }
}
