/* * @license * vendOS v2.0.0 * (c) 2020 Social Vend Ltd. trading as vendOS * Released under the MIT license * vendos.io */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).VendOS=t()}(this,(function(){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var t=function(e,t){return e(t={exports:{}},t.exports),t.exports}((function(t){!function(e){function n(){}var s=n.prototype,o=e.EventEmitter;function i(e,t){for(var n=e.length;n--;)if(e[n].listener===t)return n;return-1}function r(e){return function(){return this[e].apply(this,arguments)}}s.getListeners=function(e){var t,n,s=this._getEvents();if(e instanceof RegExp)for(n in t={},s)s.hasOwnProperty(n)&&e.test(n)&&(t[n]=s[n]);else t=s[e]||(s[e]=[]);return t},s.flattenListeners=function(e){var t,n=[];for(t=0;t{console&&console.info&&console.info(`%cVendOS SDK: ${e}`,n)},r=e=>{console&&console.info&&console.info(`%cVendOS SDK: ${e}`,s)};function c(e){const{env:t}=window.process||{env:{}};return t[e]}const d=new class extends t{constructor(){super(),this.connectionAttempts=0}send(e){this.socket.readyState===WebSocket.OPEN&&this.socket.send(JSON.stringify(e))}message(e){const t=JSON.parse(e||null);if(t){const{id:e}=t;void 0===e?this.emit("machineEvent",t):this.emit(`message.${t.id}`,(n=["id"],s=t,Object.keys(s).filter(e=>!n.includes(e)).reduce((e,t)=>({...e,[t]:s[t]}),{})))}var n,s}open(){i(o.CONNECTED_WEBSOCKET),this.socket.onclose=this.close.bind()}close(){r(o.WEBSOCKET_CLOSED),this.connectionAttempts=0,this.connectToMachine()}connect(){c("__VENDOS_FIELD_COMMAND_VERSION__")?this.connectToMachine():this.connectToDevtools()}connectToMachine(){i(o.WEBSOCKET_ATTEMPT),this.socket=new WebSocket("ws://fieldcommand:3000"),this.socket.onclose=this.reconnectToMachine.bind(this),this.socket.onopen=this.open.bind(this),this.socket.onmessage=this.message.bind(this)}connectToDevtools(){if(i(o.DEVTOOLS_ATTEMPT),window.__VENDOS_DEVTOOLS_EXTENSION__){const e=window.__VENDOS_DEVTOOLS_EXTENSION__,t=e.listen(n=>{"handshake"===n.id&&(t(),this.socket={readyState:WebSocket.OPEN,send:t=>e.send(JSON.parse(t))},e.listen(e=>this.message(JSON.stringify(e))),i(o.CONNECTED_DEVTOOLS))});e.handshake()}else setTimeout(this.reconnectToDevtools.bind(this),500)}reconnectToMachine(){this.connectionAttempts++,5-this.connectionAttempts>0?setTimeout(this.connectToMachine.bind(this),100):this.failed()}reconnectToDevtools(){this.connectionAttempts++,5-this.connectionAttempts>0?setTimeout(this.connectToDevtools.bind(this),100):this.failed()}failed(){c("__VENDOS_FIELD_COMMAND_VERSION__")?r(o.WEBSOCKET_CONNECTION_FAILED):r(o.DEVTOOLS_CONNECTION_FAILED)}};let a=0;class h extends t{async send(e){const t=a++;return d.send({...e,id:t}),this.receive(t)}receive(e){return new Promise((t,n)=>{d.once(`message.${e}`,e=>{e.ok?t(e):n(e)})})}}class u extends h{async send(...e){return super.send({resource:"social",...e})}}class l extends u{async send(...e){return super.send({action:"instagram",...e})}friendshipStatus({username:e}){return this.send({method:"friendshipStatus",username:e})}}class f extends u{async send(...e){return super.send({action:"twitter",...e})}searchPosts({username:e,hashtag:t}){return this.send({method:"searchPosts",username:e,hashtag:t})}}class m extends h{async send(e){return super.send({resource:"data",...e})}}class p extends m{async send(e){return super.send({action:"local",...e})}createSet(e){return this.send({method:"createSet",...e})}deleteSet(e){return this.send({method:"deleteSet",...e})}save(e){return this.send({method:"save",...e})}get(e){return this.send({method:"get",...e})}update(e){return this.send({method:"update",...e})}delete(e){return this.send({method:"delete",...e})}}class E extends h{async send(e){return super.send({resource:"machine",...e})}}class O extends E{async send(e){return super.send({action:"vend",...e})}random(e){return this.send({method:"random",...e})}channel(e){return this.send({method:"channel",...e})}optimal(e){return this.send({method:"optimal",...e})}}class v extends E{async send(e){return super.send({action:"channels",...e})}get(){return this.send({method:"get"})}}class _ extends h{async send(e){return super.send({resource:"payment",...e})}}class S extends _{async send(e){return super.send({action:"charge",...e})}instant(e){return this.send({method:"instant",...e})}hold(e){return this.send({method:"hold",...e})}confirm(e){return this.send({method:"confirm",...e})}cancel(){return this.send({method:"cancel"})}}-1===navigator.userAgent.indexOf("Chrome")&&r("It is highly recommended that you develop vendOS in Chrome v.77+.");return class{constructor(){d.connect(),this.Machine={vend:new O,channels:new v},this.Social={twitter:new f,instagram:new l},this.Data={local:new p},this.Payment={charge:new S}}}}));