/*

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

*/
Role('Ariadne.ExtJSDomQueryFinder.Role.ExtJSHelper', {

    methods : {

        getComponentOfDomElement : function (el) {
            var doc         = el.ownerDocument
            var body        = doc.body

            var Ext         = (doc.defaultView || doc.parentWindow).Ext

            if (!Ext || !Ext.getCmp) return null

            while (el && el != body) {
                var elId    = el.getAttribute('id')

                if (elId && Ext.getCmp(elId)) return Ext.getCmp(elId)

                el        = el.parentElement
            }

            return null
        },


        getExtByDomElement : function (el) {
            var doc     = el.ownerDocument

            return (doc.defaultView || doc.parentWindow).Ext
        },


        getExtCssClassPrefixRegexp : function (el) {
            var Ext             = this.getExtByDomElement(el)

            if (!Ext) return null

            var baseCSSPrefix   = Ext.baseCSSPrefix || 'x'

            return new RegExp('^' + baseCSSPrefix)
        },


        // Ext JS 4+: Form fields sometimes get their 'name' generated based on a parent id
        // property is considered to be auto-generated if it contains an id string and id is in turn auto-generated
        valueIsAutoGeneratedByComponent : function (comp, value) {
            // Not relevant for Ext < 4
            if (!comp.up) return false;

            value                   = String(value)

            var componentsToCheck   = comp.autoGenId ? [ comp ] : []

            var parentWithAutoId    = comp.up('[autoGenId=true]');
            if (parentWithAutoId) componentsToCheck.push(parentWithAutoId)

            var childrenWithAutoId  = parentWithAutoId && parentWithAutoId.query && parentWithAutoId.query('[autoGenId=true]')
            if (childrenWithAutoId) componentsToCheck.push.apply(componentsToCheck, childrenWithAutoId)

            return componentsToCheck.some(function (comp) {
                return value.indexOf(comp.id) >= 0
            })
        },


        componentHasAutoGeneratedId : function (component, Ext) {
            var id      = component.id

            // Ext3 ?
            if (/^ext-gen\d+|^ext-comp\d+/.test(id)) {
                return true;
            }

            if (Ext && this.idIsGeneratedByExtDotIdCall(id, Ext)) return true

            if (component.isWidget) {
                return id.replace(/\d+$/, '') == component.identifiablePrefix
            }

            // even if `autoGenId` can be set to false, the id of the component can be formed from the id
            // if its parent, like "window-1019-header_hd" (id of window header), where "window-1019" is autogenerated id
            // of parent component
            return component.autoGenId || this.valueIsAutoGeneratedByComponent(component, id);
        },


        // detect ids generated with `Ext.id()`
        idIsGeneratedByExtDotIdCall : function (id, Ext, el) {
            Ext                 = Ext || this.getExtByDomElement(el)

            if (!Ext) return false

            var match           = /^(.+?)\d+$/.exec(id)

            return Boolean(match && match[ 1 ] == Ext.idPrefix)
        },


        domElementHasAutoGeneratedId : function (el) {
            var id      = el.getAttribute('id') || ''

            if (/^ext-/.test(id) || this.idIsGeneratedByExtDotIdCall(id, null, el)) return true

            // id of node in the dom can be formed from the id of the component this node belongs to
            // for example dom node `container-1019-innertCt` belonging to container-1019
            // such ids are considered auto-generated and should be ignored
            var comp    = this.getComponentOfDomElement(el)

            if (comp) {
                if ((id !== comp.id && id.indexOf(comp.id) > -1) || this.componentHasAutoGeneratedId(comp)) {
                    return true
                }
            }

            return false
        }
    }
});