/** @odoo-module **/

import { patch } from "@web/core/utils/patch";
import { TaxTotalsComponent } from "@account/components/tax_totals/tax_totals";

/**
 * Patch TaxTotalsComponent to handle cases where subtotals is undefined
 * This fixes the "Invalid loop expression: 'undefined' is not iterable" error
 * that occurs when invoices have no tax configuration.
 */
patch(TaxTotalsComponent.prototype, {
    formatData(props) {
        // Call the original formatData method
        super.formatData(props);
        
        // If totals exists but subtotals is undefined, set it to empty array
        if (this.totals && this.totals.subtotals === undefined) {
            this.totals.subtotals = [];
        }
        
        // Also ensure each subtotal has tax_groups array
        if (this.totals && this.totals.subtotals) {
            for (const subtotal of this.totals.subtotals) {
                if (subtotal.tax_groups === undefined) {
                    subtotal.tax_groups = [];
                }
            }
        }
    }
});
