define(['./forOwn', '../lang/isArray'], function(forOwn, isArray) {

    /**
     * Description
     * @method containsMatch
     * @param {} array
     * @param {} pattern
     * @return Literal
     */
    function containsMatch(array, pattern) {
        var i = -1, length = array.length;
        while (++i < length) {
            if (deepMatches(array[i], pattern)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Description
     * @method matchArray
     * @param {} target
     * @param {} pattern
     * @return Literal
     */
    function matchArray(target, pattern) {
        var i = -1, patternLength = pattern.length;
        while (++i < patternLength) {
            if (!containsMatch(target, pattern[i])) {
                return false;
            }
        }

        return true;
    }

    /**
     * Description
     * @method matchObject
     * @param {} target
     * @param {} pattern
     * @return result
     */
    function matchObject(target, pattern) {
        var result = true;
        forOwn(pattern, function(val, key) {
            if (!deepMatches(target[key], val)) {
                // Return false to break out of forOwn early
                return (result = false);
            }
        });

        return result;
    }

    /**
     * Recursively check if the objects match.
     */
    /**
     * Description
     * @method deepMatches
     * @param {} target
     * @param {} pattern
     * @return 
     */
    function deepMatches(target, pattern){
        if (target && typeof target === 'object') {
            if (isArray(target) && isArray(pattern)) {
                return matchArray(target, pattern);
            } else {
                return matchObject(target, pattern);
            }
        } else {
            return target === pattern;
        }
    }

    return deepMatches;

});
