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

   `_cashdro_url` was left with the 17.0 API in the 18.0 migration of
   `pos_payment_method_cashdro`: `order.selected_paymentline` no longer exists,
   so reading `.payment_method` on it throws a TypeError that the caller
   swallows as a generic connection error. Rewritten with the 18.0 accessors.
*/

import { PaymentCashdro } from "@pos_payment_method_cashdro/js/payment_cashdro.esm";
import { patch } from "@web/core/utils/patch";

patch(PaymentCashdro.prototype, {
    /**
     * @override
     * The whole body has to be replaced: the very first statement is the one
     * that breaks, so there is nothing left to delegate to `super`.
     */
    _cashdro_url() {
        // Cashdro machines don't support safe POST calls, so we're sending
        // all the data quite unsafely constantly...
        const order = this.pos.get_order();
        const method = order.get_selected_paymentline()?.payment_method_id;
        const host = method && method.cashdro_host;
        if (!host) {
            return false;
        }
        let url = `${host}/Cashdro3WS/index.php`;
        url += `?name=${method.cashdro_user}`;
        url += `&password=${method.cashdro_password}`;
        return url;
    },
});
