/*
* @name:
* @description:
* @Author: huifen.ling
* @Date: 2019-02-03 14:45:00
 * @Last Modified by: huifen.ling
 * @Last Modified time: 2019-05-30 15:54:16
*/
export function getCursorPosition(element) {
    if (document.selection) {
        let selectRange = document.selection.createRange();
        selectRange.moveStart('character', -element.value.length);
        return selectRange.text.length;
    } else if (element.selectionStart || element.selectionStart == '0') {
        return element.selectionStart;
    }
}

export function setCursorPosition(ele, pos) {
    if (ele.setSelectionRange) {
        ele.focus();
        ele.setSelectionRange(pos, pos);
    } else if (ele.createTextRange) {
        let range = ele.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

export function getElementTop(domOrQuery) {
    let dom = null;
    if (typeof domOrQuery === 'string') {
        dom = document.querySelector(domOrQuery);
    } else if (domOrQuery.nodeType) {
        dom = domOrQuery;
    }
    if (!dom) {
        console.error('getElementTop 传入的参数不正确');
        return 0;
    }
    let top = 0;
    while (dom.offsetParent) {
        top += dom.offsetTop;
        dom = dom.offsetParent;
    }
    return top;
}

export function lockScroll() {
    const dom = document.documentElement || document.body;
    dom.style.overflow = 'hidden';
}

export function unlockScroll() {
    const dom = document.documentElement || document.body;
    dom.style.overflow = 'auto';
}

export function preventDefault(ev) {
    ev.preventDefault();
}

export function queryDom(queryOrEle) {
    let dom = null;
    if (typeof queryOrEle === 'string') {
        dom = document.querySelector(queryOrEle);
    } else if (queryOrEle.nodeType) {
        dom = queryOrEle;
    }
    if (!dom) {
        console.error('找不到dom');
    }
    return dom;
}

/**
 * @description
 * @export
 * @param {*} queryOrEle
 * @param {*} callback
 * @param {*} delay
 * @returns 返回取消监听事件的函数
 */
export function longPress(queryOrEle, callback, delay) {
    let dom = queryDom(queryOrEle);
    if (!dom) {
        console.error('longpress 没有获取到dom');
        return;
    }
    let timer = null;
    let startX = 0;
    let startY = 0;
    const timerDelay = delay || 600;
    const startListener = (ev) => {
        const touch = ev.targetTouches || ev.touches;
        startX = touch[0].clientX;
        startY = touch[0].clientY;
        timer = setTimeout(() => {
            callback(ev);
            timer = null;
        }, timerDelay);
    };
    const moveListener = ev => { // 如果移动距离过大，就阻止事件的发生
        const touch = ev.targetTouches || ev.touches;
        const moveX = touch[0].clientX;
        const moveY = touch[0].clientY;
        if (Math.abs(moveX - startX) > 10 || Math.abs(moveY - startY) > 10) {
            if (timer) {
                clearTimeout(timer);
            } else {
                ev.stopPropagation();
            }
        }
    }
    const endListener = (ev) => {
        if (timer) {
            clearTimeout(timer);
        } else {
            ev.stopPropagation();
        }
    };
    dom.addEventListener('touchstart', startListener);
    dom.addEventListener('touchmove', moveListener);
    dom.addEventListener('touchend', endListener);
    return function () {
        dom.removeEventListener('touchstart', startListener);
        dom.removeEventListener('touchmove', moveListener);
        dom.removeEventListener('touchend', endListener);
    };
}


export function searchElement(list, query) {
    return list.find(ele => {
        return queryMatch(ele, query);
    });
}

export function getClosestParent(ele, parentQuery) {
    if (ele.closest) {
        return ele.closest(parentQuery);
    } else {
        let parent = ele;
        while (parent && !queryMatch(parent, parentQuery)) {
            parent = parent.parentNode;
        }
        return ele;
    }
}

export function getElementIndex() {

}

export function queryMatch(ele, query) {
    if (ele.matches) {
        return ele.matches(query);
    }
    if (ele.matchesSelector) {
        return ele.matchesSelector(query);
    }
    if (query[0] === '.') {
        return ~[].indexOf.call(ele.classList || [], query.slice(1));
    } else if (query[0] === '#') {
        return ele.id === query.slice(1);
    } else {
        return ele.tagName === query.toUpperCase();
    }
}

export function convertPxToVW(px){

}