<?php

namespace Src\Common\Infrastructure;

trait TraitStorage
{

    protected function pluck(string $key, array $array): array
    {
        $result = [];
        $keySegments = explode('.', $key);
        foreach ($keySegments as $keySegment) {
            $result = [];
            foreach ($array as $item) {
                if (!key_exists($keySegment, $item)) {
                    continue;
                }
                if (is_array($item[$keySegment])) {
                    $result = array_merge($result, $item[$keySegment]);
                } else {
                    $result[] = $item[$keySegment];
                }
            }
            $array = $result;
        }
        return array_unique($result);
    }

    protected function arrayValues(array $keys, array $array): array
    {
        $result = [];
        foreach ($keys as $key) {
            if (key_exists($key, $array)) {
                $result[] = $array[$key];
            }
        }
        return $result;
    }

    public function groupBy(string $key, array $array, ?string $idField = null): array
    {
        $result = [];
        foreach ($array as $item) {
            $i = $item[$key];
            if ($idField) {
                $id = $item[$idField];
                $result[$i][$id] = $item;
            } else {
                $result[$i][] = $item;
            }
        }
        return $result;
    }

    public function appendFromRelations(array $array, array $relations, string $arrayKey, string $relKey, array $fields): array
    {
        $result = array_map(function ($item) use ($arrayKey, $relations, $relKey, $fields) {
            $arrayId = $item[$arrayKey];
            foreach ($relations as $rel) {
                if ($rel[$relKey] == $arrayId) {
                    foreach ($fields as $field) {
                        $item[$field] = $rel[$field];
                    }
                }
            }
            return $item;
        }, $array);

        return $result;
    }

    public function _appendFromRelations(array $array, array $relations, string $arrayKey, string $relKey, array $fields): array
    {
        $result = array_map(function ($item) use ($arrayKey, $relations, $relKey, $fields) {
            $arrayId = $item[$arrayKey];
            foreach ($relations as $rel) {
                if ($rel[$relKey] == $arrayId) {
                    foreach ($fields as $alias => $field) {
                        $item[$alias][] = $rel[$field];
                    }
                }
            }
            return $item;
        }, $array);

        return $result;
    }
}
