//#import $owl_mall_u:services/modelService.jsx
//#import $owl_mall_u_category:services/modelService.jsx
//#import $owl_mall_category:services/modelService.jsx
//#import $owl_org_category:services/modelService.jsx


var CommonUtilEx = (function () {
    var f = {
        getParentIds: function (cat, catType, modelService) {
            if (cat.parentPathIds) {
                var catIds = [].concat(cat.parentPathIds);
                catIds.push(cat.id);
                return catIds;
            } else {
                var pcat = modelService.get(cat.id);
                var catIds = [];
                while (pcat) {

                    catIds.push(pcat.id);
                    if (pcat.parent_id && pcat.parent_id != '0') {
                        pcat = modelService.get(pcat.parent_id);
                    }
                    if (catIds.indexOf(pcat.id) > -1 || catIds.length > 20) {
                        break;
                    }
                }
                return catIds;
            }


        },


        getUserCatIds(userId) {
            var user = owl_mall_uService.get(userId);

            if (!user) {
                return [];
            }

            var catIds = [];
            var user_categories = user.user_categories;
            if (!user_categories) {
                return [];
            }

            user_categories.forEach(function (cat) {
                var parentIds = f.getParentIds(cat, "user", owl_mall_u_categoryService);
                if (parentIds) {
                    parentIds.forEach(function (catId) {
                        if (catIds.indexOf(catId) < 0) {
                            catIds.push(catId);
                        }
                    });
                }
            });

            return catIds;
        },

        shuffleArray(array) {
            for (var i = array.length - 1; i > 0; i--) {
                var j = Math.floor(Math.random() * (i + 1));
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }

            array.sort(function (a, b) {
                return b.priority || 0 - a.priority || 0;
            });
        },


        generatePermutations(n, maxCount) {
            var arr = [];
            for (var i = 0; i < n; i++) {
                arr.push(i);
            }

            var results = [];
            var uniquePermutations = {};

            function permute(array, start) {
                if (start === array.length - 1) {
                    var permutation = array.slice();
                    var key = permutation.join(',');
                    if (!uniquePermutations[key]) {
                        results.push(permutation);
                        uniquePermutations[key] = true;
                    }
                    return;
                }
                for (var i = start; i < array.length; i++) {
                    var temp = array[start];
                    array[start] = array[i];
                    array[i] = temp;

                    permute(array, start + 1);

                    temp = array[start];
                    array[start] = array[i];
                    array[i] = temp;
                }
            }

            function shuffleAndAddUnique() {
                while (results.length < maxCount) {
                    var shuffledArray = arr.slice();
                    for (var i = n - 1; i > 0; i--) {
                        var j = Math.floor(Math.random() * (i + 1));
                        var temp = shuffledArray[i];
                        shuffledArray[i] = shuffledArray[j];
                        shuffledArray[j] = temp;
                    }
                    var key = shuffledArray.join(',');
                    if (!uniquePermutations[key]) {
                        results.push(shuffledArray);
                        uniquePermutations[key] = true;
                    }
                }
            }

            if (n >= 5) {
                shuffleAndAddUnique();
            } else {
                permute(arr, 0);
            }

            return results;
        },


        removeNull(obj) {
            if (typeof obj != "object") {
                return;
            }
            if (!obj) {
                return;
            }
            for (var k in obj) {
                if (obj[k] == null) {
                    delete obj[k];
                } else if (typeof obj[k] == "object" && Array.isArray(obj[k])) {
                    for (var i = 0; i < obj[k].length; i++) {
                        f.removeNull(obj[k][i]);
                    }
                } else if (typeof obj[k] == "object" && !Array.isArray(obj[k])) {
                    f.removeNull(obj[k]);
                }
            }
        },
        roundMoney(money) {
            if (typeof money === "undefined") {
                return null;
            }

            money = Number(money);
            return Number((money + 0.00001).toFixed(2));
        }

    }
    return f;
})();

