/**
 * Copyright (C) 2025 Brainformatik GmbH (info@brainformatik.com)
 *
 * This file is part of brainX.
 * All Rights Reserved.
 *
 * This part of brainX can not be copied and/or distributed without the
 * express permission of Brainformatik GmbH.
 */

import { test as base } from '@playwright/test';

import { MockApi } from '../utils/mockApi.js';

export const test = base.extend({
  mockApi: async ({
    context 
  }, use) => {
    const mockApi = new MockApi(context);
    await mockApi.attach();
    await use(mockApi);
    await mockApi.dispose();
  },
  intentRecorder: async ({
    page 
  }, use) => {
    await page.addInitScript(() => {
      const intents = [];
      Object.defineProperty(window, '__BRAINX_INTENTS__', {
        value: intents,
        writable: false
      });
      window.__BRAINX_INTENT_HANDLER__ = (uri) => {
        intents.push(uri);
      };
    });

    const tracker = {
      async getIntents() {
        return page.evaluate(() => window.__BRAINX_INTENTS__ || []);
      },
      async lastIntent() {
        const intents = await this.getIntents();
        return intents.at(-1) ?? null;
      },
      async reset() {
        await page.evaluate(() => {
          if (Array.isArray(window.__BRAINX_INTENTS__)) {
            window.__BRAINX_INTENTS__.length = 0;
          }
        });
      }
    };

    await use(tracker);
  }
});

export const expect = base.expect;
