<?php
/**
 * Free helpers for referrer drill-down display values in Insights UI.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Resolve drill-down target for a referring URL (channel or referrer domain).
 *
 * @param string $referring_url Raw referring URL from stats.
 * @return array{group: string, value: string}|null
 */
function linkcentral_resolve_referrer_drilldown( $referring_url ) {
	$site_domain = wp_parse_url( get_site_url(), PHP_URL_HOST );

	if ( '' === $referring_url ) {
		return array(
			'group' => 'channel',
			'value' => 'Direct',
		);
	}

	if ( $site_domain && false !== strpos( $referring_url, $site_domain ) ) {
		return array(
			'group' => 'channel',
			'value' => 'Internal',
		);
	}

	$host = wp_parse_url( $referring_url, PHP_URL_HOST );
	if ( empty( $host ) ) {
		return null;
	}

	return array(
		'group' => 'referrer',
		'value' => preg_replace( '/^www\./', '', $host ),
	);
}
