import {
  addEventListener,
  ReportBI,
  TransitionState,
  withoutDefaults,
} from '@wix/bex-core';
import { IntersectionObserverState } from '../../state';
import { makeObservable, observable } from 'mobx';
import {
  cairoTableTopNotificationDismissed,
  cairoTableTopNotificationDisplayed,
} from '@wix/bex-core/bi';
// import type { ScrollAwayPushStrategy } from './ScrollAwayPushStrategy';
// import { ScrollAwayScheduleStrategy } from './ScrollAwayScheduleStrategy';
// type ScrolledAwayStrategy = ScrollAwayPushStrategy | ScrollAwayScheduleStrategy;

export interface TableTopNotificationStateParams {
  getStickyContentHeight: () => number | null;
  topNotification: TransitionState<string | null | undefined>;
  consideredVisibleRatio?: number;
  reportBI: ReportBI;
}
export class TableTopNotificationState {
  readonly getStickyContentHeight;
  readonly topNotification;
  readonly intersection = new IntersectionObserverState();
  readonly consideredVisibleRatio;
  readonly reportBI: ReportBI;

  position: 'sticky' | 'inline' = 'inline';

  constructor(params: TableTopNotificationStateParams) {
    this.getStickyContentHeight = params.getStickyContentHeight;
    this.topNotification = params.topNotification;
    this.consideredVisibleRatio = params.consideredVisibleRatio ?? 0.9;
    this.reportBI = params.reportBI;

    makeObservable(this, {
      position: observable.ref,
    });
  }

  init() {
    const {
      topNotification: { events },
    } = this;

    const disposers = [
      addEventListener(events, 'enter', () => {
        this.reportBI(
          withoutDefaults(cairoTableTopNotificationDisplayed)({
            notificationText: this.topNotification.data ?? undefined,
          }),
        );

        const startTime = performance.now();

        events.once('dismissed', ({ dismissMode }) => {
          const duration = Math.round(performance.now() - startTime);
          this.reportBI(
            withoutDefaults(cairoTableTopNotificationDismissed)({
              dismissMode,
              duration,
              notificationText: this.topNotification.data ?? undefined,
            }),
          );
        });
      }),
    ];

    return () => {
      disposers.forEach((d) => d?.());
    };
  }
}
