window.kit=window.kit||{},kit.service=kit.service||{},kit.config=kit.config||{},function(global){var defaultConfig={version:"2.0",assetsPath:"",requiredDependencies:[],autoloadPluginList:[],debug:!0},stateList={undefined:0,registered:1,loading:3,loaded:4,installing:5,installed:6},MD_TYPE={ES:"ES",UMD:"UMD",HMD:"HMD",default:"default"};function Plugin(e,t={}){this.id=e,this.state=stateList.undefined,this.state2=stateList.undefined,this.module={code:"",exports:{}},this.config={autoRequire:!1,loadJsAfterModule:!1,isSetTimestamp:!0,path:"",module:"",type:"default",handler:void 0,onRegister:function(e){},onLoad:function(e){},onInstall:function(e){},js:[],css:[],alias:[],deps:[],debug:!1},this.pluginManager=void 0,this.promise=new Promise((function(e,t){e()})),this.errors=[],this.register(t)}function isAssetLoaded(e){return e=e.match(/^[^#?]*/)[0],document.querySelector('script[src^="'+e+'"]')||document.querySelector('link[href^="'+e+'"]')||document.querySelector('meta[property="kit:file-load"][content="'+e+'"]')}function getUrlWithTimestamp(e){try{var t=new URL(e);t.searchParams.append("__pm",(new Date).getTime()),e=t.toString()}catch(t){e=(e=String(e).split("?")[0])+"?__pm"+(new Date).getTime()}return e}Plugin.prototype.setPluginManager=function(e){this.pluginManager=e},Plugin.prototype.queuePush=function(e,t=[]){"function"==typeof e&&(this.promise=this.promise.then((function(n){var i=e.apply(this,t);if(i)return i})))},Plugin.prototype.__queue=function(e,t,n=[],i=void 0){var o=this;void 0===i&&(i=o),o.__checkState(e)?t.apply(i,n):o.promise=o.promise.then(function(){return new Promise((function(e,o){t.apply(i,n),e()}))}.bind(o))},Plugin.prototype.__exec=function(e,t,n=[],i=void 0){var o=this;void 0===i&&(i=o),o.__checkState(e)?t.apply(i,n):o.promise=o.promise.then(function(){t.apply(i,n)}.bind(o))},Plugin.prototype.__checkState=function(e,t=void 0){return void 0===t?this.state>=e:this.state>=e&&this.state<=t},Plugin.prototype.__check2State=function(e,t=void 0){return void 0===t?this.state2>=e:this.state2>=e&&this.state2<=t},Plugin.prototype.__getValidConfig=function(e={}){var t=Object.assign({},e);return"function"!=typeof t.onLoad&&(t.onLoad=function(e){}),"function"!=typeof t.onInstall&&(t.onInstall=function(e){}),"function"!=typeof t.onRegister&&(t.onRegister=function(e){}),"string"!=typeof t.path&&(t.path=""),"string"==typeof t.js?t.js=[t.js]:Array.isArray(t.js)||(t.js=[]),"string"==typeof t.css?t.css=[t.css]:Array.isArray(t.css)||(t.css=[]),"string"==typeof t.alias?t.alias=[t.alias]:Array.isArray(t.alias)||(t.alias=[]),t.autoRequire=!!t.hasOwnProperty("autoRequire")&&!!t.autoRequire,t.loadJsAfterModule=!!t.hasOwnProperty("loadJsAfterModule")&&!!t.loadJsAfterModule,t.isSetTimestamp=!t.hasOwnProperty("isSetTimestamp")||!!t.isSetTimestamp,t.type=t.type||"default","string"==typeof t.deps?t.deps=[t.deps]:Array.isArray(t.deps)||(t.deps=[]),t},Plugin.prototype.__setState=function(e,t=!1){var n=this;(t||n.__checkState(e))&&(n.state=e)},Plugin.prototype.register=function(e={}){var t,n=this,i=n.id;if(this.__checkState(stateList.installed))throw new Error("Plugin "+i+" уже невозможно конфигурировать ("+n.state+"/"+n.state2+")");if("function"==typeof e)t=e,(e=this.config).handler=t,e.type=MD_TYPE.HMD;else{if(this.__checkState(stateList.loaded)||this.__check2State(stateList.loading))throw new Error("Plugin "+i+" уже невозможно конфигурировать ("+n.state+")");if("object"!=typeof e)throw new Error("Plugin.register: plugin "+i+" is not a valid format");e=this.__getValidConfig(e),Object.assign(this.config,e)}if(!this.__checkState(stateList.registered)){n.config.debug&&console.log(`Plugin ${n.id}: onRegister + event.`),this.state=stateList.registered,this.config.onRegister(this);var o=new CustomEvent("onRegister"+this.id+"Plugin",{detail:{plugin:this}});document.dispatchEvent(o)}e.autoRequire},Plugin.prototype.load=function(e){var t=this;"function"!=typeof e&&(e=function(){});var n=t.id;if(!t.__checkState(stateList.registered))return console.warn(`Plugin.load(${n}): is not register!.`),t;if(t.__checkState(stateList.loaded))return e(),t;if(t.__check2State(stateList.loading))return t.__exec(stateList.loaded,e),t;t.state2=stateList.loading;var i,o=t.config;o.css.forEach((function(e,n,i){t.getStylePromise(o.path+e)})),t.pluginManager&&(i=t.pluginManager.__getDepsPromiseCallback(t.config.deps),t.queuePush(i));var r,s=(r=o.path+o.module,"string"==typeof o.module&&""!==o.module&&"ES"===o.type?function(){return t.getESModulePromise(r).then(function(e){e.__esModule=!0,t.module.exports=e}.bind(t))}:"string"==typeof o.module&&""!==o.module&&"UMD"===o.type?isAssetLoaded(r)?void(t.config.debug&&console.warn(`Plugin ${t.id}: Скрипт ${r} уже загружен!`)):function(){return t.httpGet(r).then(function(e){t.module.code=e}.bind(t))}:"HMD"!==o.type||o.hasOwnProperty("handler")&&"function"==typeof o.handler?"string"==typeof o.module&&""!==o.module&&"default"===o.type?isAssetLoaded(r)?void(t.config.debug&&console.warn(`Plugin ${t.id}: Скрипт ${r} уже загружен!`)):function(){return t.getScriptPromise(r)}:void 0:function(){return t.getScriptPromise(r)}),l=t.getJsAssetsPromiseCallback(o.js);return o.loadJsAfterModule?(t.queuePush(s),t.queuePush(l)):(t.queuePush(l),t.queuePush(s)),i||s||l||(t.state=stateList.loaded),t.__queue(stateList.loaded,(function(){t.config.debug&&console.log(`Plugin ${t.id}: onLoad + event.`),t.isLoaded()||(t.state=stateList.loaded),o.onLoad.call(t,t);var e=new CustomEvent("onLoad"+t.id+"Plugin",{detail:{plugin:t}});document.dispatchEvent(e)})),t.__exec(stateList.loaded,function(){e()}.bind(t)),t},Plugin.prototype.install=function(callback){var self=this,global=self.pluginManager.global,require=self.pluginManager.require.bind(self.pluginManager);"function"!=typeof callback&&(callback=function(){});var pluginId=self.id;if(!self.__checkState(stateList.registered))return console.warn(`Plugin ${pluginId}: is not register!`),self;if(self.__checkState(stateList.installed))return callback(),self;if(self.__check2State(stateList.installing))return self.__exec(stateList.installed,callback),self;self.__checkState(stateList.loaded)||this.load(),self.state2=stateList.installing;var setGlobals=function(e,t){if(void 0!==t){var n=self.id,i=self.config.alias;e.hasOwnProperty(n)||(e[n]=t),Array.isArray(i)&&i.length&&i.forEach((function(n){e.hasOwnProperty(n)||(e[n]=t)}))}},module=self.module,installer=function(){var __exports=void 0;"ES"===self.config.type&&"string"==typeof self.config.module&&""!==self.config.module?__exports="object"==typeof self.module.exports&&void 0!==self.module.exports.default?self.module.exports.default:self.module.exports:"UMD"===self.config.type&&"string"==typeof self.config.module&&""!==self.config.module?(function(require,exports,module,global){var self=global;eval(module.code)}.bind(global).call(global,require,self.module.exports,module,global),__exports="object"==typeof self.module.exports&&void 0!==self.module.exports.default?self.module.exports.default:self.module.exports):"HMD"===self.config.type&&"function"==typeof self.config.handler&&(__exports=self.config.handler.bind(global).call(global,require,self.module.exports,module,global),void 0===__exports&&self.module.exports&&(__exports=self.module.exports)),"object"==typeof global&&setGlobals(global,__exports),self.state=stateList.installed,console.log(`%cPlugin ${pluginId}: installed.`,"color: green;"),self.config.onInstall.call(self,self);var event=new CustomEvent("onInstall"+self.id+"Plugin",{detail:{plugin:self}});return document.dispatchEvent(event),!0}.bind(this);return this.__queue(stateList.loaded,installer),this.__exec(stateList.loaded,(function(){callback()})),self},Plugin.prototype.exec=function(e,t=[],n=void 0){var i=this;void 0===n&&(n=i),i.__checkState(stateList.installed)||(i.load(),i.install()),i.__exec(stateList.installed,e,t,n)},Plugin.prototype.getESModulePromise=function(e){var t=this;return new Promise((function(n,i){import(e).then((function(e){n(e)}),(function(e){t.errors.push(e),console.log(`%cPlugin ${t.id}: error import module!`,"color: red;"),i(e)}))}))},Plugin.prototype.httpGet=function(e){var t=this;return new Promise((function(n,i){t.config.debug&&console.log(`Plugin ${t.id}: httpGet ${e}`);var o=new XMLHttpRequest;o.open("GET",e,!0),o.onload=function(){if(200==this.status)n(this.response);else{console.log(`%cPlugin.load(${t.id}): error load file!`,"color: red;");var e=new Error(this.statusText);e.code=this.status,i(e)}},o.onerror=function(){console.log(`%cPlugin.load(${t.id}): error load file!`,"color: red;"),i(new Error("Network Error"))},o.send()}))},Plugin.prototype.getScriptPromise=function(e){var t=this;if(!isAssetLoaded(e))return new Promise((function(n,i){t.config.debug&&console.log(`Plugin ${t.id}: load script ${e}`);var o=document.createElement("script");o.src=e,t.config.isSetTimestamp&&(o.src=getUrlWithTimestamp(e)),o.onload=function(){n(o)},o.onerror=function(){var n=new Error(`Ошибка загрузки скрипта ${e}`);t.errors.push(n),console.log(`%cPlugin.load(${t.id}): error load script!`,"color: red;"),i(n)},document.head.append(o)}));t.config.debug&&console.warn(`Plugin ${t.id}: Скрипт ${e} уже загружен!`)},Plugin.prototype.getStylePromise=function(e){var t=this;return new Promise((function(n,i){if(isAssetLoaded(e))t.config.debug&&console.warn(`Plugin ${t.id}: Стиль ${e} уже загружен`),n();else{var o=document.createElement("link");o.rel="stylesheet",o.type="text/css",o.media="all",o.href=e,t.config.isSetTimestamp&&(o.href=getUrlWithTimestamp(e)),document.head.append(o),n(o)}}))},Plugin.prototype.getAsyncScriptListPromise=function(e){var t=e.map(this.getScriptPromise.bind(this)).filter((e=>void 0!==e));if(t.length)return Promise.all(t)},Plugin.prototype.getJsAssetsPromiseCallback=function(e=[]){var t=this;if(e.length)return function(){var n=new Promise((function(e,t){e()}));return e.forEach((function(e,i,o){n=Array.isArray(e)?n.then((function(){return t.getAsyncScriptListPromise(e)})):n.then((function(){return t.getScriptPromise(t.config.path+e)}))})),n}},Plugin.prototype.__getRealId=function(e){var t,n=this,i=e.toLowerCase();if(n.id.toLowerCase()===i)return n.id;for(t of n.config.alias)if(i===t.toLowerCase())return n.id},Plugin.prototype.__is=function(e){return this.id===this.__getRealId(e)},Plugin.prototype.isLoaded=function(){return this.__checkState(stateList.loaded)},Plugin.prototype.isInstalled=function(){return this.__checkState(stateList.installed)},Plugin.prototype.require=function(){var e=this;return e.isInstalled()||e.config.debug&&console.warn(`Plugin ${e.id}: not instaled!`),e.module.exports};var PluginManager=function(e){this.plugins={},this.global=e};function init(e){var t=new PluginManager(e);e.ready=t.ready.bind(t),e.exec=t.exec.bind(t),e.require=t.require.bind(t),t.register("pluginManager",{alias:"pm",type:MD_TYPE.HMD,handler:function(e,n,i,o){return o.PluginManagerClass=PluginManager,"object"==typeof window&&(window.__PluginManager__=t,window.__pmGlobal__=o),i.exports=t},onInstall:function(e){}}),t.install("pluginManager"),t.register("config",{type:MD_TYPE.HMD,handler:function(e,t,n,i){var o=document.currentScript;if(o)try{var r=new URL(o.src);defaultConfig.assetsPath=r.pathname.split("/").slice(0,-2).join("/")}catch(e){var s=o.src.split("?")[0];defaultConfig.assetsPath=s.split("/").slice(0,-2).join("/")}var l=i.config||{};l=Object.assign({},defaultConfig,l),"object"==typeof window&&(window.__pmConfig__=i.config=l),document.addEventListener("DOMContentLoaded",(function(){console.log("%cDOMContentLoaded","color:#2053ff")}))},onInstall:function(e){"object"==typeof window&&window.hasOwnProperty("__PluginManagerConfigHandler__")&&"function"==typeof __PluginManagerConfigHandler__&&t.registerPlugins(__PluginManagerConfigHandler__())}}),t.install("config")}PluginManager.prototype.registerPlugins=function(e){for(var t in e)e.hasOwnProperty(t)&&this.register(t,e[t]);return this},PluginManager.prototype.register=function(e,t={}){if("string"==typeof e&&""!==e){var n=this.__getPlugin(e);n?n.register(t):((n=new Plugin(e,t)).setPluginManager(this),this.plugins[e]=n)}else console.warn("Invalid pluginId!",e)},PluginManager.prototype.__getPlugin=function(e){var t,n,i=this;for(t in i.plugins)if(i.plugins.hasOwnProperty(t)&&(n=i.plugins[t]).__is(e))return n},PluginManager.prototype.__getDepsPromiseCallback=function(e=[]){var t=this,n=e.map((function(e){var n=t.__getPlugin(e);if(n){if(n.install(),n.isInstalled())return;return n.promise}console.log(`%cPluginManager: plugin ${e} not found!`,"color: red;")})).filter((e=>void 0!==e));if(n.length)return function(){return Promise.all(n)}},PluginManager.prototype.__getRealIdList=function(e=[]){var t=this;return e.map((function(e){var n=t.__getPlugin(e);if(n)return n.id}))},PluginManager.prototype.exec=function(e,t,n=[],i=void 0){if("function"==typeof e&&(i=n,n=t,t=e,e=[]),"string"==typeof e&&(e=[e]),void 0===i&&(i=this),void 0===n&&(n=[]),"function"==typeof t){global.config&&global.config.requiredDependencies&&(e=Array.from(new Set(e.concat(global.config.requiredDependencies))));var o=this.__getDepsPromiseCallback(e);o?o().then((function(){t.apply(i,n)})):t.apply(i,n)}},PluginManager.prototype.exec2=function(e,t){var n=this,i=this,o=n.__getDepsPromiseCallback(e),r=function(){var t=[];return e.forEach(function(e){n.__getPlugin(e)?t.push(n.require(e)):t.push(void 0)}.bind(n)),t}.bind(n);o?o().then((function(){t.apply(i,r())})):t.apply(i,r())},PluginManager.prototype.ready=function(e,t=void 0){var n=this;"function"==typeof e&&(t=e,e=[]),n.__ready(n.exec,[e,t],n)},PluginManager.prototype.__ready=function(e,t=[],n=window){n=n||this,/complete|interactive|loaded/.test(document.readyState)?e.apply(n,t):document.addEventListener("DOMContentLoaded",function(){e.apply(n,t)}.bind(this))},PluginManager.prototype.require=function(e,t){var n=this.__getPlugin(e);if(n){e=n.id;var i=n.module;return"function"!=typeof t&&(t=function(){}),n.install(t),i.exports}console.warn("PluginManager.require: plugin "+e+" not found!")},PluginManager.prototype.install=function(e,t){var n=this.__getPlugin(e);if(n){e=n.id;n.module;n.install(t)}else console.warn("PluginManager.install: plugin "+e+" not found!")},init(global)}(kit);