<?php
/**
 * Helper utilities for the string translation admin view.
 *
 * @package WPR_Translate
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Normalize and enrich the data for the string translation view.
 *
 * @param array $data Raw data array from the controller.
 *
 * @return array
 */
function wpr_translate_admin_prepare_strings_view_data( $data ) {
    // Normalize incoming data arrays for the template.
    $strings    = isset( $data['strings'] ) && is_array( $data['strings'] ) ? $data['strings'] : array();
    $languages  = isset( $data['languages'] ) && is_array( $data['languages'] ) ? $data['languages'] : array();
    $filters    = isset( $data['filters'] ) && is_array( $data['filters'] ) ? $data['filters'] : array();
    $domains    = isset( $data['domains'] ) && is_array( $data['domains'] ) ? $data['domains'] : array();
    $pagination = isset( $data['pagination'] ) && is_array( $data['pagination'] ) ? $data['pagination'] : array();

    // Resolve the default language code.
    $default_language_code = '';

    foreach ( $languages as $language ) {
        if ( isset( $language['is_default'], $language['code'] ) && $language['is_default'] ) {
            $default_language_code = (string) $language['code'];
            break;
        }
    }

    if ( '' === $default_language_code && ! empty( $languages ) ) {
        $first_language = reset( $languages );

        if ( isset( $first_language['code'] ) ) {
            $default_language_code = (string) $first_language['code'];
        }
    }

    // Normalize filters and pagination values.
    $status_filter = isset( $filters['status'] ) ? sanitize_text_field( $filters['status'] ) : 'all';
    $search_term   = isset( $filters['search'] ) ? sanitize_text_field( $filters['search'] ) : '';
    $domain_filter = isset( $filters['domain'] ) ? sanitize_text_field( $filters['domain'] ) : '';
    $page_slug     = isset( $data['page_slug'] ) ? sanitize_key( $data['page_slug'] ) : 'wpr-translate-strings';
    $current_page  = isset( $pagination['current_page'] ) ? max( 1, (int) $pagination['current_page'] ) : 1;
    $total_pages   = isset( $pagination['total_pages'] ) ? max( 1, (int) $pagination['total_pages'] ) : 1;
    $per_page      = isset( $pagination['per_page'] ) ? max( 1, (int) $pagination['per_page'] ) : 20;
    $total_items   = isset( $pagination['total'] ) ? max( 0, (int) $pagination['total'] ) : count( $strings );
    $displayed     = count( $strings );
    $range_start   = 0;
    $range_end     = 0;

    if ( '' === $page_slug ) {
        $page_slug = 'wpr-translate-strings';
    }

    $action_args = array(
        'page' => $page_slug,
    );

    if ( 'all' !== $status_filter ) {
        $action_args['status'] = $status_filter;
    }

    if ( '' !== $search_term ) {
        $action_args['s'] = $search_term;
    }

    if ( '' !== $domain_filter ) {
        $action_args['domain'] = $domain_filter;
    }

    if ( $current_page > 1 ) {
        $action_args['strings_page'] = $current_page;
    }

    // Build the form action URL preserving current filters.
    $form_action = add_query_arg( $action_args, admin_url( 'admin.php' ) );

    // Compute the displayed range for pagination summary.
    if ( $total_items > 0 && $displayed > 0 ) {
        $range_start = ( ( $current_page - 1 ) * $per_page ) + 1;
        $range_end   = $range_start + $displayed - 1;

        if ( $range_end > $total_items ) {
            $range_end = $total_items;
        }
    }

    // Sanitize domain labels for output.
    $prepared_domains = array();

    foreach ( $domains as $context_value => $label ) {
        $prepared_domains[ $context_value ] = sanitize_text_field( $label );
    }

    return array(
        'strings'                => $strings,
        'languages'              => $languages,
        'filters'                => array(
            'status' => $status_filter,
            'search' => $search_term,
            'domain' => $domain_filter,
        ),
        'domains'                => $prepared_domains,
        'pagination'             => array(
            'current_page' => $current_page,
            'total_pages'  => $total_pages,
            'per_page'     => $per_page,
            'total'        => $total_items,
        ),
        'default_language_code'  => $default_language_code,
        'form_action'            => $form_action,
        'range_start'            => $range_start,
        'range_end'              => $range_end,
        'displayed'              => $displayed,
        'page_slug'              => $page_slug,
        'page_title'             => isset( $data['page_title'] ) ? sanitize_text_field( $data['page_title'] ) : '',
        'page_description'       => isset( $data['page_description'] ) ? wp_kses_post( $data['page_description'] ) : '',
    );
}
