import { render, RenderResult } from '@testing-library/react';
import React from 'react';
import { Chance } from 'chance';
import { BulkActionToolbar, BulkActionToolbarProps } from './BulkActionToolbar';
import { BulkActionToolbarUniDriver } from './BulkActionToolbar.uni.driver';
import { uniTestkitFactoryCreator } from '@wix/wix-ui-test-utils/vanilla';
import { ProductsCollectionStateDriver } from '@wix/bex-core/testkit/internal';
import { CairoTestApp } from '../../test-utils';
import { CollectionProvider } from '../CollectionContext';

export class BulkActionToolbarDriver {
  chance = new Chance();
  _renderResult?: RenderResult;

  readonly state: ProductsCollectionStateDriver;

  constructor() {
    this.state = new ProductsCollectionStateDriver();
  }

  get backend() {
    return this.state.backend;
  }

  get collection() {
    return this.state.collection;
  }

  get container() {
    return this.state.container;
  }

  get i18n() {
    return this.container.i18n;
  }

  get renderResult() {
    if (this._renderResult == null) {
      throw new Error('Driver was not initialized');
    }

    return this._renderResult;
  }

  getBulkActionToolbar() {
    return uniTestkitFactoryCreator(BulkActionToolbarUniDriver)({
      wrapper: this.renderResult.baseElement,
      dataHook: 'bulk-action-toolbar',
    });
  }

  render(overrides: Partial<BulkActionToolbarProps> = {}) {
    this._renderResult = render(
      <CairoTestApp driver={this.container}>
        <CollectionProvider value={this.collection}>
          <BulkActionToolbar {...overrides} />
        </CollectionProvider>
      </CairoTestApp>,
    );
  }
}
