<commit_before>import {LightningElement, api, track, wire} from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import GeFormService from 'c/geFormService';
import GeLabelService from 'c/geLabelService';
import { isNumeric, isNotEmpty, getLikeMatchByKey } from 'c/utilCommon';
import { registerListener } from 'c/pubsubNoPageRef';

import ALLOCATION_OBJECT from '@salesforce/schema/Allocation__c';
import GENERAL_ACCOUNTING_UNIT_FIELD from '@salesforce/schema/Allocation__c.General_Accounting_Unit__c';
import AMOUNT_FIELD from '@salesforce/schema/Allocation__c.Amount__c';
import PERCENT_FIELD from '@salesforce/schema/Allocation__c.Percent__c';
const GENERAL_ACCOUNT_UNIT = GENERAL_ACCOUNTING_UNIT_FIELD.fieldApiName;

import ALLOC_DEFAULT_FIELD from '@salesforce/schema/Allocations_Settings__c.Default__c';
import ALLOC_DEFAULT_ALLOCATIONS_ENABLED_FIELD from '@salesforce/schema/Allocations_Settings__c.Default_Allocations_Enabled__c';
const ALLOC_SETTINGS_DEFAULT = ALLOC_DEFAULT_FIELD.fieldApiName;
const ALLOC_SETTINGS_DEFAULT_ALLOCATIONS_ENABLED = ALLOC_DEFAULT_ALLOCATIONS_ENABLED_FIELD.fieldApiName;

export default class GeFormWidgetAllocation extends LightningElement {
    @api element;
    @track alertBanner = {}; // { level: ('error', 'warning'), message: String }
    @track rowList = [];
    @track fieldList = [];
    @track value;
    @track allocationSettings;
    @track _totalAmount;

    CUSTOM_LABELS = GeLabelService.CUSTOM_LABELS;

    // need labels for field list
    @wire(getObjectInfo, { objectApiName: ALLOCATION_OBJECT })
    wiredObjectInfo({data, error}) {
        // Represents the fields in a row of the widget
        if(data) {
            this.fieldList = [
                {
                    mappedField: `${ALLOCATION_OBJECT.objectApiName}.${GENERAL_ACCOUNTING_UNIT_FIELD.fieldApiName}`,
                    size: 4,
                    element: {
                        required: true,
                        customLabel: data.fields[GENERAL_ACCOUNTING_UNIT_FIELD.fieldApiName].label
                    }
                },
                {
                    mappedField: `${ALLOCATION_OBJECT.objectApiName}.${AMOUNT_FIELD.fieldApiName}`,
                    size: 3,
                    element: {
                        customLabel: data.fields[AMOUNT_FIELD.fieldApiName].label,
                    }
                },
                {
                    mappedField: `${ALLOCATION_OBJECT.objectApiName}.${PERCENT_FIELD.fieldApiName}`,
                    size: 3,
                    element: {
                        customLabel: data.fields[PERCENT_FIELD.fieldApiName].label
                    }
                }
            ];
        }
    }

    /**
    * @description Initializes the component
    */
    connectedCallback() {
        registerListener('allocationValueChange', this.handleValueChange, this);
        this.init();
    }


    init = async () => {
        this.allocationSettings = await GeFormService.getAllocationSettings();
        if(this.hasDefaultGAU) {
            this.addRow(true);
        }
    };

    @api
    get totalAmount() {
       return this._totalAmount;
    }

    /**
     * Handle changes in the total donation amount.
     * @param value
     */
    set totalAmount(value) {
        this._totalAmount = value;
        if(value >= 0 && this.hasDefaultGAU) {
            // handle percentage allocations first
            // value updates don't propagate down to child nodes, so we need to pass the new Total Amount down
            this.reallocateByPercent(value);
            // assign remainder to default GAU
            this.allocateDefaultGAU();
        }
    }

    /**
     * Expected to return true if widget fields are valid, false otherwise
     * @return Boolean
     */
    @api
    isValid() {

        let fieldIsValid = true;
        if(this.element.required) {
            return this.value !== null 
                && typeof this.value !== 'undefined' 
                && this.value !== ''
                && fieldIsValid;
        }
        return fieldIsValid;
    }

    /**
     * Expected to return a map of Object API Name to array of records to be created from this widget
     * @return {'GAU_Allocation_1_abc123' : [record1, record2, ...] }
     */
    @api
    returnValues() {<commit_msg>Validate when total amount is updated<commit_after>import {LightningElement, api, track, wire} from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import GeFormService from 'c/geFormService';
import GeLabelService from 'c/geLabelService';
import { isNumeric, isNotEmpty, getLikeMatchByKey } from 'c/utilCommon';
import { registerListener } from 'c/pubsubNoPageRef';

import ALLOCATION_OBJECT from '@salesforce/schema/Allocation__c';
import GENERAL_ACCOUNTING_UNIT_FIELD from '@salesforce/schema/Allocation__c.General_Accounting_Unit__c';
import AMOUNT_FIELD from '@salesforce/schema/Allocation__c.Amount__c';
import PERCENT_FIELD from '@salesforce/schema/Allocation__c.Percent__c';
const GENERAL_ACCOUNT_UNIT = GENERAL_ACCOUNTING_UNIT_FIELD.fieldApiName;

import ALLOC_DEFAULT_FIELD from '@salesforce/schema/Allocations_Settings__c.Default__c';
import ALLOC_DEFAULT_ALLOCATIONS_ENABLED_FIELD from '@salesforce/schema/Allocations_Settings__c.Default_Allocations_Enabled__c';
const ALLOC_SETTINGS_DEFAULT = ALLOC_DEFAULT_FIELD.fieldApiName;
const ALLOC_SETTINGS_DEFAULT_ALLOCATIONS_ENABLED = ALLOC_DEFAULT_ALLOCATIONS_ENABLED_FIELD.fieldApiName;

export default class GeFormWidgetAllocation extends LightningElement {
    @api element;
    @track alertBanner = {}; // { level: ('error', 'warning'), message: String }
    @track rowList = [];
    @track fieldList = [];
    @track value;
    @track allocationSettings;
    @track _totalAmount;

    CUSTOM_LABELS = GeLabelService.CUSTOM_LABELS;

    // need labels for field list
    @wire(getObjectInfo, { objectApiName: ALLOCATION_OBJECT })
    wiredObjectInfo({data, error}) {
        // Represents the fields in a row of the widget
        if(data) {
            this.fieldList = [
                {
                    mappedField: `${ALLOCATION_OBJECT.objectApiName}.${GENERAL_ACCOUNTING_UNIT_FIELD.fieldApiName}`,
                    size: 4,
                    element: {
                        required: true,
                        customLabel: data.fields[GENERAL_ACCOUNTING_UNIT_FIELD.fieldApiName].label
                    }
                },
                {
                    mappedField: `${ALLOCATION_OBJECT.objectApiName}.${AMOUNT_FIELD.fieldApiName}`,
                    size: 3,
                    element: {
                        customLabel: data.fields[AMOUNT_FIELD.fieldApiName].label,
                    }
                },
                {
                    mappedField: `${ALLOCATION_OBJECT.objectApiName}.${PERCENT_FIELD.fieldApiName}`,
                    size: 3,
                    element: {
                        customLabel: data.fields[PERCENT_FIELD.fieldApiName].label
                    }
                }
            ];
        }
    }

    /**
    * @description Initializes the component
    */
    connectedCallback() {
        registerListener('allocationValueChange', this.handleValueChange, this);
        this.init();
    }


    init = async () => {
        this.allocationSettings = await GeFormService.getAllocationSettings();
        if(this.hasDefaultGAU) {
            this.addRow(true);
        }
    };

    @api
    get totalAmount() {
       return this._totalAmount;
    }

    /**
     * Handle changes in the total donation amount.
     * @param value
     */
    set totalAmount(value) {
        this._totalAmount = value;
        if(value >= 0 && this.hasDefaultGAU) {
            // handle percentage allocations first
            // value updates don't propagate down to child nodes, so we need to pass the new Total Amount down
            this.reallocateByPercent(value);
            // assign remainder to default GAU
            this.allocateDefaultGAU();
            this.validate();
        }
    }

    /**
     * Expected to return true if widget fields are valid, false otherwise
     * @return Boolean
     */
    @api
    isValid() {

        let fieldIsValid = true;
        if(this.element.required) {
            return this.value !== null 
                && typeof this.value !== 'undefined' 
                && this.value !== ''
                && fieldIsValid;
        }
        return fieldIsValid;
    }

    /**
     * Expected to return a map of Object API Name to array of records to be created from this widget
     * @return {'GAU_Allocation_1_abc123' : [record1, record2, ...] }
     */
    @api
    returnValues() {
{'fraction_non_alphanumeric': 0.09300194708509907, 'fraction_numerical': 0.0022906883518497307, 'mean_word_length': 2.2436849925705795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4226780', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2360, 'n_words': 512}