<?php
namespace OxygenApp\Utils;
if ( ! defined( 'ABSPATH' ) ) exit;

use OxygenApp\Models\AdminPlaylist;
use OxygenApp\Models\BackgroundSound;
use OxygenApp\Utils\UserUtil;

class AdminPlaylistUtils {

    static function getPlaylists(): array {

        $playlists = AdminPlaylist::getAll();
        if (!is_array($playlists)) {
            return [];
        }

        if (empty($playlists)) {
            return [];
        }

        $playlists = array_map(function ($playlist) {
            return AdminPlaylist::getPretty($playlist);
        }, $playlists);


        $out = [];
        foreach ($playlists as $key => $playlist) {

            $bkgSounds = new BackgroundSound();
            $soundsPosts = $bkgSounds->getAll(['term_id' => $playlist['term_id']]);

            if (is_array($soundsPosts) && sizeof($soundsPosts) > 0) {
                $out[] = [...$playlist, 'sounds' => array_map(function ($soundPost) {
                        return BackgroundSound::getPretty($soundPost);
                    }, $soundsPosts)];
            }
        }

        return $out;
    }
}