import { test as base, expect } from "../conf";
import { CasePage } from "../../pom/case/case.page";

const test = base.extend<{ casePage22: CasePage }>({
    casePage22: async ({ page, conf }, use) => {
        const casePage = new CasePage(page);
        await base.step("Setup: Verify case not have any business archive", async () => {
            const listBusinesses = (await casePage.caseLoc.detail.listBusinesses.all());

            if (listBusinesses.length !== 3) {
                //unArchive Business
                await casePage.caseLoc.detail.btnViewArchivedBusiness.click({ force: true });
                await casePage.caseLoc.detail.singleBusinesses(conf.data.business_archive).waitFor({ state: 'visible' });
                await casePage.clickButtonDropdownSelect("BTN_ARCHIVE_UNARCHIVE_BUSINESS_IN_CASE_DETAIL");
                await casePage.caseLoc.detail.addLegalModal.msgSuccess.first().waitFor({ state: 'visible' });
                await casePage.waitForSecond(3);

                //unArchive request of Business
                const listRequests = (await casePage.caseLoc.detail.listArchiveRequests.all()).length;
                if (listRequests > 0) {
                    for (let i = 0; i < listRequests; i++) {
                        await casePage.caseLoc.detail.listLinkArchiveRequests.first().waitFor({ state: 'visible' });
                        await casePage.waitForSecond(1.5);
                        await casePage.caseLoc.detail.listLinkArchiveRequests.first().click();
                        await casePage.caseLoc.detail.update.btnUnArchive.click();
                        await casePage.dashboardLoc.buttonByText("Unarchive").click()
                        await casePage.caseLoc.btnCloseModal.click();
                        await casePage.caseLoc.linkToCase(conf.data.case).waitFor({ state: 'visible' });
                        await casePage.waitForSecond(1.5);
                        await casePage.caseLoc.linkToCase(conf.data.case).click();
                    }
                }
            }
        });

        await use(casePage);
    }
});

export { test };
