<?php
/**
 * Helper utilities for permalink adjustments.
 *
 * @package WPR_Translate
 */

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

if ( ! function_exists( 'wpr_translation_strip_language_slug_from_url' ) ) {
    /**
     * Removes a language slug from the provided URL when it should remain implicit.
     *
     * @param string $url      Original permalink.
     * @param array  $language Language definition payload.
     *
     * @return string
     */
    function wpr_translation_strip_language_slug_from_url( $url, $language ) {
        if ( empty( $language ) ) {
            // No language context means no changes.
            return $url;
        }

        if ( ! is_array( $language ) ) {
            // Build a minimal payload when a string is passed in.
            $language = array( 'code' => $language );
        }

        // Treat the requested language as default to remove its slug.
        $language['is_default'] = true;

        return wpr_translation_localize_url_for_language( $url, $language );
    }
}

if ( ! function_exists( 'wpr_translation_get_known_language_slugs' ) ) {
    /**
     * Returns all known language slugs and codes used in URLs.
     *
     * @return array
     */
    function wpr_translation_get_known_language_slugs() {
        // Collect slugs and codes for all configured languages.
        $slugs     = array();
        $languages = wpr_translate_get_active_languages( false );

        foreach ( $languages as $language ) {
            $slug = wpr_translate_get_language_slug( $language );

            if ( '' !== $slug ) {
                // Store the slug for URL matching.
                $slugs[] = strtolower( $slug );
            }

            if ( ! empty( $language['code'] ) ) {
                // Also store the sanitized language code.
                $slugs[] = strtolower( sanitize_title( $language['code'] ) );
            }
        }

        // Return a de-duplicated list of valid slugs.
        $slugs = array_values( array_unique( array_filter( $slugs, 'strlen' ) ) );

        return $slugs;
    }
}

if ( ! function_exists( 'wpr_translation_build_url_from_parts' ) ) {
    /**
     * Rebuilds a URL string from its parsed components.
     *
     * @param array $parts Parsed URL parts.
     *
     * @return string
     */
    function wpr_translation_build_url_from_parts( $parts ) {
        // Assemble URL components in the correct order.
        $scheme   = isset( $parts['scheme'] ) ? $parts['scheme'] . '://' : '';
        $host     = isset( $parts['host'] ) ? $parts['host'] : '';
        $port     = isset( $parts['port'] ) ? ':' . $parts['port'] : '';
        $user     = isset( $parts['user'] ) ? $parts['user'] : '';
        $pass     = isset( $parts['pass'] ) ? ':' . $parts['pass'] : '';
        $pass     = $user && $pass ? $pass : '';
        $auth     = $user ? $user . $pass . '@' : '';
        $path     = isset( $parts['path'] ) ? $parts['path'] : '';
        $query    = isset( $parts['query'] ) ? '?' . $parts['query'] : '';
        $fragment = isset( $parts['fragment'] ) ? '#' . $parts['fragment'] : '';

        return $scheme . $auth . $host . $port . $path . $query . $fragment;
    }
}
