<?php

function reorder_shop_loop_hooks() {
    // Remove existing hooks
    remove_action( 'woocommerce_before_shop_loop', 'woocommerce_output_all_notices', 10 );
    remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
    remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

    // Add hooks back in new order
    add_action( 'woocommerce_before_shop_loop', 'woocommerce_output_all_notices', 10 );    
    add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 20 );     
    add_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 30 );   
    add_action( 'rc_filter_items', 'render_rc_filter_items', 10 );          
}
add_action( 'init', 'reorder_shop_loop_hooks' );


function render_rc_filter_items() {
    $selected_filters = [];
    if (method_exists(RC()->filter(), 'get_selected_filters')) {
        $selected_filters = RC()->filter()->get_selected_filters() ?? [];
    } else {
        return false;
    }

    if(empty($selected_filters)){
        return false;
    }
    echo '<div id="applied-filters">'.$selected_filters.'</div>';
}


add_filter('woocommerce_catalog_orderby', 'wc_customize_product_sorting');

function wc_customize_product_sorting($sorting_options){
    $sorting_options = array(
        'menu_order' => __( 'Odporúčame', 'woocommerce' ),
        'price'      => __( 'Najlacnejšie', 'woocommerce' ),
        'price-desc' => __( 'Najdrahšie', 'woocommerce' ),
        'popularity' => __( 'Najpredávanejšie', 'woocommerce' ),
    );

    return $sorting_options;
}

add_filter('ngettext_with_context', function($translation, $single, $plural, $number, $context, $domain) {
    if ($domain !== 'woocommerce') {
        return $translation;
    }

    if ($single === 'Showing all %d result' && $plural === 'Showing all %d results') {
        return $number === 1 
            ? '%d položiek celkom'
            : '%d položiek celkom';
    }

    if ($single === 'Showing %1$d&ndash;%2$d of %3$d result' && $plural === 'Showing %1$d&ndash;%2$d of %3$d results') {
        return $number === 1 
            ? '%3$d položiek celkom'
            : '%3$d položiek celkom';
    }

    return $translation;
}, 10, 6);
