// Fusion Accounting - Bank Reconciliation Monetary Field
// Copyright (C) 2026 Nexa Systems Inc.

import { MonetaryField, monetaryField } from "@web/views/fields/monetary/monetary_field";

/**
 * BankRecMonetaryField - Extended monetary field that automatically
 * applies sign inversion for forced negative values during reconciliation.
 */
export class BankRecMonetaryField extends MonetaryField{
    static template = "fusion_accounting.BankRecMonetaryField";
    static props = {
        ...MonetaryField.props,
        hasForcedNegativeValue: { type: Boolean },
    };

    /** Override **/
    get inputOptions(){
        const options = super.inputOptions;
        const parse = options.parse;
        options.parse = (value) => {
            let parsedValue = parse(value);
            if (this.props.hasForcedNegativeValue) {
                parsedValue = -Math.abs(parsedValue);
            }
            return parsedValue;
        };
        return options;
    }

    /** Override **/
    get value() {
        let value = super.value;
        if(this.props.hasForcedNegativeValue){
            value = Math.abs(value);
        }
        return value;
    }
}

export const bankRecMonetaryField = {
    ...monetaryField,
    component: BankRecMonetaryField,
};
