import { Maybe } from '@/core';
import { CommonModule } from '@angular/common';
import {
  Component,
  ContentChild,
  EventEmitter,
  Input,
  Output,
  signal,
  TemplateRef,
} from '@angular/core';
import { RouterLink, UrlTree } from '@angular/router';
import { Button, ButtonDirective } from 'primeng/button';
import { Card } from 'primeng/card';

@Component({
  selector: 'app-card-data',
  standalone: true,
  templateUrl: './card-data.component.html',
  imports: [Card, CommonModule, ButtonDirective, Button, RouterLink],
})
export class AppCardComponent {
  @Input() cardTitle!: string;
  @Input() editable: boolean = false;
  @Input() showRefresh: boolean = false;
  @Input() backRoute?: UrlTree | string | any[];

  @Input()
  set editMode(v: boolean) {
    this.editModeSignal.set(!!v);
    this.editModeChange.emit(!!v);
  }
  get editMode() {
    return this.editModeSignal();
  }
  private editModeSignal = signal(false);
  @Output() editModeChange = new EventEmitter<boolean>();
  @Output() onRefresh = new EventEmitter<void>();

  @ContentChild('header', { static: true }) header?: Maybe<TemplateRef<any>>;
  @ContentChild('moreActions', { static: true }) moreActions?: Maybe<TemplateRef<any>>;

  // constructor() {
  //   if (this.editable) {
  //     if (!this.ed) {
  //       throw new Error('onEdit output must be bound when editable is true');
  //     }
  //   }
  // }

  onEditClick() {
    this.editModeChange.emit(!this.editMode);
  }

  refresh() {
    this.onRefresh.emit();
  }
}
