<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Libraries;

use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\User;
use App\Traits\Memoizes;
use App\Transformers\BeatmapDiscussionTransformer;
use App\Transformers\BeatmapTransformer;
use App\Transformers\UserCompactTransformer;
use Illuminate\Pagination\Paginator;

class BeatmapsetDiscussionsBundle extends BeatmapsetDiscussionsBundleBase
{
    use Memoizes;

    private const DISCUSSION_WITHS = ['beatmapDiscussionVotes', 'beatmapset', 'startingPost'];

    private $searchParams;

    public function getData()
    {
        return $this->getDiscussions();
    }

    public function getSearchParams()
    {
        return $this->searchParams;
    }

    public function toArray()
    {
        // TODO: beatmapset nested include should be removed (should be moved to side load);
        // currently left here as some components assume beatmapset is always nested.
        static $discussionIncludes = ['starting_post', 'beatmapset', 'current_user_attributes'];

        return [
            'beatmaps' => json_collection($this->getBeatmaps(), new BeatmapTransformer()),
            'cursor' => $this->getCursor(),
            'discussions' => json_collection($this->getDiscussions(), new BeatmapDiscussionTransformer(), $discussionIncludes),
            'included_discussions' => json_collection($this->getRelatedDiscussions(), new BeatmapDiscussionTransformer(), $discussionIncludes),
            'reviews_config' => BeatmapsetDiscussionReview::config(),
            'users' => json_collection($this->getUsers(), new UserCompactTransformer(), ['groups']),
        ];
    }

    private function getBeatmaps()
    {
        return $this->memoize(__FUNCTION__, function () {
            // using all beatmaps of the beatmapsets for the beatmap selector when editing.
            $beatmapsetIds = $this->getDiscussions()->pluck('beatmapset_id')->unique()->values();

            return Beatmap::whereIn('beatmapset_id', $beatmapsetIds)->get();
        });
    }

    private function getDiscussions()
    {
        return $this->memoize(__FUNCTION__, function () {
            ['query' => $query, 'params' => $this->searchParams] = BeatmapDiscussion::search($this->params);

            $discussions = $query->with(static::DISCUSSION_WITHS)->limit($this->searchParams['limit'] + 1)->get();

            $this->paginator = new Paginator(
                $discussions,
                $this->searchParams['limit'],
                $this->searchParams['page'],
                [
                    'path' => Paginator::resolveCurrentPath(),<commit_msg>Include deleted beatmap in discussion bundle<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Libraries;

use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\User;
use App\Traits\Memoizes;
use App\Transformers\BeatmapDiscussionTransformer;
use App\Transformers\BeatmapTransformer;
use App\Transformers\UserCompactTransformer;
use Illuminate\Pagination\Paginator;

class BeatmapsetDiscussionsBundle extends BeatmapsetDiscussionsBundleBase
{
    use Memoizes;

    private const DISCUSSION_WITHS = ['beatmapDiscussionVotes', 'beatmapset', 'startingPost'];

    private $searchParams;

    public function getData()
    {
        return $this->getDiscussions();
    }

    public function getSearchParams()
    {
        return $this->searchParams;
    }

    public function toArray()
    {
        // TODO: beatmapset nested include should be removed (should be moved to side load);
        // currently left here as some components assume beatmapset is always nested.
        static $discussionIncludes = ['starting_post', 'beatmapset', 'current_user_attributes'];

        return [
            'beatmaps' => json_collection($this->getBeatmaps(), new BeatmapTransformer()),
            'cursor' => $this->getCursor(),
            'discussions' => json_collection($this->getDiscussions(), new BeatmapDiscussionTransformer(), $discussionIncludes),
            'included_discussions' => json_collection($this->getRelatedDiscussions(), new BeatmapDiscussionTransformer(), $discussionIncludes),
            'reviews_config' => BeatmapsetDiscussionReview::config(),
            'users' => json_collection($this->getUsers(), new UserCompactTransformer(), ['groups']),
        ];
    }

    private function getBeatmaps()
    {
        return $this->memoize(__FUNCTION__, function () {
            // using all beatmaps of the beatmapsets for the beatmap selector when editing.
            $beatmapsetIds = $this->getDiscussions()->pluck('beatmapset_id')->unique()->values();

            return Beatmap::withTrashed()->whereIn('beatmapset_id', $beatmapsetIds)->get();
        });
    }

    private function getDiscussions()
    {
        return $this->memoize(__FUNCTION__, function () {
            ['query' => $query, 'params' => $this->searchParams] = BeatmapDiscussion::search($this->params);

            $discussions = $query->with(static::DISCUSSION_WITHS)->limit($this->searchParams['limit'] + 1)->get();

            $this->paginator = new Paginator(
                $discussions,
                $this->searchParams['limit'],
                $this->searchParams['page'],
                [
                    'path' => Paginator::resolveCurrentPath(),
{'fraction_non_alphanumeric': 0.12286324786324786, 'fraction_numerical': 0.0010683760683760685, 'mean_word_length': 3.258529188779378, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3216413', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1480, 'n_words': 346}