<?php

namespace App\Http\Livewire\Categories;

use Livewire\Component;

class ColorPicker extends Component
{
    public $selectedColor = 'gray';
    public $label = 'Color';
    public $required = false;
    public $previewName = 'Category name';

    protected $listeners = ['colorSelected'];

    public function mount($color = 'gray', $label = 'Color', $required = false, $previewName = 'Category name')
    {
        $this->selectedColor = $color;
        $this->label = $label;
        $this->required = $required;
        $this->previewName = $previewName;
    }

    public function updatedSelectedColor($value)
    {
        $this->dispatch('colorUpdated', $value);
    }

    public function updatePreviewName($name)
    {
        $this->previewName = $name ?: __('Category name');
    }

    public function getAvailableColorsProperty()
    {
        $colors = [
            'slate', 'gray', 'zinc', 'neutral', 'stone',
            'red', 'orange', 'amber', 'yellow', 'lime',
            'green', 'emerald', 'teal', 'cyan', 'sky',
            'blue', 'indigo', 'violet', 'purple', 'fuchsia',
            'pink', 'rose'
        ];

        return collect($colors)->map(function ($color) {
            return [
                'value' => $color,
                'label' => ucfirst($color)
            ];
        })->toArray();
    }

    public function render()
    {
        return view('livewire.categories.color-picker');
    }
}
