<?php

namespace Mtc\Shop\Services;

use Mtc\Shop\ActiveSubstance;

class ActiveSubstanceService
{
    public static function syncItemSubstances($item, array $names): void
    {
        $names = array_filter(array_map('trim', $names));

        $ids = collect($names)->map(function ($name) {
            return ActiveSubstance::firstOrCreate(['name' => $name])->id;
        })->all();

        $item->active_substances()->sync($ids);
    }
}
