<?php

namespace Autogedal\Extend\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\Pricing\Helper\Data as PriceHelper;
use Magento\Framework\Serialize\Serializer\Json;

class ProductCompatibility implements ArgumentInterface
{
    /**
     * @var PriceHelper
     */
    protected $priceHelper;

    /**
     * @var Json
     */
    protected $jsonSerializer;

    /**
     * @param PriceHelper $priceHelper
     * @param Json $jsonSerializer
     */
    public function __construct(
        PriceHelper $priceHelper,
        Json $jsonSerializer
    ) {
        $this->priceHelper = $priceHelper;
        $this->jsonSerializer = $jsonSerializer;
    }

    /**
     * Format price with currency
     *
     * @param float $price
     * @return string
     */
    public function formatPrice($price)
    {
        return $this->priceHelper->currency($price, true, false);
    }

    /**
     * Convert array to JSON
     *
     * @param array $data
     * @return string
     */
    public function toJson($data)
    {
        return $this->jsonSerializer->serialize($data);
    }

    /**
     * Format weight
     *
     * @param float $weight
     * @return string
     */
    public function formatWeight($weight)
    {
        return $weight ? number_format($weight, 2) . ' kg' : '-';
    }
}
