import { FiltersMap, KeyedItem } from '@wix/bex-core';
import { NestedTableNodeStatePublicAPI } from './NestedTableNodeStatePublicAPI';
import { NestedTableNodeState } from './NestedTableNodeState';

export interface NestedTableLeafNodeStateParams<C extends string> {
  parentNode: NestedTableNodeState<C, any, any>;
  parent: KeyedItem<unknown> | null;
}

export class NestedTableLeafNodeState<C extends string, T, F extends FiltersMap>
  implements NestedTableNodeStatePublicAPI<C, T, F>
{
  readonly _parentNode: NestedTableNodeState<C, any, any>;
  readonly _parent: KeyedItem<unknown> | null;

  constructor(params: NestedTableLeafNodeStateParams<C>) {
    this._parentNode = params.parentNode;
    this._parent = params.parent;
  }

  getParent<TD, FD extends FiltersMap>(): NestedTableNodeStatePublicAPI<
    C,
    TD,
    FD
  > | null {
    return this._parentNode;
  }

  getDescendants<TD, FD extends FiltersMap>(): NestedTableNodeStatePublicAPI<
    C,
    TD,
    FD
  >[] {
    return [];
  }

  getData<TD>(): TD | null {
    const { _parent, _parentNode } = this;
    if (_parent == null || _parentNode == null) {
      return null;
    }
    return _parentNode.collection.getItem(_parent.key);
  }

  toggleExpand() {}

  get expanded() {
    return false;
  }

  get depth() {
    return this._parentNode.childrenDepth;
  }
}
