<?php

use League\HTMLToMarkdown\HtmlConverter;

/* SECTIONS HELPERS */
if( !function_exists( 'section_must_be' ) ){

    function section_must_be( array|string $sections, string $item ): bool
    {
        if( is_array($sections) ){
            return in_array( $item, $sections );
        }
        return $sections === $item;
    }

}

if( !function_exists( 'section_must_not_be' ) ){

    function section_must_not_be( array|string $sections, string $item ): bool
    {
        if( is_array($sections) ){
            return ! in_array( $item, $sections );
        }
        return $sections !== $item;
    }

}

if( !function_exists('userTheme' ) ){
    function userTheme(): string {
        if( !\Auth::guest() ){
            return \Auth::user()->style_variation ?? 'default';
        }
        return \Illuminate\Support\Facades\Cookie::get('xf_style_variation', 'default');
    }
}

if( !function_exists( 'databaseRoute' ) ) {

    function databaseRoute(array $params = []): string
    {
        $defaults = [
            'types' => [],
            'platforms' => [],
            'genres' => [],
            'games' => [],
            'statuses' => [],
            'authors' => [],
            'authorsMode' => 'or',
            'languages' => [],
            'languagesMode' => 'or',
            'modifications' => [],
            'modificationsMode' => 'or',
            'categories' => [],
            'categoriesMode' => 'or',
            'systems' => [],
            'systemsMode' => 'or',
            'levels' => [],
            'sort' => 'created_at',
            'dir' => 'desc',
            's' => ''
        ];

        $query = array_filter(
            array_merge($defaults, $params),
            fn($v, $k) => match (true) {
                is_array($v) => !empty($v),
                in_array($k, ['authorsMode', 'languagesMode', 'modificationsMode', 'categoriesMode', 'systemsMode']) => $v !== 'or',
                $k === 'sort' => $v !== 'created_at',
                $k === 'dir' => $v !== 'desc',
                default => $v !== '',
            },
            ARRAY_FILTER_USE_BOTH
        );

        return route('entries.index', $query);
    }

}

if( !function_exists( 'newsRoute' ) ){
    function newsRoute(array $params = []): string
    {
        $defaults = [
            'categories' => [],
            'sort' => 'created_at',
            'dir' => 'desc',
            's' => ''
        ];

        $query = array_filter(
            array_merge($defaults, $params),
            fn($v, $k) => match (true) {
                is_array($v) => !empty($v),
                $k === 'sort' => $v !== 'created_at',
                $k === 'dir' => $v !== 'desc',
                default => $v !== '',
            },
            ARRAY_FILTER_USE_BOTH
        );

        return route('news.index', $query);
    }
}

if( !function_exists('reviewsRoute') ){
    function reviewsRoute( array $params = [] ): string
    {
        $defaults = [
            'entryId' => null,
            'rating' => null,
            'sort' => 'created_at',
            'dir' => 'desc',
        ];

        $query = array_filter(
            array_merge($defaults, $params),
            fn($v,$k) => match(true){
                is_array($v) => !empty($v),
                $k === 'sort' => $v !== 'created_at',
                $k === 'dir' => $v !== 'desc',
                default => $v !== '',
            },
            ARRAY_FILTER_USE_BOTH
        );

        return route('reviews.index', $query );
    }
}
