import { createElement } from 'lwc';
import GeBatchGiftEntryHeader from 'c/geBatchGiftEntryHeader';
import { getRecord } from 'lightning/uiRecordApi';
import { registerLdsTestWireAdapter, registerApexTestWireAdapter } from '@salesforce/sfdx-lwc-jest';

import getGiftBatchTotalsBy from '@salesforce/apex/GE_GiftEntryController.getGiftBatchTotalsBy';
import isElevateCustomer from '@salesforce/apex/GE_GiftEntryController.isElevateCustomer';

const mockGetRecord = require('./data/getRecord.json');
const getRecordAdapter = registerLdsTestWireAdapter(getRecord);
const isElevateCustomerAdapter = registerApexTestWireAdapter(isElevateCustomer);

const APEX_GIFT_BATCH_TOTALS_BY_SUCCESS = {
    'PROCESSED': 10,
    'FAILED': 5,
    'FAILED_PAYMENT': 1,
    'TOTAL': 20
};

const APEX_GIFT_BATCH_TOTALS_WITHOUT_ROWS = {
    'PROCESSED': 0,
    'FAILED': 0,
    'FAILED_PAYMENT': 0,
    'TOTAL': 0
};

jest.mock(
    "@salesforce/apex/GE_GiftEntryController.getGiftBatchTotalsBy",
    () => {
        return {
            default: jest.fn()
        };
    },
    { virtual: true }
);

describe('c-ge-batch-gift-entry-header', () => {
    afterEach(() => {
        clearDOM();
        jest.clearAllMocks();
    });

    function flushPromises() {
        return new Promise((resolve) => setImmediate(resolve));
    }

    const setup = () => {
        const element = createElement('c-ge-batch-gift-entry-header', {
            is: GeBatchGiftEntryHeader
        });
        document.body.appendChild(element);
        getRecordAdapter.emit(mockGetRecord);
        return element;
    }

    describe('getRecord @wire data', () => {
        it('renders user record details', async () => {
            getGiftBatchTotalsBy.mockResolvedValue(APEX_GIFT_BATCH_TOTALS_BY_SUCCESS);
            const element = setup();

            await flushPromises();
            const headerElement = element.shadowRoot.querySelectorAll('c-util-page-header');
            const headerElements = headerElement[0].shadowRoot.querySelectorAll('h1');
            const text = headerElements[0].querySelectorAll('.uiOutputText');
            expect(text[0].innerHTML).toBe(mockGetRecord.fields.Name.value);
        });
    });

    describe('gift entry batch header elements', () => {
        it('should render three action buttons', async () => {
            getGiftBatchTotalsBy.mockResolvedValue(APEX_GIFT_BATCH_TOTALS_BY_SUCCESS);
            const element = setup();

            await flushPromises();
            const buttons = element.shadowRoot.querySelectorAll('lightning-button');
            expect(buttons.length).toBe(3);
        });

        it('renders detail row', async () => {
            getGiftBatchTotalsBy.mockResolvedValue(APEX_GIFT_BATCH_TOTALS_BY_SUCCESS);
            const element = setup();

            await flushPromises();
            const headerDetailRows = element.shadowRoot.querySelectorAll('c-util-page-header-detail-row');
            expect(headerDetailRows.length).toBe(1);
        });

        it('does not render detail row', async () => {
            getGiftBatchTotalsBy.mockResolvedValue(APEX_GIFT_BATCH_TOTALS_WITHOUT_ROWS);
            const element = setup();

            await flushPromises();
            const headerDetailRows = element.shadowRoot.querySelectorAll('c-util-page-header-detail-row');
            expect(headerDetailRows.length).toBe(0);
        });

        it('renders detail blocks for records processed and records failed with correct record counts on load', async () => {
            getGiftBatchTotalsBy.mockResolvedValue(APEX_GIFT_BATCH_TOTALS_BY_SUCCESS);
            let element = setup();

            isElevateCustomerAdapter.emit(false);

            await flushPromises();

            const detailBlocks = element.shadowRoot.querySelectorAll('c-util-page-header-detail-block');
            expect(detailBlocks.length).toBe(2);

            const processedGifts = detailBlocks[0].querySelectorAll('p')[1].innerHTML;
            expect(processedGifts).toBe('10 / 20');

            const failedGifts = detailBlocks[1].querySelectorAll('p')[1].innerHTML;
            expect(failedGifts).toBe('5');
        });

        it('renders detail blocks when elevate is enabled with correct record counts on load', async () => {
            getGiftBatchTotalsBy.mockResolvedValue(APEX_GIFT_BATCH_TOTALS_BY_SUCCESS);
            const element = setup();

            isElevateCustomerAdapter.emit(true);

            await flushPromises();

            const detailBlocks = element.shadowRoot.querySelectorAll('c-util-page-header-detail-block');
            expect(detailBlocks.length).toBe(3);

            const failedGifts = detailBlocks[2].querySelectorAll('p')[1].innerHTML;
            expect(failedGifts).toBe('1');
        });
    });
});
@@ -183,37 +183,8 @@
 pter
-, registerApexTestWireAdapter
  %7D f
@@ -216,16 +216,16 @@
 -jest';%0A
+
 %0Aimport 
@@ -317,99 +317,8 @@
 By';
-%0Aimport isElevateCustomer from '@salesforce/apex/GE_GiftEntryController.isElevateCustomer';
 %0A%0Aco
@@ -438,89 +438,8 @@
 rd);
-%0Aconst isElevateCustomerAdapter = registerApexTestWireAdapter(isElevateCustomer);
 %0A%0Aco
@@ -522,33 +522,8 @@
  5,%0A
-    'FAILED_PAYMENT': 1,%0A
     
@@ -596,32 +596,32 @@
 'PROCESSED': 0,%0A
+
     'FAILED': 0,
@@ -625,33 +625,8 @@
  0,%0A
-    'FAILED_PAYMENT': 0,%0A
     
@@ -3387,59 +3387,8 @@
 );%0A%0A
-            isElevateCustomerAdapter.emit(false);%0A%0A
     
@@ -3856,623 +3856,8 @@
  %7D);
-%0A%0A        it('renders detail blocks when elevate is enabled with correct record counts on load', async () =%3E %7B%0A            getGiftBatchTotalsBy.mockResolvedValue(APEX_GIFT_BATCH_TOTALS_BY_SUCCESS);%0A            const element = setup();%0A%0A            isElevateCustomerAdapter.emit(true);%0A%0A            await flushPromises();%0A%0A            const detailBlocks = element.shadowRoot.querySelectorAll('c-util-page-header-detail-block');%0A            expect(detailBlocks.length).toBe(3);%0A%0A            const failedGifts = detailBlocks%5B2%5D.querySelectorAll('p')%5B1%5D.innerHTML;%0A            expect(failedGifts).toBe('1');%0A        %7D);
 %0A   

remove jest tests related to payment processing failure