"use strict";
/**
 * @file: diff and patch
 * @author: yangqianjun
 * @Date: 2019-12-31 13:48:32
 * @LastEditors: yangqianjun
 * @LastEditTime: 2020-01-03 15:02:33
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var lodash_1 = tslib_1.__importDefault(require("lodash"));
// @IMP: 确保浏览器和node使用相同的版本
// eslint-disable-next-line @typescript-eslint/no-var-requires
var realDiff = require('../../static/jsondiffpatch.umd');
var splitor = '__@__@__';
function diffAndPatch(curVersion, newVersion) {
    var delta = realDiff.diff(curVersion, newVersion);
    return {
        delta: delta,
        /** 按需更新 */
        selectDelta: function (keys) {
            var initialMap = {};
            var setKeyMap = {};
            var newDelta = keys.reduce(function (newDelta, indexs) {
                var val = lodash_1.default.get(delta, indexs);
                var newIndexs = indexs;
                var curSetKey = newIndexs.join(splitor);
                var parentIndexes = indexs.slice(0, indexs.length - 1);
                parentIndexes.reduce(function (curKeys, curKey) {
                    curKeys.push(curKey);
                    var key = curKeys.join(splitor);
                    if (!(key in initialMap)) {
                        var curValue = lodash_1.default.get(curVersion, curKeys);
                        var newValue = lodash_1.default.get(newVersion, curKeys);
                        if ((curValue && newValue && Array.isArray(curValue) && Array.isArray(newValue)) ||
                            (curValue && !newValue && Array.isArray(curValue)) ||
                            (!curValue && newValue && Array.isArray(newValue))) {
                            lodash_1.default.setWith(newDelta, curKeys.concat(['_t']), 'a', Object);
                        }
                        initialMap[key] = '';
                    }
                    return curKeys;
                }, []);
                if (!val) {
                    throw "\u5DE5\u5177\u51FA bug \u4E86";
                }
                else {
                    lodash_1.default.setWith(newDelta, newIndexs, val, Object);
                    setKeyMap[curSetKey] = '';
                }
                return newDelta;
            }, {});
            return newDelta;
        },
        /** 更新 */
        patch: function (d) {
            if (d) {
                realDiff.patch(curVersion, d);
                return curVersion;
            }
            return void 0;
        }
    };
}
exports.diffAndPatch = diffAndPatch;
//# sourceMappingURL=diff.js.map