import { _decorator, Component, error, Node, resources } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ResourceUtil')
export class ResourceUtil{
    public static loadRes (url: string, type: any, cb: Function = ()=>{}) {
        resources.load(url, (err: any, res: any)=>{
            if (err) {
                error(err.message || err);
                cb(err, res);
                return;
            }

            cb && cb(null, res);
        })
    }
    /**
     * 移除数组中predicate（断言）返回为真值的所有元素
     * @param {Array} array  一个用来迭代的集合.
     * @param {Function} predicate  一个迭代函数
     * @returns 
     */
    public static remove(array: any[], predicate: Function) {
        var result: any[] = [];
        var indexes: any[] = [];
        array.forEach(function (item, index) {
            if (predicate(item)) {
                result.push(item);
                indexes.push(index);
            }
        });

        ResourceUtil._basePullAt(array, indexes);
        return result;
    }

    
    private static _basePullAt(array: any[], indexes: any[]) {
        var length = array ? indexes.length : 0;
        var lastIndex = length - 1;
        var previous;

        while (length--) {
            var index = indexes[length];
            if (length === lastIndex || index !== previous) {
                previous = index;
                Array.prototype.splice.call(array, index, 1);
            }
        }

        return array;
    }
}


