import { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';

export class TpaConfirmationPage extends BasePage {
    readonly page: Page;
    readonly successMessage: Locator;
    readonly shopConfirmationMessage: Locator;
    readonly issPageValue = 'tpa-confirmation';

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.successMessage = page.getByText('Thank you for submitting your claim; there is one final step.');
        this.shopConfirmationMessage = page.locator('div.subheader').first()
    }

    async getTpaShopSelectedName() {
        const sessionStorage = JSON.parse(await this.page.evaluate('sessionStorage.getItem(\'submittedOrder\')'));
        const tpaShopSelectedName = sessionStorage.serviceLocation.provider.companyName;
        return tpaShopSelectedName;
    }

    async validateSuccessMessage() {
        const shopConfirmationMessageText = await this.shopConfirmationMessage.textContent();
        const tpaShopSelectedNameText = await this.getTpaShopSelectedName();

        await this.successMessage.waitFor({ state: 'visible' });
        await expect(this.successMessage).toBeVisible();
        expect.soft(shopConfirmationMessageText?.toLowerCase()).toContain(
            ('Your information has been sent to' + ' ' + tpaShopSelectedNameText).toLowerCase()
        );    
    }
}