import { Action } from './action';

export class TreeNode {
  label: string;
  data: any;
  children: TreeNode[];
  leaf: boolean;
  expanded: boolean;
  hidden: boolean;
  disabled: boolean;
  selected: boolean;
  actions: Action[];

  index: number;
  isFirst: boolean;
  isLast: boolean;

  constructor(data: Partial<TreeNode>) {
    this.label = data?.label ?? '';
    this.data = data?.data ?? null;
    this.children = (data?.children ?? []).map(
      (i: Partial<TreeNode>, index: number) =>
        new TreeNode({ ...i, ...{ index: index } })
    );
    this.leaf = data?.leaf ?? this.children.length === 0;
    this.expanded = data?.expanded ?? false;
    this.hidden = data?.hidden ?? false;
    this.disabled = data?.disabled ?? false;
    this.selected = data?.selected ?? false;
    this.actions = (data?.actions ?? []).map(
      (i: Partial<Action>) => new Action(i)
    );

    this.index = data?.index ?? 0;
    this.isFirst = data?.isFirst ?? false;
    this.isLast = data?.isLast ?? false;
  }
}
