import { Injectable } from "@angular/core";
import { Observable, map } from "rxjs";
import { InvoiceModel } from "../models/invoice";
import { invoice } from "../data/invoice";

@Injectable({
  providedIn: "root",
})
export class InvoiceService {
  invoiceData = new Observable<InvoiceModel[]>((subscriber) => {
    subscriber.next(invoice);
  });

  // Get all invoices
  getAllInvoices(): Observable<InvoiceModel[]> {
    return this.invoiceData;
  }

  // Get an invoice based on id
  getInvoiceById(companyId: number): Observable<InvoiceModel[]> {
    return this.invoiceData.pipe(
      map((invoice) =>
        invoice.filter((invoice) => invoice.companyId === companyId)
      )
    );
  }
}
