<?php
if (!defined('ABSPATH')) exit;

if (!function_exists('custausu_default_route_language')) {
  function custausu_default_route_language(): string {
    $lang = function_exists('custausu_config')
      ? sanitize_key((string) custausu_config('routes.policy.default_lang', ''))
      : '';

    if ($lang === '') {
      $locale = function_exists('determine_locale')
        ? determine_locale()
        : get_locale();
      $locale = strtolower(str_replace('_', '-', (string) $locale));
      $parts = explode('-', $locale);
      $lang = sanitize_key((string) ($parts[0] ?? ''));
    }

    if (!in_array($lang, ['en', 'it'], true)) {
      $lang = 'en';
    }

    return $lang;
  }
}

if (!function_exists('custausu_route_url')) {
  function custausu_route_url(string $key, ?string $lang = null): string {
    unset($lang);
    $lang = custausu_default_route_language();

    $pid = (int) (function_exists('custausu_config') ? custausu_config("routes.page_ids.$lang.$key", 0) : 0);
    if ($pid > 0) {
      $url = get_permalink($pid);
      if (is_string($url) && $url !== '') {
        return $url;
      }
    }

    $path = (string) (function_exists('custausu_config') ? custausu_config("routes.paths.$lang.$key", '') : '');
    if ($path !== '') {
      return home_url($path);
    }

    return home_url('/');
  }
}

if (!function_exists('custausu_login_url_for_lang')) {
  /**
   * Resolve the CAS login page for the active Free language context.
   */
  function custausu_login_url_for_lang(?string $lang = null): string {
    if (
      function_exists('custausu_safe_mode_is_enabled')
      && custausu_safe_mode_is_enabled()
    ) {
      return wp_login_url();
    }

    unset($lang);
    $lang = custausu_default_route_language();

    $page_id = function_exists('custausu_config')
      ? (int) custausu_config("routes.page_ids.{$lang}.login", 0)
      : 0;

    if ($page_id > 0 && get_post_status($page_id) === 'publish') {
      $permalink = get_permalink($page_id);

      if (is_string($permalink) && $permalink !== '') {
        return $permalink;
      }
    }

    $path = function_exists('custausu_config')
      ? trim((string) custausu_config("routes.paths.{$lang}.login", ''))
      : '';

    if ($path !== '') {
      return home_url('/' . trim($path, '/') . '/');
    }

    return wp_login_url();
  }
}

if (!function_exists('custausu_login_url_with_redirect')) {
  /** Attach a post-login destination without double encoding. */
  function custausu_login_url_with_redirect(
    string $redirect_to,
    ?string $lang = null
  ): string {
    $redirect_to = esc_url_raw(trim($redirect_to));
    $login_url = custausu_login_url_for_lang($lang);

    if ($redirect_to === '') {
      return $login_url;
    }

    $native_path = (string) wp_parse_url(
      wp_login_url(),
      PHP_URL_PATH
    );

    $resolved_path = (string) wp_parse_url(
      $login_url,
      PHP_URL_PATH
    );

    if (
      $native_path !== ''
      && $resolved_path !== ''
      && rtrim($native_path, '/') === rtrim($resolved_path, '/')
    ) {
      return wp_login_url($redirect_to);
    }

    return (string) add_query_arg(
      'redirect_to',
      $redirect_to,
      $login_url
    );
  }
}

if (!function_exists('custausu_profile_login_url')) {
  /** Route profile calls-to-action through the CAS login skin. */
  function custausu_profile_login_url(
    ?string $lang = null
  ): string {
    return custausu_login_url_with_redirect(
      admin_url('profile.php'),
      $lang
    );
  }
}

