<?php

declare(strict_types=1);

/*
 * This file is part of vonRotenberg Shopware API Bundle.
 *
 * (c) vonRotenberg
 *
 * @license proprietary
 */

namespace vonRotenberg\ShopwareApiBundle\Helper;

use Contao\System;

class ShopwareMappings
{
    protected $mappings = [];

    protected function getMappings()
    {
        if (!isset($this->mappings['properties']))
        {
            $this->mappings['properties'] = System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.properties');
        }
        if (!isset($this->mappings['property_groups']))
        {
            $this->mappings['property_groups'] = System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.property_groups');
        }
        if (!isset($this->mappings['units']))
        {
            $this->mappings['units'] = System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.units');
        }

        return $this->mappings;
    }

    public function getPropertyIdByNameAndGroup(string $name, string $group, bool $useGroupUuid = false): ?string
    {
        $mappings = $this->getMappings();
        $propertyId = null;

        if (!$useGroupUuid)
        {
            $group = array_search($group, $mappings['property_groups']);
        }

        if ($group !== false && ($property = array_search($name, $mappings['properties'][$group])) !== false)
        {
            $propertyId = $property;
        }

        return $propertyId;
    }

    public function getPropertyGroupIdByName(string $name): ?string
    {
        $mappings = $this->getMappings();
        $groupId = null;

        if (($group = array_search($name, $mappings['property_groups'])) !== false)
        {
            $groupId = $group;
        }

        return $groupId;
    }

    public function getUnitIdByName(string $name): ?string
    {
        $mappings = $this->getMappings();
        $unitId = null;

        if (($unit = array_search($name, $mappings['units'])) !== false)
        {
            $unitId = $unit;
        }

        return $unitId;
    }
}