<?php

/**
 * LolaCore — Hub Fact Hash
 *
 * @package LolaCore
 * @license GPL-2.0-or-later
 */

declare(strict_types=1);

namespace LolaCore\Hub;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Stable content hash for fleet memory reconcile dedupe (E10 §3, shared contract with H8).
 */
final class HubFactHash
{
    /**
     * Normalizes and hashes a memory fact for cross-node dedupe.
     */
    public static function compute(string $factType, string $content): string
    {
        $normalized = self::normalizeContent($content);
        $type       = strtolower(trim($factType));

        return hash('sha256', $type . '|' . $normalized);
    }

    public static function normalizeContent(string $content): string
    {
        $content = trim($content);
        $content = preg_replace('/\s+/u', ' ', $content) ?? $content;

        return function_exists('mb_strtolower') ? mb_strtolower($content) : strtolower($content);
    }
}
