<?php

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}


/**
 * Overloads the DOM with a notification if the Translation API is overloaded.
 *
 * This function modifies the DOM to include a notification message when the
 * Translation API is overloaded. It checks the provided arguments and user
 * permissions before making changes to the DOM.
 *
 * @param object $dom  The DOM object to be modified.
 * @param array  $args An array of arguments, including 'load' and 'overloaded' keys.
 * @return object      The modified DOM object.
 */
function wplng_dom_load_overload( $dom, $args ) {

	wplng_args_setup( $args );

	if ( empty( $args['overloaded'] )
		|| ! current_user_can( 'edit_posts' )
	) {
		return $dom;
	}

	$html = '<div id="wplng-overloaded-container">';

	$html .= '<span class="dashicons dashicons-info-outline"></span> ';

	$html .= '<span id="wplng-overloaded-text-mobile">';
	$html .= esc_html__( 'Translation API overloaded', 'wplingua' );
	$html .= '</span>';

	$api_data = wplng_get_api_data();

	if ( empty( $api_data['status'] ) || $api_data['status'] === 'FREE' ) {
		$html .= '<span id="wplng-overloaded-text-desktop">';
		$html .= esc_html__( 'Translation API overloaded. Retry in a few minutes or ', 'wplingua' );
		$html .= '<a href="https://wplingua.com/pricing/" target="_blank" rel="noopener noreferrer">';
		$html .= esc_html__( 'upgrade your API key', 'wplingua' );
		$html .= '</a>';
		$html .= esc_html__( ' to get priority API access.', 'wplingua' );
		$html .= '</span>';
	} else {
		$html .= '<span id="wplng-overloaded-text-desktop">';
		$html .= esc_html__( 'Translation API overloaded. Please retry in a few minutes.', 'wplingua' );
		$html .= '</span>';
	}

	$html .= '<span';
	$html .= ' id="wplng-overloaded-close"';
	$html .= ' class="dashicons dashicons-no-alt"';
	$html .= ' title="' . esc_attr__( 'Close', 'wpLingua' ) . '"';
	$html .= '></span>';

	$html .= '</div>'; // End #wplng-overloaded-container

	foreach ( $dom->find( 'body' ) as $body ) {

		// Add body clas .wplingua-api-overloaded
		if ( isset( $body->class ) ) {
			$body->class .= ' wplingua-api-overloaded';
		} else {
			$body->class = 'wplingua-api-overloaded';
		}

		// Add "API overloaded" HTML bar to body
		$body->innertext .= $html;
	}

	return $dom;
}
