<?php

/**
 * SCPWOO_Aliases
 *
 * Resolves city and neighborhood name aliases to their canonical forms.
 * African city names often have multiple spellings (Douala/Duala, Yaoundé/Yaounde).
 * This class provides a filter-based alias system so variant spellings resolve correctly.
 *
 * @package SCPWOO
 */

defined( 'ABSPATH' ) or die( 'Forbidden' );

class SCPWOO_Aliases {

    /**
     * Resolve a city name to its canonical form using aliases.
     *
     * Developers can hook into 'scpwoo_city_aliases' to add aliases:
     *   add_filter('scpwoo_city_aliases', function($aliases, $country_code) {
     *       $aliases['Duala'] = 'Douala';
     *       return $aliases;
     *   }, 10, 2);
     *
     * @param string $city City name (possibly an alias).
     * @param string $country_code Country code (e.g. 'CM').
     * @return string Canonical city name, or original if no alias found.
     */
    public static function resolve_city( string $city, string $country_code ): string {
        $aliases = apply_filters( 'scpwoo_city_aliases', array(), $country_code );
        if ( empty( $aliases ) || ! is_array( $aliases ) ) {
            return $city;
        }
        // Case-insensitive lookup
        foreach ( $aliases as $alias => $canonical ) {
            if ( mb_strtolower( trim( $alias ) ) === mb_strtolower( trim( $city ) ) ) {
                return $canonical;
            }
        }
        return $city;
    }

    /**
     * Resolve a neighborhood name to its canonical form.
     *
     * Filter: 'scpwoo_neighborhood_aliases'
     *
     * @param string $neighborhood Neighborhood name.
     * @param string $city City name.
     * @param string $country_code Country code.
     * @return string Canonical neighborhood name.
     */
    public static function resolve_neighborhood( string $neighborhood, string $city, string $country_code ): string {
        $aliases = apply_filters( 'scpwoo_neighborhood_aliases', array(), $city, $country_code );
        if ( empty( $aliases ) || ! is_array( $aliases ) ) {
            return $neighborhood;
        }
        foreach ( $aliases as $alias => $canonical ) {
            if ( mb_strtolower( trim( $alias ) ) === mb_strtolower( trim( $neighborhood ) ) ) {
                return $canonical;
            }
        }
        return $neighborhood;
    }
}
