(function(a,n){typeof exports=="object"&&typeof module<"u"?module.exports=n():typeof define=="function"&&define.amd?define(n):(a=typeof globalThis<"u"?globalThis:a||self,a["bitrix24-library"]=n())})(this,function(){"use strict";var O=Object.defineProperty;var L=(a,n,m)=>n in a?O(a,n,{enumerable:!0,configurable:!0,writable:!0,value:m}):a[n]=m;var u=(a,n,m)=>L(a,typeof n!="symbol"?n+"":n,m);function a(o){return new Promise((e,t)=>{let i=!1,r=document.querySelector(`script[src^="${o}"]`);if(r){e(r);return}else r=document.createElement("script"),r.async=!0,r.src=o,i=!0;r.addEventListener("error",t),r.addEventListener("abort",t),r.addEventListener("load",s=>{e(s.target)}),i&&document.head.append(r)})}var n={exports:{}};n.exports=g;var m=n.exports.isMobile=g;n.exports.default=g;const X=/(android|bb\d+|meego).+mobile|armv7l|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|redmi|series[46]0|samsungbrowser.*mobile|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i,w=/CrOS/,y=/android|ipad|playbook|silk/i;function g(o){o||(o={});let e=o.ua;if(!e&&typeof navigator<"u"&&(e=navigator.userAgent),e&&e.headers&&typeof e.headers["user-agent"]=="string"&&(e=e.headers["user-agent"]),typeof e!="string")return!1;let t=X.test(e)&&!w.test(e)||!!o.tablet&&y.test(e);return!t&&o.tablet&&o.featureDetect&&navigator&&navigator.maxTouchPoints>1&&e.indexOf("Macintosh")!==-1&&e.indexOf("Safari")!==-1&&(t=!0),t}class P{constructor(e){u(this,"BX24");this.BX24=e}init(){return new Promise(e=>{this.BX24.init(e)})}install(e=""){return e.length?(this.BX24.install(e),Promise.resolve()):new Promise(t=>{this.BX24.install(t)})}installFinish(){this.BX24.installFinish()}getAuth(){return this.BX24.getAuth()}refreshAuth(){return new Promise(e=>{this.BX24.refreshAuth(e)})}callMethod(e,t,i){this.BX24.callMethod(e,t,i)}callBatch(e,t){return new Promise(i=>{this.BX24.callBatch(e,i,t)})}callBind(e,t,i,r){return this.BX24.callBind(e,t,i,r),this.callUnbind.bind(this,e,t,i,r)}callUnbind(e,t,i,r){this.BX24.callUnbind(e,t,i,r)}get userOption(){return{set:(e,t)=>{this.BX24.userOption.set(e,t)},get:e=>this.BX24.userOption.get(e)}}get appOption(){return{set:(e,t)=>new Promise(i=>{this.BX24.appOption.set(e,t,i)}),get:e=>this.BX24.appOption.get(e)}}selectUser(){return new Promise(e=>{this.BX24.selectUser(e)})}selectUsers(){return new Promise(e=>{this.BX24.selectUsers(e)})}selectAccess(e=[]){return new Promise(t=>{e.length?this.BX24.selectAccess(e,t):this.BX24.selectAccess(t)})}selectCRM(e){return new Promise(t=>{e?this.BX24.selectCRM(e,t):this.BX24.selectCRM(t)})}get placement(){return{bindEvent:e=>new Promise(t=>{this.BX24.placement.bindEvent(e,t)}),call:(e,t)=>new Promise(i=>{this.BX24.placement.call(e,t,i)}),getInterface:()=>new Promise(e=>{this.BX24.placement.getInterface(e)}),info:()=>this.BX24.placement.info()}}isAdmin(){return this.BX24.isAdmin()}getLang(){return this.BX24.getLang()}resizeWindow(e,t){return new Promise(i=>{this.BX24.resizeWindow(e,t,i)})}fitWindow(){return new Promise(e=>{this.BX24.fitWindow(e)})}reloadWindow(){this.BX24.reloadWindow()}setTitle(e){return new Promise(t=>{this.BX24.setTitle(e,t)})}ready(){return new Promise(e=>{this.BX24.ready(e)})}isReady(){return this.BX24.isReady()}proxy(e,t){return this.BX24.proxy(e,t)}closeApplication(){this.BX24.closeApplication()}getDomain(e=!1){const t=this.BX24.getDomain();return e?["https:",t].join("//"):t}openApplication(e){return new Promise(t=>{this.BX24.openApplication(e,t)})}openPath(e){return new Promise((t,i)=>{this.BX24.openPath(e,r=>{r.result==="error"&&i(new Error(r.errorCode)),t()})})}proxyContext(){return this.BX24.proxyContext()}scrollParentWindow(e){return new Promise(t=>{this.BX24.scrollParentWindow(e,t)})}bind(e,t,i){return this.BX24.bind(e,t,i),this.unbind.bind(this,e,t,i)}unbind(e,t,i){this.BX24.unbind(e,t,i)}getScrollSize(){return this.BX24.getScrollSize()}get im(){return{callTo:(e,t)=>{this.BX24.im.callTo(e,t)},phoneTo:e=>{this.BX24.im.phoneTo(e)},openMessenger:e=>{this.BX24.im.openMessenger(e)},openHistory:e=>{this.BX24.im.openHistory(e)}}}}class x{constructor(e,t={}){u(this,"callBatch");u(this,"handler");u(this,"commands",[]);u(this,"result",{});u(this,"errors",{});u(this,"delay",500);u(this,"limit",50);this.callBatch=e,this.handler=t}batch(e){return this.commands=[],this.result={},this.errors={},new Promise((t,i)=>{const r=this.parseRequest(e),s=[];let h=0;this.callBatch(r,B=>{Object.entries(B).forEach(([c,d])=>{const l=d.error();l&&(this.errors[c]=l,h+=1);const p=d.total(),f=d.data();let b=1;if(f&&(Array.isArray(f.items)?b=f.items.length:Array.isArray(f)&&(b=f.length)),this.result[c]=f,p>this.limit&&p>b){const E=Math.ceil(p/this.limit)-1,M=Array.from({length:E});s.push([c,r[c],M])}}),h>0?(this.errorLogger(),i(this.errors)):s.length?(this.buildCommandsArray(s),this.batchPayload().then(()=>t(this.parseResult()))):t(this.parseResult())})})}buildCommandsArray(e){let t=[];e.forEach(([i,r,s])=>{s.forEach((h,B)=>{const c=B+1,d=this.limit*c,p=[[i,c].join("_"),this.addStart(r,d)];t.length===this.limit?(this.commands.push(t),t=[p]):t.push(p)})}),t.length&&this.commands.push(t)}batchPayload(){const e=[];return this.commands.forEach(t=>{const i=Object.fromEntries(t),r=new Promise(s=>{setTimeout(()=>{this.callBatch(i,h=>{Object.entries(h).forEach(([B,c])=>{const[d]=B.split("_"),l=c.data();l!=null&&l.items?this.result[d].items.push(...l.items):Array.isArray(l)&&this.result[d].push(...l)}),s()})},this.delay)});e.push(r)}),Promise.all(e)}parseResult(){return Object.entries(this.handler).forEach(([e,t])=>{this.result[e]&&(this.result[e]=t(this.result[e]))}),this.result}parseRequest(e){return Object.entries(e).reduce((t,[i,r])=>{if(Array.isArray(r)){const[s,h={}]=r;t[i]=[s,h]}else{const{method:s,params:h={}}=r;t[i]=[s,h]}return t},{})}addStart(e,t){const[i,r]=e;return[i,{start:t,...r}]}errorLogger(){console.group(`${this.constructor.name}: Ошибки в методах!`),Object.entries(this.errors).forEach(([e,t])=>{console.info(`[${e}]`,t.toString())}),console.groupEnd()}}class A extends P{isMobile(e){return m(e)}loadScript(e){return a(e)}createBatch(e={},t=x){return new t(this.BX24.callBatch,e)}openLink(e,t=!1){const i=r=>{const s=document.createElement("a");s.href=[this.getDomain(!0),r].join(""),s.target="_blank",s.click()};t||m()?i(e):this.openPath(e).catch(()=>i(e))}}return{async init(){await a("https://api.bitrix24.com/api/v1/");const o=new A(window.BX24);return o.init().then(()=>o)}}});