<?php
/**
 * Elite Licenser UI helpers for WP PowerSuite.
 *
 * License validation and deactivation are handled exclusively by
 * {@see W_P_Power_Suite_Premium_Base}. This class only provides shared
 * presentation helpers from the original Elite Licenser SDK.
 *
 * @package WP_PowerSuite
 * @since 1.0.0
 */

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

/**
 * Shared Elite Licenser helper methods.
 */
final class WPPS_Elite_Licenser_Client {

	/**
	 * Build renew URLs from the original Elite Licenser helper.
	 */
	public static function get_renew_link( object $response_obj, string $type = 's' ): string {
		if ( empty( $response_obj->renew_link ) || empty( $response_obj->license_key ) ) {
			return '';
		}

		$is_show_button = false;

		if ( $type === 's' ) {
			$support_str = strtolower( trim( (string) $response_obj->support_end ) );
			if ( $support_str === 'no support' ) {
				$is_show_button = true;
			} elseif ( ! in_array( $support_str, [ 'unlimited' ], true ) ) {
				$support_end = strtotime( (string) $response_obj->support_end );
				if ( $support_end !== false && strtotime( 'ADD 30 DAYS', $support_end ) < time() ) {
					$is_show_button = true;
				}
			}
		} else {
			$expire_str = strtolower( trim( (string) $response_obj->expire_date ) );
			if ( ! in_array( $expire_str, [ 'unlimited', 'no expiry' ], true ) ) {
				$expire_date = strtotime( (string) $response_obj->expire_date );
				if ( $expire_date !== false && strtotime( 'ADD 30 DAYS', $expire_date ) < time() ) {
					$is_show_button = true;
				}
			}
		}

		if ( ! $is_show_button ) {
			return '';
		}

		$separator = strpos( (string) $response_obj->renew_link, '?' ) === false ? '?' : '&';
		$lic_type  = $type === 's' ? 's' : 'l';

		return (string) $response_obj->renew_link . $separator . 'type=' . $lic_type . '&lic=' . rawurlencode( (string) $response_obj->license_key );
	}
}
