"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint prefer-const: "off" */
/**
 * Client Quote Hooks
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework
 *
 *  Liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createQuoteStagingHook = exports.createQuotePreStagingHook = void 0;
/**
 * Create a function to hook the quote before data is staged
 *
 * @param cb - a function to call with the pre staging diff
 *
 * @return a function to hook the quote before data is staged
 */
exports.createQuotePreStagingHook = function (client) { return function (quote) {
    quote.on('preDataUpdate', function (diff) { return client.validateChange(diff); });
}; };
/**
 * Create a function to hook the quote when data is staged
 *
 * @param program   - the program we are operating on
 * @param transport - a transport to send data
 *
 * @return a function to hook the quote when data is staged
 */
exports.createQuoteStagingHook = function (client, program, transport) { return function (quote) {
    if (!program.autosave || quote.isLocked()) {
        return;
    }
    quote.on('dataUpdate', function (diff) {
        quote.invalidateAutosave();
        if (!diff || client.getUi().isSaving() || client.isNavigating()) {
            return;
        }
        var valid_diffs = Object.keys(diff).reduce(function (total, key) {
            var val = diff[key];
            return Array.isArray(val) && val.length > 0 ? total + 1 : total;
        }, 0);
        if (valid_diffs === 0) {
            return;
        }
        quote.autosave(transport);
    });
}; };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ2xpZW50UXVvdGVIb29rcy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIkNsaWVudFF1b3RlSG9va3MudHMiXSw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