import { test as base } from "../conf";
import { RequestPage } from "../../pom/request/request.page";

const test = base.extend<{ checkCart: RequestPage }>({
    checkCart: async ({ page }, use) => {
        const requestPage = new RequestPage(page);
        await base.step("Setup: Check cart already clear", async () => {
            await requestPage.page.waitForLoadState('domcontentloaded');
            await requestPage.requestLoc.headingRequestPage.waitFor({ state: 'visible' });
            await requestPage.waitForSecond(4);
            await requestPage.dashboardLoc.cart.buttonCart.hover({ force: true });
            await requestPage.waitForSecond(2);
            const isCartButtonVisible: boolean = await requestPage.dashboardLoc.cart.requestButtonCart.isVisible({ timeout: 5_000 });
            const isCartButtonBulkEditVisible: boolean = await requestPage.dashboardLoc.cart.requestButtonEditCart.isVisible({ timeout: 5_000 });
            if (isCartButtonVisible) {
                await requestPage.dashboardLoc.cart.btnDeleteAllRequestCart("is_submission_cart").click({ force: true });
                await requestPage.waitForSecond(1);
                await requestPage.dashboardLoc.cart.buttonCart.click();
                await requestPage.dashboardLoc.common.pText("Cart Is Empty").first().waitFor({ state: 'visible' });
            } else if (isCartButtonBulkEditVisible) {
                await requestPage.dashboardLoc.cart.btnDeleteAllRequestCart("is_bulk_edit_cart").click({ force: true });
                await requestPage.waitForSecond(1);
                await requestPage.dashboardLoc.cart.buttonCart.click();
                await requestPage.dashboardLoc.common.pText("Cart Is Empty").first().waitFor({ state: 'visible' });
            }
        });

        await use(requestPage);
    }
});

export { test };
