import { Collection } from "@repo/rdx-utils";
import { InvoiceAmount } from "../../value-objects";
import { InvoiceTax } from "./invoice-tax";

export interface InvoiceTaxesProps {
  items?: InvoiceTax[];
}

export class InvoiceTaxes extends Collection<InvoiceTax> {
  constructor(props: InvoiceTaxesProps) {
    const { items = [] } = props;
    super(items);
  }

  public static create(props: InvoiceTaxesProps): InvoiceTaxes {
    return new InvoiceTaxes(props);
  }

  public getTaxesAmount(taxableAmount: InvoiceAmount): InvoiceAmount {
    return this.getAll().reduce(
      (total, tax) => total.add(tax.getTaxAmount(taxableAmount)),
      InvoiceAmount.zero(taxableAmount.currencyCode)
    ) as InvoiceAmount;
  }

  public getCodesToString(): string {
    return this.getAll()
      .map((taxItem) => taxItem.tax.code)
      .join(", ");
  }
}
