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

export class VinLookupPage extends BasePage {
    readonly page: Page;
    readonly vinLookupTextBox: Locator;
    readonly lookupVinForMe: Locator;
    readonly vinMismatchAlert: Locator;
    issPageValue = 'vin-lookup';

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.vinLookupTextBox = page.getByRole('textbox', { name: 'Enter your VIN' });
        this.lookupVinForMe = page.getByRole('link', { name: 'look up your VIN' });
        this.vinMismatchAlert = page.getByLabel('vehicle-not-matched-alert');
        // this.validateURL(this.url);
    }

    async enterVin(vin: string | string[], isVehicleLookupValidations = false) {
        if (Array.isArray(vin)) {
            if (vin.length > 1) {
                if (isVehicleLookupValidations) {
                    await this.vinLookupTextBox.fill(vin[0]);
                    await this.continueButton.click({ timeout: 1000 });
                    await expect(this.vinMismatchAlert).toBeVisible();
                    await this.vinLookupTextBox.fill(vin[1]);
                }
            } else {
                console.warn("VIN array is blank. Either provide VIN as string or string[]")
            }

        } else {
            await this.vinLookupTextBox.fill(vin);
        }
    }

    async triggerBailout() {
        await this.continueButton.click();
        await this.lookupVinForMe.click();
    }

    async handleNonServiceableVin() {
        await this.continueButton.click();
        const nonServiceableWarning = this.page.getByText('Service not available for your vehicle');
        await expect(nonServiceableWarning).toBeVisible();
        await expect(this.continueButton).toBeDisabled();
    }
}
