<?php

namespace App\Domain\Tags\Action;

use Illuminate\Support\Str;
use App\Domain\Tags\Models\Tag;

class CreateTagAction
{
    public function __invoke(Array $tags)
    {
        $tagIds = [];
        $tags = array_map(function($tag){
            $tag = trim(strip_tags($tag));
            //$tag = mb_strtolower($tag);
            //$tag = mb_ucfirst($tag);
            return $tag;
        }, $tags);
        $tags = array_unique($tags);

        foreach ($tags as $tag) {
            $slug = Str::slug($tag, '-');

            $tagDB = Tag::firstOrCreate(
                ['slug' => $slug],
                ['name' => $tag]
            );

            $tagIds[] = $tagDB->id;
        }

        return $tagIds;
    }

    protected function create()
    {

    }
}
