/**
 * File Path: assets/js/mbs-admin-dashboard.js
 *
 * Admin Dashboard JS
 * Enqueued only on the MBS dashboard page.
 * Handles news feed refresh via AJAX.
 *
 * @package MyBookShowroom
 * @since   1.1.0
 */
/* global mbsDashboard, jQuery */
( function ( $ ) {
    'use strict';
    var __ = wp.i18n.__;

    // ── News Feed Refresh ─────────────────────────────────────────────────────
    $( document ).on( 'click', '#mbs-refresh-news', function () {
        var $btn  = $( this );
        var $list = $( '#mbs-news-list' );

        $btn.prop( 'disabled', true ).text( $btn.data( 'loading' ) || __( 'Refreshing\u2026', 'my-book-showroom' ) );

        $.post(
            mbsDashboard.ajaxUrl,
            {
                action : 'mbs_refresh_news_feed',
                _ajax_nonce: mbsDashboard.refreshNewsNonce,
            },
            function ( response ) {
                if ( response.success && response.data.html ) {
                    $list.html( response.data.html );
                } else {
                    var errMsg = ( response.data && response.data.message )
                        ? response.data.message
                        : __( 'Could not refresh feed.', 'my-book-showroom' );
                    $list.empty().append(
                        $( '<li>' ).addClass( 'mbs-news-empty' ).text( errMsg )
                    );
                }
            }
        ).always( function () {
            $btn.prop( 'disabled', false ).text( $btn.data( 'label' ) || __( 'Refresh', 'my-book-showroom' ) );
        } );
    } );

} )( jQuery );
