<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! class_exists( 'WFACP_Klarna' ) ) {
	/**
	 * Official Klarna Payments for WooCommerce (https://wordpress.org/plugins/klarna-payments-for-woocommerce/)
	 * Payment Plugins for Stripe WooCommerce (Klarna) (https://wordpress.org/plugins/woo-stripe-payment/)
	 */
	#[AllowDynamicProperties]
	class WFACP_Klarna {
		public function __construct() {
			add_action( 'wfacp_internal_css', array( $this, 'disable_pop_state' ) );
			add_action( 'woocommerce_checkout_order_processed', array( $this, 'attach_hooks' ) );
		}


		public function disable_pop_state() {
			?>
			<script>
				window.addEventListener('bwf_checkout_load', function () {
					wfacp_frontend.hooks.addFilter('disable_pop_state', function (status, newHash) {
						if ('' !== newHash && newHash.indexOf('#kp') > -1) {
							status = true;
						}
						return status;
					});
				})
			</script>
			<?php
		}

		public function attach_hooks() {
			add_filter( 'woocommerce_get_checkout_url', array( $this, 'change_checkout_url' ) );
		}

		public function change_checkout_url( $url ) {
			if ( ! isset( $_REQUEST['payment_method'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Payment gateway callback, nonce verification handled by payment gateway
				return $url;
			}
			$payment_method = sanitize_text_field( wp_unslash( $_REQUEST['payment_method'] ) );
			if ( ! in_array( $payment_method, array( 'klarna_payments', 'klarna_payments_pay_later', 'klarna_payments_pay_over_time', 'stripe_klarna' ), true ) ) {
				return $url;
			}
			if ( ! isset( $_REQUEST['_wfacp_post_id'] ) || empty( $_REQUEST['_wfacp_post_id'] ) ) {
				return $url;
			}

			$aero_id   = absint( wp_unslash( $_REQUEST['_wfacp_post_id'] ) );
			$is_global = isset( $_REQUEST['wfacp_is_checkout_override'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wfacp_is_checkout_override'] ) ) : 'no';
			$global_id = WFACP_Common::get_checkout_page_id();
			if ( 'yes' === $is_global ) {
				$global_post = get_post( $global_id );
				if ( ! is_null( $global_post ) && $global_post->post_type !== WFACP_Common::get_post_type_slug() ) {
					$url = get_the_permalink( $global_id );
				}
			} elseif ( isset( $_REQUEST['wfacp_embed_form_page_id'] ) ) {
					$url = get_the_permalink( absint( wp_unslash( $_REQUEST['wfacp_embed_form_page_id'] ) ) );
			} else {
				$url = get_the_permalink( $aero_id );
			}

			return $url;
		}
	}


	new WFACP_Klarna();


}

