import { BreadcrumbService } from '@/core/services';
import { SharedSaleInvoiceSingleViewComponent } from '@/shared/components/invoices/sale-invoice-single-view.component';
import pageParamsUtils from '@/utils/page-params.utils';
import { Component, computed, effect, inject, signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ConsumerSaleInvoiceStore } from '../store/main.store';

@Component({
  selector: 'consumer-saleInvoice',
  templateUrl: './single.component.html',
  imports: [SharedSaleInvoiceSingleViewComponent],
})
export class ConsumerSaleInvoiceComponent {
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);
  private readonly store = inject(ConsumerSaleInvoiceStore);

  pageParams = computed(() => pageParamsUtils(this.route));
  invoiceId = signal<string>(this.pageParams()['invoiceId']);
  editMode = signal<boolean>(false);

  readonly invoice = computed(() => this.store.entity());
  readonly loading = computed(() => this.store.loading());

  constructor() {
    effect(() => {
      if (this.invoice()?.id) {
        this.setBreadcrumb();
      }
    });
  }

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.store.getData(this.invoiceId());
  }

  setBreadcrumb() {
    this.breadcrumbService.setItems([...this.store.breadcrumbItems()]);
  }
}
