import { MoneyDTOHelper, PercentageDTOHelper, formatCurrency } from "@erp/core";
import { InvoiceSummaryFormData } from "./invoice-resume.form.schema";
import { CustomerInvoiceSummary } from "./invoices.api.schema";

/**
 * Convierte el DTO completo de API a datos numéricos para el formulario.
 */
export const invoiceResumeDtoToFormAdapter = {
  fromDto(dtos: CustomerInvoiceSummary[], context?: any) {
    return dtos.map(
      (dto) =>
        ({
          ...dto,

          subtotal_amount: MoneyDTOHelper.toNumber(dto.subtotal_amount),
          subtotal_amount_fmt: formatCurrency(
            MoneyDTOHelper.toNumber(dto.subtotal_amount),
            Number(dto.total_amount.scale || 2),
            dto.currency_code,
            dto.language_code
          ),

          discount_percentage: PercentageDTOHelper.toNumber(dto.discount_percentage),
          discount_percentage_fmt: PercentageDTOHelper.toNumericString(dto.discount_percentage),

          discount_amount: MoneyDTOHelper.toNumber(dto.discount_amount),
          discount_amount_fmt: formatCurrency(
            MoneyDTOHelper.toNumber(dto.discount_amount),
            Number(dto.total_amount.scale || 2),
            dto.currency_code,
            dto.language_code
          ),

          taxable_amount: MoneyDTOHelper.toNumber(dto.taxable_amount),
          taxable_amount_fmt: formatCurrency(
            MoneyDTOHelper.toNumber(dto.taxable_amount),
            Number(dto.total_amount.scale || 2),
            dto.currency_code,
            dto.language_code
          ),

          taxes_amount: MoneyDTOHelper.toNumber(dto.taxes_amount),
          taxes_amount_fmt: formatCurrency(
            MoneyDTOHelper.toNumber(dto.taxes_amount),
            Number(dto.total_amount.scale || 2),
            dto.currency_code,
            dto.language_code
          ),

          total_amount: MoneyDTOHelper.toNumber(dto.total_amount),
          total_amount_fmt: formatCurrency(
            MoneyDTOHelper.toNumber(dto.total_amount),
            Number(dto.total_amount.scale || 2),
            dto.currency_code,
            dto.language_code
          ),

          //taxes: dto.taxes,
        }) as unknown as InvoiceSummaryFormData
    );
  },
};
