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

const test = base.extend<{ casePage17: CasePage }>({
    casePage17: async ({ page, conf }, use) => {
        const casePage = new CasePage(page);
        
        await base.step("Setup: Check case unarchived and already have request", async () => {
            await casePage.waitForSecond(2);
            const isCaseUnarchived = (await casePage.dashboardLoc.table.itemInRow(conf.data.case).all()).length === 1;
            
            if (isCaseUnarchived) {
                // Case is unarchived - open directly
                await casePage.dashboardLoc.table.itemInRow(conf.data.case).click();
                await casePage.caseLoc.detail.buttonUpdateCase.waitFor({ state: 'visible' });
                await casePage.caseLoc.detail.buttonUpdateCase.click({ force: true });
                await casePage.dashboardLoc.modal.headerModal("Case Detail").waitFor({ state: 'visible' });
            } else {
                // Show archived cases
                await casePage.dashboardLoc.search.filter.btn.click();
                await casePage.caseLoc.inputArchive.fill("Yes");
                await casePage.caseLoc.boxSelectArchive.click();
                await casePage.dashboardLoc.search.filter.popup.btnApply.click();

                // Unarchive case
                await casePage.waitForSecond(2);
                await casePage.dashboardLoc.table.itemInRow(conf.data.case).waitFor({ state: 'visible' });
                await casePage.dashboardLoc.table.itemInRow(conf.data.case).click();
                await casePage.caseLoc.detail.update.btnUnArchive.click();

                // Move request to old case
                await casePage.open();
                await casePage.getDetailCase(conf.data.case_transfer_to);
                await casePage.clickButtonDropdownSelect("BTN_SHOW_MOVE_REQUEST_OF_BUSINESS_MODAL");
                await casePage.dashboardLoc.modal.headerModal("Move Requests").waitFor({ state: 'visible' });
                await casePage.caseLoc.detail.inputMoveRequest.click();
                await casePage.caseLoc.detail.inputMoveRequest.type(conf.data.case_transfer, { delay: 100 });
                await casePage.caseLoc.detail.transfer.boxOption(conf.data.case_transfer).click();
                await casePage.caseLoc.buttonByText("Move").click();

                // Reopen case
                await casePage.open();
                await casePage.getDetailCase(conf.data.case);
                await casePage.caseLoc.detail.buttonUpdateCase.waitFor({ state: 'visible' });
                await casePage.caseLoc.detail.buttonUpdateCase.click({ force: true });
                await casePage.dashboardLoc.modal.headerModal("Case Detail").waitFor({ state: 'visible' });
            }
        });

        await use(casePage);
    }
});

export { test };
