var o_stored = {};

importScripts(
    "/portfolio/app/worker/Util.js",
    "/portfolio/app/worker/Methods.js",
)

addEventListener("message", function(i_event) {
    
    var o_sendt = Ext.decode(i_event.data, true) || i_event.data;
    var o_view = o_sendt.view;
    var s_entity = o_sendt.entity;
    var s_property = o_sendt.property;
    var a_definitions = o_sendt.definitions;

    var a_forms = Ext.Array.filter(a_definitions, function(o_def) {
        return o_def.type == "formCollection";
    });

    var a_layouts = Ext.clone(o_view.layouts || []);

    Ext.Array.each(a_forms, function(o_fc) {
        
        var s_value = o_fc.name || o_fc.fcid;

        var o_viewLayout = Ext.Array.findBy(a_layouts, function(o_layout) {
            return o_layout.fcid == s_value;
        });

        var o_viewClone = Ext.clone(o_viewLayout || o_view);
        
        var o_collection = Ext.Array.findBy(a_definitions, function(o_definition) {
            return (o_definition.name == s_value || o_definition.id == s_value)  && o_definition.type == "formCollection";
        });

        if (o_collection.extend) {

            var a_recursive = Portfolio.pd.util.Handler.getExtendedFormCollection([s_value], a_definitions);

            a_recursive.reverse();

            Ext.Array.each(a_recursive, function(o_config) {

                var o_layout = Ext.Array.findBy(a_layouts, function(o_search) {
                    return o_search.fcid == o_config.name || o_search.fcid == o_config.fcid;
                });

                if (o_layout) {

                    var o_layoutClone = Ext.clone(o_layout);
                    
                    Ext.Object.each(o_layoutClone, function(s_key, value) {
                        var temp;
                        if (Ext.isArray(value)) {
                            temp = Ext.clone(Ext.Array.from(o_viewClone[s_key]));
                            
                            var a_merged = Ext.Array.merge(Ext.Array.clone(value), temp);

                            var a_searched = [];

                            var a_filtered = Ext.Array.filter(a_merged, function(value) {

                                var b_found = false;
                                var s_search = value;

                                if (Ext.isObject(value)) {

                                    if (value.type) {
                                        switch(value.type) {
                                            case "property":
                                                s_search = value.name;
                                                break;
                                            case "collection":
                                            case "group":
                                                s_search = value.itemId;
                                                break;
                                        }
                                    }
                                    else {
                                        s_search = value.itemId || value.name;
                                    }
                                }

                                b_found = a_searched.indexOf(s_search) > -1;

                                if (!b_found) {
                                    a_searched.push(s_search);
                                }

                                return !b_found;
                            });

                            o_viewClone[s_key] = a_filtered;

                        }
                        if (Ext.isObject(value)) {
                            temp = Ext.clone(o_viewClone[s_key]) || {};
                            o_viewClone[s_key] = Ext.Object.merge(Ext.clone(value), temp);
                        }
                    });

                    Ext.Object.mergeIf(o_viewClone, o_layout);
                }
            });
        }
        
        var s_set = [
            s_entity,
            s_property,
            s_value
        ].join(".");

        o_stored[s_set] = o_viewClone;
    });

    self.postMessage(o_stored);
});
