/* Copyright 2026 - Today Criptomart
   License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

   The `add_paymentline` patch of `pos_payment_method_cashdro` checks
   `line.payment_method`, renamed to `payment_method_id` in 18.0, so the
   condition is never met and the line keeps the default due amount instead of
   waiting for the amount the customer actually inserts in the drawer.
*/

import { PosOrder } from "@point_of_sale/app/models/pos_order";
import { patch } from "@web/core/utils/patch";

patch(PosOrder.prototype, {
    /**
     * @override
     * Set the amount to 0 as it's going to be filled by the Cashdro response.
     */
    add_paymentline() {
        const line = super.add_paymentline(...arguments);
        if (!line) {
            return line;
        }
        if (
            line.payment_method_id?.use_payment_terminal === "cashdro" &&
            line.amount > 0 // For refund transactions we need to keep the amount
        ) {
            line.set_amount(0);
        }
        return line;
    },
});
