import{i as e,n as t,o as n,t as r}from"./tslib.es6-CQzwG08B.js";import{i,n as a,t as o,u as s}from"./load-script-DEpXgVdM.js";import{t as c}from"./helpers-DLXHM90T.js";import{A as l,C as u,D as d,E as f,S as p,T as m,_ as h,b as g,k as _,v,x as y}from"./index-CvkZuY26.js";import{t as b}from"./is-plan-event-enabled-B-tUfKCy.js";var x=g();function S(e){return e.toLowerCase().replace(`.`,``).replace(/\s+/g,`-`)}function C(e,t){return t===void 0&&(t=!1),t?btoa(e).replace(/=/g,``):void 0}function w(e){return(`Integration`in e?e.Integration:e).prototype.name}function T(e,t,r){try{var i=((window==null?void 0:window.performance)?.getEntriesByName(e,`resource`)??[])[0];i&&t.stats.gauge(`legacy_destination_time`,Math.round(i.duration),n([r],i.duration<100?[`cached`]:[],!0))}catch{}}function E(e,t,n){var r;`Integration`in e?(e({user:function(){return n.user()},addIntegration:function(){}}),r=e.Integration):r=e;var i=new r(t);return i.analytics=n,i}function D(n,r,a,s){return t(this,void 0,void 0,function(){var t,c,l,u,d,f;return e(this,function(e){switch(e.label){case 0:t=S(r),c=C(t,s),l=i(),u=`${l}/${c??t}.js?version=${a}`,e.label=1;case 1:return e.trys.push([1,3,,4]),[4,o(u)];case 2:return e.sent(),T(u,n,r),[3,4];case 3:throw d=e.sent(),n.stats.gauge(`legacy_destination_time`,-1,[`plugin:${r}`,`failed`]),d;case 4:return f=window[`${t}Deps`],[4,Promise.all(f.map(function(e){return o(l+e+`.gz`)}))];case 5:return e.sent(),window[`${t}Loader`](),[2,window[`${t}Integration`]]}})})}function O(n,r,o){return t(this,void 0,void 0,function(){var t,s,c,l;return e(this,function(e){return t=i(),s=S(n),c=C(n,o),l=`${t}/${c??s}.js?version=${r}`,[2,a(l)]})})}function k(e){return e?.versionSettings?.override??e?.versionSettings?.version??`latest`}var A=function(e,t){var n=t.type,r=t.bundlingStatus,i=t.versionSettings,a=r!==`unbundled`&&(n===`browser`||(i?.componentTypes)?.includes(`browser`));return e!==`Customer.io Data Pipelines`&&e!==`Iterable`&&a},j=function(e,t){var n=t.All===!1&&t[e]===void 0;return t[e]===!1||n};function M(n,r){return t(this,void 0,void 0,function(){var i,a=this;return e(this,function(o){switch(o.label){case 0:return i=[],_()?[2,r]:[4,h(function(){return r.length>0&&l()},function(){return t(a,void 0,void 0,function(){var t,a,o;return e(this,function(e){switch(e.label){case 0:return t=r.pop(),t?[4,m(t,n)]:[2];case 1:return a=e.sent(),o=a instanceof u,o||i.push(t),[2]}})})})];case 1:return o.sent(),i.map(function(e){return r.pushWithBackoff(e)}),[2,r]}})})}var N=function(){function n(e,t,n,i,a){n===void 0&&(n={}),this.options={},this.type=`destination`,this.middleware=[],this._ready=!1,this._initialized=!1,this.flushing=!1,this.name=e,this.version=t,this.settings=r({},n),this.disableAutoISOConversion=i.disableAutoISOConversion||!1,this.integrationSource=a,this.settings.type&&this.settings.type===`browser`&&delete this.settings.type,this.options=i,this.buffer=i.disableClientPersistence?new d(4,[]):new p(4,`dest-${e}`),this.scheduleFlush()}return n.prototype.isLoaded=function(){return this._ready},n.prototype.ready=function(){return this.onReady??Promise.resolve()},n.prototype.load=function(n,r){var i;return t(this,void 0,void 0,function(){var t,a,o=this;return e(this,function(e){switch(e.label){case 0:return this._ready||this.onReady!==void 0?[2]:(i=this.integrationSource)==null?[3,1]:(a=i,[3,3]);case 1:return[4,D(n,this.name,this.version,this.options.obfuscate)];case 2:a=e.sent(),e.label=3;case 3:t=a,this.integration=E(t,this.settings,r),this.onReady=new Promise(function(e){o.integration.once(`ready`,function(){o._ready=!0,e(!0)})}),this.onInitialize=new Promise(function(e){o.integration.on(`initialize`,function(){o._initialized=!0,e(!0)})});try{n.stats.increment(`analytics_js.integration.invoke`,1,[`method:initialize`,`integration_name:${this.name}`]),this.integration.initialize()}catch(e){throw n.stats.increment(`analytics_js.integration.invoke.error`,1,[`method:initialize`,`integration_name:${this.name}`]),e}return[2]}})})},n.prototype.unload=function(e,t){return O(this.name,this.version,this.options.obfuscate)},n.prototype.addMiddleware=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];this.middleware=(e=this.middleware).concat.apply(e,t)},n.prototype.shouldBuffer=function(e){return e.event.type!==`page`&&(_()||this._ready===!1||this._initialized===!1)},n.prototype.send=function(n,i,a){return t(this,void 0,void 0,function(){var t,o,s,c,l,u;return e(this,function(e){switch(e.label){case 0:if(this.shouldBuffer(n))return this.buffer.push(n),this.scheduleFlush(),[2,n];if(t=this.options?.plan?.track,o=n.event.event,t&&o&&this.name!==`Customer.io Data Pipelines`){if(s=t[o],b(t,s))n.updateEvent(`integrations`,r(r({},n.event.integrations),s?.integrations));else return n.updateEvent(`integrations`,r(r({},n.event.integrations),{All:!1,"Customer.io Data Pipelines":!0})),n.cancel(new f({retry:!1,reason:`Event ${o} disabled for integration ${this.name} in tracking plan`,type:`Dropped by plan`})),[2,n];if(s?.enabled&&s?.integrations[this.name]===!1)return n.cancel(new f({retry:!1,reason:`Event ${o} disabled for integration ${this.name} in tracking plan`,type:`Dropped by plan`})),[2,n]}return[4,v(this.name,n.event,this.middleware)];case 1:if(c=e.sent(),c===null)return[2,n];l=new i(c,{traverse:!this.disableAutoISOConversion}),n.stats.increment(`analytics_js.integration.invoke`,1,[`method:${a}`,`integration_name:${this.name}`]),e.label=2;case 2:return e.trys.push([2,5,,6]),this.integration?[4,this.integration.invoke.call(this.integration,a,l)]:[3,4];case 3:e.sent(),e.label=4;case 4:return[3,6];case 5:throw u=e.sent(),n.stats.increment(`analytics_js.integration.invoke.error`,1,[`method:${a}`,`integration_name:${this.name}`]),u;case 6:return[2,n]}})})},n.prototype.track=function(n){return t(this,void 0,void 0,function(){return e(this,function(e){return[2,this.send(n,x.Track,`track`)]})})},n.prototype.page=function(n){return t(this,void 0,void 0,function(){var t=this;return e(this,function(e){return this.integration?._assumesPageview&&!this._initialized&&this.integration.initialize(),[2,this.onInitialize.then(function(){return t.send(n,x.Page,`page`)})]})})},n.prototype.identify=function(n){return t(this,void 0,void 0,function(){return e(this,function(e){return[2,this.send(n,x.Identify,`identify`)]})})},n.prototype.alias=function(n){return t(this,void 0,void 0,function(){return e(this,function(e){return[2,this.send(n,x.Alias,`alias`)]})})},n.prototype.group=function(n){return t(this,void 0,void 0,function(){return e(this,function(e){return[2,this.send(n,x.Group,`group`)]})})},n.prototype.scheduleFlush=function(){var n=this;this.flushing||setTimeout(function(){return t(n,void 0,void 0,function(){var t;return e(this,function(e){switch(e.label){case 0:return this.flushing=!0,t=this,[4,M(this,this.buffer)];case 1:return t.buffer=e.sent(),this.flushing=!1,this.buffer.todo>0&&this.scheduleFlush(),[2]}})})},Math.random()*5e3)},n}();function P(e,t,i,a,o){if(t===void 0&&(t={}),i===void 0&&(i={}),s())return[];e.plan&&(i??={},i.plan=e.plan);var l=e.middlewareSettings?.routingRules??[],u=e.integrations,d=i.integrations,f=y(e,i??{}),p=o?.reduce(function(e,t){var n;return r(r({},e),(n={},n[w(t)]=t,n))},{}),m=new Set(n(n([],Object.keys(u).filter(function(e){return A(e,u[e])}),!0),Object.keys(p||{}).filter(function(e){return c(u[e])||c(d?.[e])}),!0));return Array.from(m).filter(function(e){return!j(e,t)}).map(function(e){var t=u[e],n=new N(e,k(t),f[e],i,p?.[e]);return l.filter(function(t){return t.destinationName===e}).length>0&&a&&n.addMiddleware(a),n})}export{P as ajsDestinations};