import { IPresenterOutputParams, Presenter } from "@erp/core/api";
import { GetCustomerInvoiceByIdResponseDTO } from "@erp/customer-invoices/common";
import { ArrayElement } from "@repo/rdx-utils";
import { FormatMoneyOptions, formatMoneyDTO, formatQuantityDTO } from "../../helpers";

type CustomerInvoiceItemsDTO = GetCustomerInvoiceByIdResponseDTO["items"];
type CustomerInvoiceItemDTO = ArrayElement<CustomerInvoiceItemsDTO>;

export class CustomerInvoiceItemsReportPersenter extends Presenter<
  CustomerInvoiceItemsDTO,
  unknown
> {
  private _locale!: string;

  private _mapItem(invoiceItem: CustomerInvoiceItemDTO, index: number) {
    const moneyOptions: FormatMoneyOptions = {
      locale: this._locale,
      hideZeros: true,
      newScale: 2,
    };

    return {
      ...invoiceItem,

      quantity: formatQuantityDTO(invoiceItem.quantity),
      unit_amount: formatMoneyDTO(invoiceItem.unit_amount, moneyOptions),
      subtotal_amount: formatMoneyDTO(invoiceItem.subtotal_amount, moneyOptions),
      //      discount_percetage: formatPercentageDTO(invoiceItem.discount_percentage, this._locale),
      discount_amount: formatMoneyDTO(invoiceItem.discount_amount, moneyOptions),
      taxable_amount: formatMoneyDTO(invoiceItem.taxable_amount, moneyOptions),
      taxes_amount: formatMoneyDTO(invoiceItem.taxes_amount, moneyOptions),
      total_amount: formatMoneyDTO(invoiceItem.total_amount, moneyOptions),
    };
  }

  toOutput(invoiceItems: CustomerInvoiceItemsDTO, params: IPresenterOutputParams): unknown {
    const { locale } = params as {
      locale: string;
    };

    this._locale = locale;

    return invoiceItems.map((item, index) => {
      return this._mapItem(item, index);
    });
  }
}
