<?php

namespace KupShop\ElninoBundle\Email;

class OrderMessageEmail extends \KupShop\KupShopBundle\Email\OrderMessageEmail
{
    public function getPackages(): ?array
    {
        if (isset($this->order->packages)) {
            return $this->order->packages;
        }

        $this->order->packages = [];

        if ($this->order->package_id) {
            $packages = $this->order->getData('packages');
            if ($packages && count($packages) > 1) {
                // pokud je baliku vic nez jeden, to znamena ze zasilka byla rozdelena na vice baliku,
                // prvni vygenerovany balik byl stornovan => musime ho taky zahodit
                array_shift($packages);
            } else {
                $packages = [$this->order->package_id];
            }
            foreach ($packages as $package_id) {
                $this->order->packages[$package_id] = $package_id;
                $link = $this->order->getDeliveryType()->getDelivery()->getTrackAndTraceLink($package_id, $this->order);
                if ($link) {
                    $link = "<a href=\"{$link}\">{$package_id}</a>";
                    $this->order->packages[$package_id] = $link;
                }
            }
        }

        return $this->order->packages;
    }
}
