/**
 * Created by CSL on 2017-06-06.
 * public functions.
 */

function deleteEmptyObject(arr) {
    function isEmptyObject(e) {
        var t;
        for (t in e)
            return !1;
        return !0
    };

    for (var i = 0; i < arr.length; i++) {
        if (isEmptyObject(arr[i])) {
            arr.splice(i, 1);
            i = i - 1;
        };
    };
};

((factory) => {
    if (typeof module !== 'undefined') {
        module.exports = factory();
    } else {
        window.commonUtil = factory();
    }
})(() => {
    // 是否定义
    function isDef(val) {
        return typeof val !== 'undefined' && val !== null;
    }
    // 是否空值
    function isEmptyVal(val) {
        return val === null || val === undefined || val === '';
    }
    // 是否数值
    function isNumber(val) {
        return !isEmptyVal(val) && !isNaN(val);
    }
    // 是否是有值得对象（至少要有一个可枚举属性）
    function isNotEmptyObject(obj) {
        return Object.prototype.toString.call(obj) === '[object Object]' && Object.keys(obj).length > 0;
    }
    // 是否近似相等(null = undefined = '', 1 = '1'...)
    function similarEqual(a, b) {
        // null == '' 为false，所以不能用非严等
        if (isEmptyVal(a) && isEmptyVal(b)) {
            return true;
        }
        return a == b;
    }
    // 递归获取必填项(基本信息、工程特征)
    function getRequired(rst, datas) {
        if (!datas) {
            return rst;
        }
        for (const data of datas) {
            const required = typeof data.required === 'string' ? JSON.parse(data.required) : data.required;
            const readOnly = typeof data.readOnly === 'string' ? JSON.parse(data.readOnly) : data.readOnly;
            if (required && !readOnly) {
                rst.push(data);
            }
            if (data.items && data.items.length) {
                getRequired(rst, data.items);
            }
        }
        return rst;
    }
    // 将树数据排序好
    function getSortedTreeData(rootID, items) {
        return sortSameDedth(rootID, items).reverse();
        
        function sortSameDedth(parentID, items) {
            const sameDepthItems = items.filter(item => item.ParentID === parentID);
            if (!sameDepthItems.length) {
                return [];
            }
            const NextIDMapping = {};
            sameDepthItems.forEach(item => NextIDMapping[item.NextSiblingID] = item);
            let curItem = sameDepthItems.length > 1 ? sameDepthItems.find(item => item.NextSiblingID === -1) : sameDepthItems[0];
            const sorted = [];
            while (curItem) {
                sorted.push(...sortSameDedth(curItem.ID, items));
                sorted.push(curItem);
                curItem = NextIDMapping[curItem.ID] || null;
            }
            return sorted;
        }
    }
    
    // 控制全屏(浏览器有限制)
    // Element.requestFullscreen的全屏和“F11”的全屏是不一样的。前者是将相关Element变成全屏显示。后者是将浏览器导航、标签等隐藏。
    // Fullscreen API对于全屏的判断和监听都是基于Element.requestFullscreen的，比如Document.fullscreenElement。通过F11触发的全屏Document.fullscreenElement返回null，无法正确返回全屏状态。
    // F11全屏后，无法通过Fullscreen API对全屏状态判断，会导致F11全屏后点击按钮变成了再次调用api全屏。因此，使用window.innerHeight和window.screen.height作为判断。（打开了控制台后，此方法可能会失效：无法正确或缺innerHeight)
    // 通过F11打开全屏后，没有办法通过代码退出全屏，只能通过F11退出：
    // https://stackoverflow.com/questions/51114885/combining-requestfullscreen-and-f11; https://stackoverflow.com/questions/43392583/fullscreen-api-not-working-if-triggered-with-f11/44368592#44368592;
    function handleFullscreen() {
        if (isFullscreen()) {
            const p = exitFullscreen();
            if (Object.prototype.toString.call(p) === '[object Promise]') {
                p.catch(() => alert('按F11即可退出全屏模式'));
            }
        } else {
            fullscreen(document.documentElement);
        }
    }
    function isFullscreen() {
        return window.innerHeight === window.screen.height;
    }
    function fullscreen(ele) {
        if (ele.requestFullscreen) {
            ele.requestFullscreen();
        } else if (ele.mozRequestFullscreen) {
            ele.mozRequestFullScreen();
        } else if (ele.webkitRequestFullscreen) {
            ele.webkitRequestFullscreen();
        } else if (ele.msRequestFullscreen) {
            ele.msRequestFullscreen();
        }
    }
    function exitFullscreen() {
        if(document.exitFullscreen) {
            return document.exitFullscreen();
        } else if(document.mozCancelFullscreen) {
            return document.mozCancelFullscreen();
        } else if(document.webkitExitFullscreen) {
            return document.webkitExitFullscreen();
        } else if(document.msExitFullscreen) {
            return document.msExitFullscreen();
        }
    }

    return {
        isDef,
        isEmptyVal,
        isNumber,
        similarEqual,
        getRequired,
        getSortedTreeData,
        isNotEmptyObject,
        handleFullscreen
    };
});
