/** @odoo-module */

import { patch } from "@web/core/utils/patch";
import { NumberPopup } from "@point_of_sale/app/components/popups/number_popup/number_popup";
import { TicketScreen } from "@point_of_sale/app/screens/ticket_screen/ticket_screen";
// jsonrpc removed - not used and causing dependency error in Odoo 19

import { AlertDialog as ErrorPopup } from "@web/core/confirmation_dialog/confirmation_dialog";
import { _t } from "@web/core/l10n/translation";
//Refund password validation and popup
patch(TicketScreen.prototype, {
 async onDoRefund() {
        const order = this.getSelectedOrder();
        // Check if order is posted
        if (order.state == 'done') {
            this.env.services.popup.add(ErrorPopup,{
                body : _t('Refund Not Allowed, This order has already been posted and cannot be refunded.'),
                });
            return;
        }

        var refund="";
        var session_refund= false;
        var data = await this.env.services.orm.call("pos.config", "fetch_global_refund_security", [])
        refund = data;
        if (!refund){
            session_refund = this.pos.config.refund_security;
            }
        if(refund){
            const { confirmed, payload } = await this.env.services.popup.add(NumberPopup, { isPassword: true })
            if(refund == payload){
                super.onDoRefund(...arguments);
            }
            else{
                    this.env.services.popup.add(ErrorPopup,{
                    body : _t('Invalid Password, Enter your global password'),
                    });
                }
        }
        else if(session_refund){
            const { confirmed, payload } = await this.env.services.popup.add(NumberPopup, { isPassword: true })
            if(session_refund == payload){
                super.onDoRefund(...arguments);
            }
            else{
                    this.env.services.popup.add(ErrorPopup,{
                    body : _t('Incorrect Password'),
                    });
                }
        }
        else{
            super.onDoRefund(...arguments);
        }
    },
})
