const handsonUtils = {
    /**
     * 获取被排序后的数组
     *
     * @param changeData 发生改变的数据
     * @param source     数组
     *
     * @return array     经过排序后或者经过搜索后的数组
     */
    getParam: function(changeData, source, hot, trimmedArr) {
        function arrayUnique(arr, name){ //去重
            var hash = {};
            return arr.reduce(function(item, next) {
                hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
                return item;
            }, []);
        }
        let newData = arrayUnique(changeData, 0)
        let param = "";
        //被筛选过后的数组
        // let trimmedArr = this.trimmedRows();
        //是否启用了排序
        let isSort = hot.getPlugin("columnSorting").isSorted();
        if (trimmedArr.length && isSort) {
            //排序后的数组
            let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
            param = newData.map(item => {
                return hot.getSourceDataAtRow(trimmedArr[sortArr[item[0]]]);
            });
        } else if (isSort) {
            //排序后的数组
            let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
            param = newData.map(item => {
                return hot.getSourceDataAtRow(sortArr[item[0]]);
            });
        } else if (trimmedArr.length) {
            param = newData.map(item => {
                return hot.getSourceDataAtRow(trimmedArr[item[0]]);
            });
        } else {
            param = newData.map(item => {
                return hot.getSourceDataAtRow(item[0]);
            });
        }
        return param;
    },

    /**
     * 
     * @param {handsontable修改参数} changeData 
     * @param {*} source 
     * @param {handsontabele实例} hot 
     * @param {排序数组} trimmedArr
     * 
     * @return 修改数值的前一个对象 
     */
    getUnshiftParam: function(changeData, source, hot, trimmedArr) {
        //是否启用了排序
        let isSort = hot.getPlugin("columnSorting").isSorted();
        if (trimmedArr.length && isSort) {
            //排序后的数组
            let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
            return hot.getSourceDataAtRow(trimmedArr[sortArr[changeData[0][0] - 1]])
        } else if (isSort) {
            //排序后的数组
            let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
            return hot.getSourceDataAtRow(sortArr[changeData[0][0] - 1])
        } else if (trimmedArr.length) {
            //进行了筛选
            return hot.getSourceDataAtRow(trimmedArr[changeData[0][0] - 1])
        } else {
            //没有进行排序和筛选
            return hot.getSourceDataAtRow(changeData[0][0] - 1);
        }
    },
}

export default handsonUtils