/**
 * @file assets/js/adminjs/mbs-admin-imp-exp.js
 * Import/Export shared utilities (HTML escaper, mbsShowPanelMessage, mbsRenderCandidatePreview) exposed on MBS_ImpExpUtils.
 * @package MyBookShowroom
 * @since   1.0.0
 */

( function ( $ ) {
	'use strict';

	var __ = wp.i18n.__;
	var _n = wp.i18n._n;

	/**
	 * Minimal HTML escaper for dynamic content inserted into the DOM.
	 *
	 * @param {*} val Value to escape.
	 * @return {string}
	 */
	function esc( val ) {
		if ( ! val && val !== 0 ) {
			return '';
		}
		return String( val )
			.replace( /&/g, '&amp;' )
			.replace( /</g,  '&lt;' )
			.replace( />/g,  '&gt;' )
			.replace( /"/g,  '&quot;' )
			.replace( /'/g,  '&#039;' );
	}

	/**
	 * Show a success / error / info message inside a panel.
	 *
	 * @param {string} elemId  ID (without #) of the message container.
	 * @param {string} text    Message text.
	 * @param {string} type    'success' | 'error' | 'info'
	 */
	function mbsShowPanelMessage( elemId, text, type ) {
		var typeClass = {
			success: 'notice-success',
			error:   'notice-error',
			info:    'notice-info'
		}[ type ] || 'notice-info';

		$( '#' + elemId )
			.removeClass( 'notice-success notice-error notice-info' )
			.addClass( 'notice is-dismissible ' + typeClass )
			.html( '<p>' + esc( text ) + '</p>' )
			.show();
	}

	/**
	 * Render a candidate preview table inside containerSelector.
	 *
	 * @param {string} containerSelector jQuery selector for the preview area.
	 * @param {Array}  candidates        Array of candidate objects from the API.
	 * @param {string} nonce             mbs_import_export nonce value.
	 * @param {string} messageSelector   jQuery selector for status message div.
	 */
	function mbsRenderCandidatePreview( containerSelector, candidates, nonce, messageSelector ) {
		var $container = $( containerSelector );

		if ( ! candidates.length ) {
			$container.html( '<p>' + MBS_AdminUtils.escHtml( __( 'No books found for the provided identifiers.', 'my-book-showroom' ) ) + '</p>' );
			return;
		}

		var html = '<table class="mbs-candidate-table widefat">';
		html += '<thead><tr>';
		html += '<th>' + MBS_AdminUtils.escHtml( __( 'Title',     'my-book-showroom' ) ) + '</th>';
		html += '<th>' + MBS_AdminUtils.escHtml( __( 'Author',    'my-book-showroom' ) ) + '</th>';
		html += '<th>' + MBS_AdminUtils.escHtml( __( 'ISBN-13',   'my-book-showroom' ) ) + '</th>';
		html += '<th>' + MBS_AdminUtils.escHtml( __( 'Publisher', 'my-book-showroom' ) ) + '</th>';
		html += '</tr></thead><tbody>';

		candidates.forEach( function ( c ) {
			html += '<tr>';
			html += '<td>' + esc( c.title || '\u2014' ) + '</td>';
			html += '<td>' + esc( c.author || '\u2014' ) + '</td>';
			html += '<td>' + esc( c.isbn13 || '\u2014' ) + '</td>';
			html += '<td>' + esc( c.publisher || '\u2014' ) + '</td>';
			html += '</tr>';
		} );

		html += '</tbody></table>';
		html += '<p>';
		html += '<button type="button" class="button button-primary mbs-confirm-import-btn"';
		html += ' data-nonce="' + esc( nonce ) + '"';
		html += ' data-message-selector="' + esc( messageSelector ) + '">';
		html += MBS_AdminUtils.escHtml( wp.i18n.sprintf(
			_n( 'Import %d Book', 'Import %d Books', candidates.length, 'my-book-showroom' ),
			candidates.length
		) );
		html += '</button>';
		html += '</p>';

		$container.html( html );
	}

	// Expose utilities for use by the feature modules.
	window.MBS_ImpExpUtils = {
		esc:                      esc,
		mbsShowPanelMessage:      mbsShowPanelMessage,
		mbsRenderCandidatePreview: mbsRenderCandidatePreview,
	};

} )( jQuery );
