<?php
/**
 * Optional debug logger for social OAuth.
 *
 * @package WP_PowerSuite\OAuth\Helpers
 */

namespace WP_PowerSuite\OAuth\Helpers;

use WP_PowerSuite\Core\OptionStorePluginSettingsRepository;

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

final class Logger {

	/**
	 * @param array<string, mixed> $context
	 */
	public static function log( string $message, array $context = [] ): void {
		$debug = (bool) OptionStorePluginSettingsRepository::instance()->get( 'social_oauth_debug', false );
		if ( ! $debug && ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) {
			return;
		}
		$line = sprintf(
			'[WPPS OAuth] %s %s',
			$message,
			$context !== [] ? wp_json_encode( $context, JSON_UNESCAPED_SLASHES ) : ''
		);
		if ( function_exists( 'error_log' ) ) {
			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
			error_log( $line );
		}
	}
}
