<?php
/**
 * Human-facing labels for partner product attribute terms.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Admin_Partner_Product_Label_Normalizer {
	public function term_label( $value, $taxonomy = '', $context = array() ) {
		$explicit = isset( $context['label'] ) ? $this->scalar_label_value( $context['label'], (string) $value ) : '';
		if ( '' !== $explicit ) {
			return $this->humanize( $explicit );
		}

		$value = trim( (string) $value );
		if ( '' === $value ) {
			return '';
		}

		$label = preg_replace( '/(?:[-_](?:en|sr|rs|ba|hr|me|mk|de|fr|it|es|us|uk))$/i', '', $value );
		$label = $this->humanize( $label );

		return '' !== $label ? $label : $this->humanize( $value );
	}

	public function term_label_map_from_product_data( $product_data ) {
		$product_data = is_array( $product_data ) ? $product_data : array();
		$map          = array();

		foreach ( isset( $product_data['attribute_term_labels'] ) && is_array( $product_data['attribute_term_labels'] ) ? $product_data['attribute_term_labels'] : array() as $taxonomy => $labels ) {
			$taxonomy = $this->normalize_taxonomy( $taxonomy );
			if ( '' === $taxonomy || ! is_array( $labels ) ) {
				continue;
			}
			foreach ( $labels as $slug => $label ) {
				$slug  = sanitize_title( (string) $slug );
				$label = $this->term_label( $slug, $taxonomy, array( 'label' => $label ) );
				if ( '' !== $slug && '' !== $label ) {
					$map[ $taxonomy ][ $slug ] = $label;
				}
			}
		}

		foreach ( isset( $product_data['dynamic_variant_manifest']['attribute_labels'] ) && is_array( $product_data['dynamic_variant_manifest']['attribute_labels'] ) ? $product_data['dynamic_variant_manifest']['attribute_labels'] : array() as $taxonomy => $labels ) {
			$taxonomy = $this->normalize_taxonomy( $taxonomy );
			if ( '' === $taxonomy || ! is_array( $labels ) ) {
				continue;
			}
			foreach ( $labels as $slug => $label ) {
				$slug  = sanitize_title( (string) $slug );
				$label = $this->term_label( $slug, $taxonomy, array( 'label' => $label ) );
				if ( '' !== $slug && '' !== $label ) {
					$map[ $taxonomy ][ $slug ] = $label;
				}
			}
		}

		foreach ( isset( $product_data['dynamic_variant_manifest']['variants'] ) && is_array( $product_data['dynamic_variant_manifest']['variants'] ) ? $product_data['dynamic_variant_manifest']['variants'] : array() as $variant ) {
			foreach ( isset( $variant['attribute_labels'] ) && is_array( $variant['attribute_labels'] ) ? $variant['attribute_labels'] : array() as $taxonomy => $label ) {
				$taxonomy = $this->normalize_taxonomy( $taxonomy );
				$slug     = isset( $variant['attributes'][ $taxonomy ] ) ? sanitize_title( (string) $variant['attributes'][ $taxonomy ] ) : '';
				$label    = $this->term_label( $slug, $taxonomy, array( 'label' => $label ) );
				if ( '' !== $taxonomy && '' !== $slug && '' !== $label ) {
					$map[ $taxonomy ][ $slug ] = $label;
				}
			}
		}

		return $map;
	}

	private function scalar_label_value( $label, $slug = '' ) {
		if ( is_array( $label ) ) {
			$slug = sanitize_title( (string) $slug );
			if ( '' !== $slug && isset( $label[ $slug ] ) && ! is_array( $label[ $slug ] ) ) {
				return trim( (string) $label[ $slug ] );
			}
			foreach ( $label as $value ) {
				if ( ! is_array( $value ) ) {
					$value = trim( (string) $value );
					if ( '' !== $value ) {
						return $value;
					}
				}
			}
			return '';
		}
		return trim( (string) $label );
	}

	private function humanize( $value ) {
		$value = trim( (string) $value );
		if ( '' === $value ) {
			return '';
		}
		$value = preg_replace( '/[-_]+/u', ' ', $value );
		$value = preg_replace( '/\s+/u', ' ', $value );
		$value = trim( (string) $value );
		if ( function_exists( 'mb_strtolower' ) && function_exists( 'mb_substr' ) && function_exists( 'mb_strtoupper' ) ) {
			$value = mb_strtolower( $value, 'UTF-8' );
			return mb_strtoupper( mb_substr( $value, 0, 1, 'UTF-8' ), 'UTF-8' ) . mb_substr( $value, 1, mb_strlen( $value, 'UTF-8' ), 'UTF-8' );
		}
		$value = strtolower( $value );
		return ucfirst( $value );
	}

	private function normalize_taxonomy( $taxonomy ) {
		$taxonomy = sanitize_key( preg_replace( '/^attribute_/', '', (string) $taxonomy ) );
		return 0 === strpos( $taxonomy, 'pa_' ) ? $taxonomy : ( '' !== $taxonomy ? 'pa_' . $taxonomy : '' );
	}
}
