/*

Siesta 5.1.0
Copyright(c) 2009-2018 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license

*/
Class('Ariadne.ExtJSComponentQueryFinder.Identifier.NthChild', {

    isa         : Ariadne.QueryFinder.Identifier,

    does        : [
        Ariadne.ExtJSComponentQueryFinder.Role.ExtJSComponentHelper
    ],

    methods : {

        identify : function (cmp, root, maze) {
            var container       = this.finder.getParent(cmp)

            if (!container || container == root) return null

            if (!container.items) return null

            var index           = container.items.indexOf(cmp)

            if (index == -1) {
                var refItems    = container.getRefItems && container.getRefItems()

                if (!refItems) return null

                var len         = refItems.length

                for (var i = 0; i < len; i++) {
                    if (refItems[ i ] === cmp) {
                        index   = i
                        break
                    }
                }

                if (i >= len) return null
            }

            return {
                query           : ':ariadne-nth-child(' + (index + 1) + ')',
                weight          : 1e6
            }
        }
    }
});