( function() {
	if ( typeof wp === 'undefined' || typeof wp.domReady !== 'function' ) {
		console.error( "EMT Block v2: wp.domReady is missing." );
		return;
	}

	const wcBlocksRegistry = window.wc.wcBlocksRegistry;

	if ( ! wcBlocksRegistry || ! wcBlocksRegistry.registerPaymentMethod ) {
		console.warn( "EMT Block v2: wc.wcBlocksRegistry.registerPaymentMethod is not available." );
		return;
	}

	wp.domReady( () => {
		const { registerPaymentMethod } = wcBlocksRegistry;
		const { createElement: el } = window.wp.element;
		const { __ } = window.wp.i18n;

		if ( ! registerPaymentMethod || ! el ) {
			console.warn( "EMT Block v2: Woo Blocks not ready or missing dependencies." );
			return;
		}

		console.log( "[EMT v2] registerPaymentMethod called" );

		// Get payment method settings from localized data
		let title = __( 'Email Money Transfer', 'advanced-emt-payment-gateway' );
		let description = __( 'Pay via EMT. You will receive instructions after placing the order.', 'advanced-emt-payment-gateway' );
		let logoUrl = '';
		let discountPercent = 0;
		let discountNotice = '';

		// Use localized data if available
		if ( window.emtBlocksData ) {
			if ( window.emtBlocksData.title ) title = window.emtBlocksData.title;
			if ( window.emtBlocksData.description ) description = window.emtBlocksData.description;
			if ( window.emtBlocksData.logoUrl ) logoUrl = window.emtBlocksData.logoUrl;
			if ( window.emtBlocksData.discountPercent ) discountPercent = window.emtBlocksData.discountPercent;
			if ( window.emtBlocksData.discountNotice ) discountNotice = window.emtBlocksData.discountNotice;
		}

		// Build label (logo optional, discount notice always allowed)
		let labelInner = title;

		if ( logoUrl ) {
			labelInner = el(
				'span',
				{},
				el( 'img', {
					src: logoUrl,
					alt: 'EMT',
					style: { height: '28px', verticalAlign: 'middle', marginRight: '8px' }
				} ),
				title
			);
		}

		let labelContent = labelInner;

		if ( discountNotice ) {
			labelContent = el(
				'span',
				{},
				labelInner,
				el(
					'em',
					{ style: { color: 'green', fontStyle: 'italic', fontWeight: 'normal', marginLeft: '8px' } },
					discountNotice
				)
			);
		}

		// Build content block
		const contentParts = [];
		if ( description ) {
			contentParts.push(
				el( 'p', {}, description )
			);
		}
		if ( discountNotice ) {
			contentParts.push(
				el( 'p', { style: { color: 'green' } }, discountNotice )
			);
		} else if ( discountPercent > 0 ) {
			contentParts.push(
				el(
					'p',
					{ style: { color: 'green', fontWeight: 'bold' } },
					__( 'A ' + discountPercent + '% discount will be applied when you order with EMT.', 'advanced-emt-payment-gateway' )
				)
			);
		}

		const contentElement = el( 'div', {}, ...contentParts );

		registerPaymentMethod( {
			name: 'advanced_emt',
			label: labelContent,
			ariaLabel: title,
			content: contentElement,
			edit: contentElement,
			canMakePayment: () => true,
			supports: {
				features: [ 'products' ],
			},
		} );
	} );
} )();
