<?php
/**
 * Unique placeholder generation for shadow products.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChangesWooCommerce\CoreCompat\Recording;

use ScheduledChanges\Domain\Shared\Uuid;

/**
 * Generates reversible placeholder values for globally unique fields.
 */
final class UniquePlaceholderService {
	/**
	 * Builds a stable session-scoped placeholder.
	 */
	public function placeholder( Uuid $session_uuid, int $source_object_id, string $field_path ): string {
		if ( $source_object_id <= 0 || '' === trim( $field_path ) ) {
			throw new \InvalidArgumentException( 'A source object ID and field path are required.' );
		}

		$hash = hash( 'sha256', $session_uuid->value() . '|' . $source_object_id . '|' . $field_path );

		if ( substr_compare( $field_path, 'inventory.global_unique_id', -strlen( 'inventory.global_unique_id' ) ) === 0 ) {
			return '9' . substr(
				strtr(
					$hash,
					array(
						'a' => '0',
						'b' => '1',
						'c' => '2',
						'd' => '3',
						'e' => '4',
						'f' => '5',
					)
				),
				0,
				31
			);
		}

		return sprintf( 'scheduled-changes-shadow-%s', substr( $hash, 0, 18 ) );
	}
}
