import { Component, EventEmitter, Input, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TreeNode } from '../../models/tree-node';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { faCaretDown, faCaretRight } from '@fortawesome/free-solid-svg-icons';
import { Action } from '../../models/action';

@Component({
  selector: 'fbi-tree',
  standalone: true,
  imports: [CommonModule, FontAwesomeModule],
  templateUrl: './tree.component.html',
  styleUrl: './tree.component.scss',
})
export class TreeComponent {
  @Input() node!: TreeNode;
  @Output() actionClick = new EventEmitter<{
    action: Action;
    node: TreeNode;
  }>();

  faCaretDown = faCaretDown;
  faCaretRight = faCaretRight;

  toggle(event: MouseEvent): void {
    if (!this.node.leaf) {
      event.stopPropagation();
      this.node.expanded = !this.node.expanded;
    }
  }

  passActionClick(event: { action: Action; node: TreeNode }): void {
    this.actionClick.emit(event);
  }

  onActionClick(event: MouseEvent, action: Action): void {
    event.stopPropagation();
    this.actionClick.emit({ action: action, node: this.node });
  }
}
