<?php

namespace CouponsPlus\App\Coupon\Attributes\Data;

use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\Original\Characters\StringManager;
use CouponsPlus\App\Coupon\Attributes\Abilities\AttributeData;
use CouponsPlus\App\Coupon\Attributes\Abilities\WithSelectedValues;

class LocalAttributeData implements AttributeData, WithSelectedValues
{

    /**
     * @var string
     */
    protected $slug;
    /**
     * @var array
     */
    protected $values;
    /**
     * @var array
     */
    protected $selectedValues;

    public function __construct(string $slug, array $values)
    {
        $this->slug = $slug;
        $this->values = $values;
    }

    public function useValues(array $values)
    {
        $this->selectedValues = $values;
    }

    public function getId() : string
    {
        return $this->slug;
    }

    public function getKey(): StringManager
    {
        return new StringManager($this->getId());
    }

    /**
     * @return Collection of LocalAttributeValueData
     */
    public function getValues(): Collection
    {
        return Collection::create($this->getValuesToUse())->map(function ($value) {
            if ($value instanceof LocalAttributeValueData) {
                return $value;
            }
            return new LocalAttributeValueData($value);
        });
    }

    /**
     * @return array
     */
    protected function getValuesToUse(): array
    {
        if ($this->selectedValues) {
            return $this->selectedValues;
        }

        return $this->values;
    }
}