import { CustomColumnsState } from './CustomColumnsState';
import { CollectionOptimisticActions, FiltersMap } from '@wix/bex-core';
import { arrayMove } from '@wix/wix-style-react-incubator/dnd-kit/sortable';
import { addEventListener } from '@wix/bex-core/util';
import type { CustomColumnsDragAndDrop } from '../components/CustomColumns/CustomColumnsDragAndDrop';
import { CollectionDragAndDropState } from '../components/DragAndDrop';
import { Column } from '../model';
import { action, computed, makeObservable } from 'mobx';
import { RectState } from './RectState';
import { indexOfCompareFn } from './indexOfCompareFn';

export interface CustomColumnsDragAndDropStateParams<T, F extends FiltersMap> {
  readonly customColumns: CustomColumnsState<T, F>;
  readonly components: typeof CustomColumnsDragAndDrop;
  readonly modalsContainerRef: {
    current: HTMLElement | null | undefined;
  };
}

export class CustomColumnsDragAndDropState<T, F extends FiltersMap> {
  readonly container;
  readonly customColumns;
  readonly table;
  readonly columnsActions;
  readonly components;

  get _dnd() {
    return this.collectionDragAndDropState?.dnd;
  }

  collectionDragAndDropState: CollectionDragAndDropState<Column, {}> | null =
    null;

  forceRenderIndexes: number[] | undefined = undefined;

  reportBi = () => {
    // report columns re-order BI here
  };

  readonly listRect;

  constructor(params: CustomColumnsDragAndDropStateParams<T, F>) {
    const { customColumns, components } = params;
    this.components = components;
    this.container = customColumns.container;
    this.listRect = new RectState({
      container: customColumns.container,
    });
    this.customColumns = customColumns;
    this.table = customColumns.table;
    this.columnsActions = new CollectionOptimisticActions({
      container: this.container,
      collection: this.customColumns.columnsCollection,
    });

    this.collectionDragAndDropState = new CollectionDragAndDropState({
      state: this,
      a11yContainer: params.modalsContainerRef,
      container: this.container,
    });

    makeObservable(this, {
      _onCustomColumnsReorder: action.bound,
      containerRect: computed,
      orderedColumns: computed,
      orderedColumnsByReorderDisabled: computed,
    });
  }

  get orderedColumnsByReorderDisabled() {
    return this.table.columns.reduce(
      (state, column) => {
        column.reorderDisabled
          ? state.reorderDisabled.push(column)
          : state.regular.push(column);

        return state;
      },
      { regular: [] as Column[], reorderDisabled: [] as Column[] },
    );
  }

  get orderedColumns(): Column[] {
    const { table } = this;
    const {
      selectedColumns: { value: selectedColumns },
    } = table;

    const { reorderDisabled, regular } = this.orderedColumnsByReorderDisabled;

    return [
      ...reorderDisabled,
      ...regular.sort(
        indexOfCompareFn(selectedColumns as { id: string }[], (c) => c.id),
      ),
    ];
  }

  get collection() {
    return this.customColumns.columnsCollection;
  }

  get containerRect() {
    const { listRect } = this;
    if (listRect.rect.left) {
      return {
        ...listRect.rect,
      };
    }
    return undefined;
  }

  _onCustomColumnsReorder(columnId: string, overColumnId: string) {
    const {
      table: { selectedColumns },
    } = this;

    const columnsArray = selectedColumns.value;

    const reorderedColumnsArray = arrayMove(
      columnsArray,
      columnsArray.findIndex((c) => c.id === columnId),
      columnsArray.findIndex((c) => c.id === overColumnId),
    );

    selectedColumns.refresh(reorderedColumnsArray);
  }

  init() {
    const { columnsActions } = this;
    const disposers = [
      columnsActions.init(),
      addEventListener(columnsActions.events, 'updated', async () => {
        const {
          customColumns: { columnsCollection },
        } = this;
        await columnsCollection.refreshAllPages();
      }),
    ];

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