/**
 * This updater increments all records found on the {@link Ext.pivot.result.Base matrix result}
 * using the specified value.
 *
 * Let's say that the result object contains the following records (each record is a
 * {@link Ext.data.Model model} in fact but we use json representation for this example):
 *
 *      [
 *          { product: 'Phone', country: 'USA', order: 100 },
 *          { product: 'Tablet', country: 'USA', order: 200 }
 *      ]
 *
 * And we want to increment all orders by a fixed value of 50. This is how the updater config
 * looks like:
 *
 *      {
 *          type: 'increment',
 *          leftKey: resultLeftKey,
 *          topKey: resultTopKey,
 *          matrix: matrix,
 *          dataIndex: 'order',
 *          value: 50
 *      }
 *
 * And this is how the records look after the update:
 *
 *      [
 *          { product: 'Phone', country: 'USA', order: 150 },
 *          { product: 'Tablet', country: 'USA', order: 250 }
 *      ]
 *
 */
Ext.define('Ext.pivot.update.Increment', {
    extend: 'Ext.pivot.update.Base',

    alias: 'pivotupdate.increment',

    onUpdate: function(result, resolve, reject) {
        // This could fire asynchronously after we've been destroyed
        if (this.destroyed) {
            return;
        }

        // eslint-disable-next-line vars-on-top
        var dataIndex = this.getDataIndex(),
            value = parseFloat(this.getValue()),
            records = result.records,
            len, i, rec;

        if (isNaN(value)) {
            value = null;
        }

        if (records && value) {
            len = records.length;

            for (i = 0; i < len; i++) {
                rec = records[i];
                rec.set(dataIndex, rec.get(dataIndex) + value);
            }
        }

        this.callParent([result, resolve, reject]);
    }
});
