define("Controls-DataEnv/dataLoader",["WasabyLoader/ModulesLoader","UI/Deps","Types/entity","Env/Constants","Core/PromiseLib/PromiseLib","Controls-DataEnv/dataFactory","Controls-DataEnv/dataContext","Browser/Transport","Application/Env","require","exports"],(function(t,e,a,o,r,i,n,s,d,l,c){Object.defineProperty(c,"__esModule",{value:true}),c["Controls-DataEnv/_dataLoader/Constants"]=true;var u=function(t,e,a){"use strict";return Object.defineProperty(e,"__esModule",{value:true}),e.GET_CONFIG_TIMEOUT=e.CLIENT_GET_CONFIG_TIMEOUT=e.SERVER_GET_CONFIG_TIMEOUT=void 0,e.SERVER_GET_CONFIG_TIMEOUT=3e3,e.CLIENT_GET_CONFIG_TIMEOUT=15e3,e.GET_CONFIG_TIMEOUT=a.constants.isServerSide?e.SERVER_GET_CONFIG_TIMEOUT:e.CLIENT_GET_CONFIG_TIMEOUT,e}(l,{},o);c["Controls-DataEnv/_dataLoader/utils"]=true;var f=function(t,e,a,o,r,i){"use strict";function n(t){let e=r.logger.error;if(t instanceof i.RPC.Error)e="warning"===t.errType?r.logger.warn:r.logger.error;return e}async function s(t){try{const e=await(0,a.loadAsync)(t);return(0,o.addPageDeps)([t]),e}catch(e){d(e,`Controls-DataEnv/dataLoader::Ошибка при загрузке модуля ${t}`)}}function d(t,e){if(t instanceof Error){const a=undefined;n(t)(`${e} ${t.message}`)}}return Object.defineProperty(e,"__esModule",{value:true}),e.loadErrorHandler=e.loadModule=void 0,e.loadModule=s,e.loadErrorHandler=d,e}(l,{},t,e,d,s);c["Controls-DataEnv/_dataLoader/DataFactoryLoader"]=true;var h=function(t,e,a,o){"use strict";Object.defineProperty(e,"__esModule",{value:true});const r=1e4;function i(t){var e;return(null===(e=t.dataFactoryArguments)||void 0===e?void 0:e.loadDataTimeout)||(o.constants.isServerSide?r:null)}function n(t,e,a,o){let r={...e.dataFactoryArguments};if(t.getDataFactoryArguments)r=t.getDataFactoryArguments(r,a);if(o&&!(null===r||void 0===r?void 0:r.loadDataTimeout))r.loadDataTimeout=o;const i=r.loaderExtraValues;if(i){const t={};i.forEach((({propName:e,dependencyName:o,dependencyPropName:r,prepare:i})=>{let n=null===a||void 0===a?void 0:a[o];if(n instanceof Object)n=n[r];if(i)t[e]=i(n);else t[e]=n})),r={...r,...t}}return r}function s(t,e){(0,a.loadErrorHandler)(t,e)}class d{constructor(t){this._$config={...t.config},this._$config.dataFactoryArguments=this._$config.dataFactoryArguments||{},this._$router=t.router,this._$loadTimeout=t.loadTimeout||i(this._$config),this._$name=t.name,this._$dependencies=t.dependencies,this._$errorHandler=t.errorHandler||s,this._$data=t.data}async _load(){const t=await(0,a.loadModule)(this._$config.dataFactoryName);let e;if(!t)throw new Error(`Не удалось загрузить фабрику ${this._$config.dataFactoryName}`);try{if(void 0!==this._$data)e=this._$data;else{const o=n(t,this._$config,this._$dependencies,this._$loadTimeout);if(e=await t.loadData(o,this._$dependencies,this._$router,false,this._$name),this._$config.afterLoadCallback){const t=await(0,a.loadModule)(this._$config.afterLoadCallback);if(!t)this._$errorHandler(new Error(`Не удалось загрузить модуль afterLoadCallback ${this._$config.afterLoadCallback}`));else this._callAfterLoadCallback(this._$name,t,e)}}}catch(t){this._$errorHandler(t,`Controls-DataEnv/dataLoader:Loader.loadDataFactory: Произошла ошибка во время выполнения метода loadData у фабрики ${this._$config.dataFactoryName} с ключом ${this._$name}\n             и параметрами ${this._$config.dataFactoryArguments}`),e=t}return e}_callAfterLoadCallback(t,e,a){try{e(a)}catch(e){this._$errorHandler(e,`DataLoader: Ошибка вызова afterLoadCallback в загрузчике ${t}`)}}isLoading(){return!!this._loadPromise}async load(){if(!this.isLoading())this._loadPromise=this._load();return this._loadPromise}}return e.default=d,e}(l,{},f,o);c["Controls-DataEnv/_dataLoader/DataConfigsLoader"]=true;var _=function(t,e,a,o){"use strict";function r(t){const e=[];return Object.entries(t).forEach((([o,r])=>{const i=a.DataConfigResolver.calcAllDependencies(r,"load");if(i){const a=[],r=[];if(i.forEach((e=>{var i;if(!t.hasOwnProperty(e))a.push(e);else{const a=undefined;if(null===(i=t[e].dependencies)||void 0===i?void 0:i.includes(o))r.push(e)}})),a.length)e.push(`Отсутствуют фабрики с ключами ${a.join(", ")}, указанные в зависимостях для загрузчика ${o}`);if(r.length)e.push(`У фабрики с ключом ${o} найдены циклические зависимости ${r.join(", ")}`)}})),e}Object.defineProperty(e,"__esModule",{value:true});class i{async _callLoader(t,e){var r;let i={};const n=a.DataConfigResolver.calcAllDependencies(t,"load");if(n)i=await this._loadDependencies(n);if(!this._$elements[e])this._$elements[e]=new o.default({config:t,router:this._$router,dependencies:i,name:e,loadTimeout:this._$loadTimeout,data:null===(r=this._$data)||void 0===r?void 0:r[e]});return this._$elements[e].load()}async _loadDependencies(t){const e=[],a={};return t.forEach(((t,o)=>{const r=this._callLoader(this._$configs[t],t);r.then((e=>{a[o]=e,a[t]=e})),e.push(r)})),await Promise.all(e),a}constructor(t){this._$configs={},this._$elements={},this._$validateErrors=[],this._$configs=t.configs,this._$router=t.router,this._$validateErrors=r(this._$configs),this._$data=t.data,this._$loadTimeout=t.loadTimeout}async load(){if(this._$validateErrors.length)return Promise.reject(this._$validateErrors.join(", "));const t={},e=this.loadEvery(),a=[];for(const[o,r]of Object.entries(e))a.push(r.then((e=>{t[o]=e})));return await Promise.all(a),t}loadEvery(){const t={};return Object.entries(this._$configs).forEach((([e,a])=>{t[e]=this._callLoader(a,e)})),t}}return e.default=i,e}(l,{},i,h),g=_;c["Controls-DataEnv/_dataLoader/DataNodeLoader"]=true;var $=function(t,e,a,o,r,i,n,s){"use strict";Object.defineProperty(e,"__esModule",{value:true});class d{_checkDependencies(){return true}constructor(t){this._$dataConfigs={},this._$dataContext=t.dataContext,this._$config=t.config,this._$router=t.router,this._$path=t.path||[],this._$loadTimeout=t.loadTimeout}async loadConfigGetter(){if(!this._checkDependencies());if((0,a.isDataNodeConfigGetter)(this._$config)){const t=await(0,s.loadModule)(this._$config.configGetter);let e={},o=false,d;if(t){try{const r=(0,a.callGetConfig)(t,this._$config,this._$dataContext,this._$path);if(o=!!r.then,o)e=await(0,i.wrapTimeout)(r,n.GET_CONFIG_TIMEOUT);else e=r}catch(t){if(t instanceof Error)d=t;(0,s.loadErrorHandler)(t,"Ошибка при получении конфигурации предзагрузки из "+`${this._$config.configGetter}. \n Error: ${t}`)}if(!a.DataConfigResolver.isOldDataFormat(e)){if(o)r.logger.warn(`Controls-DataEnv/dataLoader:Loader.loadConfigGetter::Метод getConfig у модуля ${this._$config.configGetter} должен быть синхронным!`)}else e=a.DataConfigResolver.convertLoadResultsToFactory(e)}else d=new Error(`Произошла ошибка при загрузке модуля ${this._$config.configGetter}. Узел контекста будет создан пустым`);return this._$dataConfigs=e,this._$resolvedConfig={isAsyncConfigGetter:o,error:d,...this._$config},{dataConfigs:e,isAsyncConfigGetter:o,error:d}}else return Promise.reject("Передан неверный конфиг в loadConfigGetter")}async load(){if("configs"in this._$config)return new o.default({configs:this._$config.configs,router:this._$router,data:this._$dataContext.hasNode(this._$path)?this._$dataContext.getNodeData(this._$path):void 0,loadTimeout:this._$loadTimeout}).load();else if("configGetter"in this._$config){const t=await this.loadConfigGetter();if(t.dataConfigs)return new o.default({configs:t.dataConfigs,router:this._$router,data:this._$dataContext.hasNode(this._$path)?this._$dataContext.getNodeData(this._$path):void 0,loadTimeout:this._$loadTimeout}).load()}return{}}getDataConfigs(){return this._$dataConfigs}getResolvedConfig(){return this._$resolvedConfig}}return e.default=d,e}(l,{},i,_,d,r,u,f);c["Controls-DataEnv/_dataLoader/DataContextLoader"]=true;var C=function(t,e,a,o,r){"use strict";Object.defineProperty(e,"__esModule",{value:true});const i={keyProperty:"id",parentProperty:"parentId",childrenProperty:"children"};function n(t){const e=[];return Object.entries(t).forEach((([t,a])=>{e.push({id:t,...a})})),e}class s{async _loadNode(t,e,a){var o;const i=a.getPath(true),n=a.getPath(false);this._$dataContext.addNode(n,e,a.value.data);const s=new r.default({config:t,dataContext:this._$dataContext,router:this._$router,path:i,loadTimeout:this._$loadTimeout});this._$nodes.set(i.toString(),s);const d=await s.load();if(this._$dataContext.hasNode(i))this._$dataContext.addToNode(i,d);else this._$dataContext.addNode(n,e,d);return a.value.data=d,a.value.isAsyncConfigGetter=!!(null===(o=s.getResolvedConfig())||void 0===o?void 0:o.isAsyncConfigGetter),d}_findDependencyNode(t,e){var a,o;let r=t.parent,i;while(r&&!i){if(null===(o=null===(a=null===r||void 0===r?void 0:r.value)||void 0===a?void 0:a.configs)||void 0===o?void 0:o.hasOwnProperty(e))i=r;r=r.parent}return i}constructor(t){this._loadPromises=new Map,this._$nodes=new Map,this._$dataContext=new o.DataContext({data:{}}),this._$configs=t.configs,this._$tree=new a.relation.Tree(i),this._$loadTimeout=t.loadTimeout,this._$tree.parseTree(this._$configs),this._$router=t.router}async load(){this._$tree.each((async(t,e,a)=>{var o;const r=a.getPath(true).join(", ");if(null===(o=t.dependencies)||void 0===o?void 0:o.length){const o=[];t.dependencies.forEach((i=>{const n=this._findDependencyNode(a,i);if(!n);else{const i=n.getPath(true).join(", ");if(this._loadPromises.has(i))o.push(this._loadPromises.get(i));else{const i=this._loadNode(t,e,a);o.push(i),this._loadPromises.set(r,i)}}}));const i=Promise.all(o).then((()=>this._loadNode(t,e,a)));this._loadPromises.set(r,i)}else this._loadPromises.set(r,this._loadNode(t,e,a))}));const t=[];for(const e of this._loadPromises.values())t.push(e);return await Promise.all(t),this._loadPromises.clear(),this._$tree.toObject()}async loadCompatible(t,e){const a=n(t);this._$tree.parseTree(a),await this.load();const o={},r={},i={};return this._$tree.each(((t,a,n)=>{const s=this._$nodes.get(n.getPath(true).toString());if(o[a]=t.data,s)if(i[a]=null===s||void 0===s?void 0:s.getResolvedConfig(),e)r[a]=(null===s||void 0===s?void 0:s.getDataConfigs())||{}})),{loadResults:o,dataConfigs:r,configs:i}}getDataContext(){return this._$dataContext}}return e.default=s,e}(l,{},a,n,$),v=C;c["Controls-DataEnv/_dataLoader/Loader"]=true;var p=function(t,e,a,o){"use strict";Object.defineProperty(e,"__esModule",{value:true});class r{static async load(t,e,o){return new a.default({configs:t,router:o,loadTimeout:e}).load()}static loadEvery(t,e,o){return new a.default({configs:t,router:o,loadTimeout:e}).loadEvery()}static async loadByConfigs(t,e,a,r){return new o.default({configs:{},router:a,loadTimeout:e}).loadCompatible(t,r)}}return e.default=r,e}(l,{},_,C),m=p;return c.DataConfigsLoader=c.DataContextLoader=c.Loader=void 0,Object.defineProperty(c,"Loader",{enumerable:true,get:function(){return m.default}}),Object.defineProperty(c,"DataContextLoader",{enumerable:true,get:function(){return v.default}}),Object.defineProperty(c,"DataConfigsLoader",{enumerable:true,get:function(){return g.default}}),c}));