/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/heos-api@4.0.0/dist/heos-api.umd.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("dgram"),require("net")):"function"==typeof define&&define.amd?define(["exports","dgram","net"],t):t(e.heosApi={},e.dgram,e.net)}(this,(function(e,t,n){"use strict";var r;function o(){}function i(){i.init.call(this)}function s(e){return void 0===e._maxListeners?i.defaultMaxListeners:e._maxListeners}function u(e,t,n,r){var i,u,c,a;if("function"!=typeof n)throw new TypeError('"listener" argument must be a function');if((u=e._events)?(u.newListener&&(e.emit("newListener",t,n.listener?n.listener:n),u=e._events),c=u[t]):(u=e._events=new o,e._eventsCount=0),c){if("function"==typeof c?c=u[t]=r?[n,c]:[c,n]:r?c.unshift(n):c.push(n),!c.warned&&(i=s(e))&&i>0&&c.length>i){c.warned=!0;var f=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");f.name="MaxListenersExceededWarning",f.emitter=e,f.type=t,f.count=c.length,a=f,"function"==typeof console.warn?console.warn(a):console.log(a)}}else c=u[t]=n,++e._eventsCount;return e}function c(e,t,n){var r=!1;function o(){e.removeListener(t,o),r||(r=!0,n.apply(e,arguments))}return o.listener=n,o}function a(e){var t=this._events;if(t){var n=t[e];if("function"==typeof n)return 1;if(n)return n.length}return 0}function f(e,t){for(var n=new Array(t);t--;)n[t]=e[t];return n}function h(e){return e.commandGroup+"/"+e.command}o.prototype=Object.create(null),i.EventEmitter=i,i.usingDomains=!1,i.prototype.domain=void 0,i.prototype._events=void 0,i.prototype._maxListeners=void 0,i.defaultMaxListeners=10,i.init=function(){this.domain=null,i.usingDomains&&r.active&&r.Domain,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new o,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},i.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},i.prototype.getMaxListeners=function(){return s(this)},i.prototype.emit=function(e){var t,n,r,o,i,s,u,c="error"===e;if(s=this._events)c=c&&null==s.error;else if(!c)return!1;if(u=this.domain,c){if(t=arguments[1],!u){if(t instanceof Error)throw t;var a=new Error('Uncaught, unspecified "error" event. ('+t+")");throw a.context=t,a}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=u,t.domainThrown=!1,u.emit("error",t),!1}if(!(n=s[e]))return!1;var h="function"==typeof n;switch(r=arguments.length){case 1:!function(e,t,n){if(t)e.call(n);else for(var r=e.length,o=f(e,r),i=0;i<r;++i)o[i].call(n)}(n,h,this);break;case 2:!function(e,t,n,r){if(t)e.call(n,r);else for(var o=e.length,i=f(e,o),s=0;s<o;++s)i[s].call(n,r)}(n,h,this,arguments[1]);break;case 3:!function(e,t,n,r,o){if(t)e.call(n,r,o);else for(var i=e.length,s=f(e,i),u=0;u<i;++u)s[u].call(n,r,o)}(n,h,this,arguments[1],arguments[2]);break;case 4:!function(e,t,n,r,o,i){if(t)e.call(n,r,o,i);else for(var s=e.length,u=f(e,s),c=0;c<s;++c)u[c].call(n,r,o,i)}(n,h,this,arguments[1],arguments[2],arguments[3]);break;default:for(o=new Array(r-1),i=1;i<r;i++)o[i-1]=arguments[i];!function(e,t,n,r){if(t)e.apply(n,r);else for(var o=e.length,i=f(e,o),s=0;s<o;++s)i[s].apply(n,r)}(n,h,this,o)}return!0},i.prototype.addListener=function(e,t){return u(this,e,t,!1)},i.prototype.on=i.prototype.addListener,i.prototype.prependListener=function(e,t){return u(this,e,t,!0)},i.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,c(this,e,t)),this},i.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,c(this,e,t)),this},i.prototype.removeListener=function(e,t){var n,r,i,s,u;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(r=this._events))return this;if(!(n=r[e]))return this;if(n===t||n.listener&&n.listener===t)0==--this._eventsCount?this._events=new o:(delete r[e],r.removeListener&&this.emit("removeListener",e,n.listener||t));else if("function"!=typeof n){for(i=-1,s=n.length;s-- >0;)if(n[s]===t||n[s].listener&&n[s].listener===t){u=n[s].listener,i=s;break}if(i<0)return this;if(1===n.length){if(n[0]=void 0,0==--this._eventsCount)return this._events=new o,this;delete r[e]}else!function(e,t){for(var n=t,r=n+1,o=e.length;r<o;n+=1,r+=1)e[n]=e[r];e.pop()}(n,i);r.removeListener&&this.emit("removeListener",e,u||t)}return this},i.prototype.removeAllListeners=function(e){var t,n;if(!(n=this._events))return this;if(!n.removeListener)return 0===arguments.length?(this._events=new o,this._eventsCount=0):n[e]&&(0==--this._eventsCount?this._events=new o:delete n[e]),this;if(0===arguments.length){for(var r,i=Object.keys(n),s=0;s<i.length;++s)"removeListener"!==(r=i[s])&&this.removeAllListeners(r);return this.removeAllListeners("removeListener"),this._events=new o,this._eventsCount=0,this}if("function"==typeof(t=n[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},i.prototype.listeners=function(e){var t,n=this._events;return n&&(t=n[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),n=0;n<t.length;++n)t[n]=e[n].listener||e[n];return t}(t):[]},i.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):a.call(e,t)},i.prototype.listenerCount=a,i.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var l=function(){function e(){this.emitter=new i,this.listenersOnAll=[]}return e.prototype.put=function(e){var t=h(e.heos.command);this.listenersOnAll.forEach((function(t){return t(e)})),this.emitter.emit(t,e)},e.prototype.on=function(e,t){var n=h(e);return this.emitter.on(n,t),this},e.prototype.once=function(e,t){var n=h(e);return this.emitter.once(n,t),this},e.prototype.onAll=function(e){return this.listenersOnAll=this.listenersOnAll.concat([e]),this},e}(),p=function(){return p=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},p.apply(this,arguments)};
/*! *****************************************************************************
  Copyright (c) Microsoft Corporation. All rights reserved.
  Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  this file except in compliance with the License. You may obtain a copy of the
  License at http://www.apache.org/licenses/LICENSE-2.0

  THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  MERCHANTABLITY OR NON-INFRINGEMENT.

  See the Apache Version 2.0 License for specific language governing permissions
  and limitations under the License.
  ***************************************************************************** */function m(e){return!(Object.keys(e).length&&(!e.hasOwnProperty("unparsed")||"string"!=typeof e.unparsed||e.hasOwnProperty("parsed")&&(!e.hasOwnProperty("parsed")||"object"!=typeof e.parsed)))}function d(e){return e.hasOwnProperty("heos")&&e.heos.hasOwnProperty("command")&&"string"==typeof e.heos.command}var v=function(){function e(e){this.buffer="",this.callback=e}return e.prototype.put=function(e){var t=this;this.buffer+=e;var n=this.buffer.split("\r\n"),r=n.pop();""===r?(n.push(r),this.buffer=""):this.buffer=r||"";try{n.filter((function(e){return e.length>0})).map((function(e){return JSON.parse(e)})).map((function(e){if(d)return e;throw new TypeError})).map((function(e){var t,n,r=(t=e.heos.command,{commandGroup:(n=t.split("/"))[0],command:n[1]});return e.heos.command=r,e})).map((function(e){var t,n,r=(t=e.heos.message,n={},"string"==typeof t&&(n=t.split("&").reduce((function(e,t){var n,r=t.split("="),o=r[0],i=r[1],s=r.slice(2);return p({},e,o&&i&&!s.length&&((n={})[o]=isNaN(Number(i))?i:Number(i),n))}),n)),p({},"string"==typeof t&&{unparsed:t},Object.keys(n).length&&{parsed:n}));return e.heos.message=r,e})).map((function(e){if(function(e){if(e.hasOwnProperty("heos")){var t=e.heos;if(t.hasOwnProperty("command")&&t.hasOwnProperty("result")&&t.hasOwnProperty("message")&&"object"==typeof t.command&&"string"==typeof t.result&&"object"==typeof t.message&&m(t.message)&&t.command.hasOwnProperty("commandGroup")&&t.command.hasOwnProperty("command")&&"string"==typeof t.command.commandGroup&&"string"==typeof t.command.command)return!0}return!1}(e)||function(e){if(e.hasOwnProperty("heos")){var t=e.heos;if(t.hasOwnProperty("command")&&"object"==typeof t.command&&t.command.hasOwnProperty("commandGroup")&&t.command.hasOwnProperty("command")&&"string"==typeof t.command.commandGroup&&"string"==typeof t.command.command)return!t.hasOwnProperty("message")||"object"!=typeof t.message||m(t.message)}return!1}(e))return e;throw new TypeError})).forEach((function(e){try{t.callback(e)}catch(e){console.log("Error handling response")}}))}catch(e){e instanceof TypeError?console.log("Heos response has wrong structure. Flushing buffer."):console.log("Error parsing incoming messages. Flushing buffer."),this.buffer=""}},e}(),y="heos://",g="\r\n";function w(e){return!e||Object.entries(e).length<1?"":"?"+Object.entries(e).map((function(e){return e[0]+"="+e[1]})).reduce((function(e,t){return e+"&"+t}))}var b=function(){function e(e,t,n,r){var o=this;this.closed=!1,this.on=function(t,n){return o.closed?console.warn("You are trying to add an event listener to a closed HeosConnection."):e(t,n),o},this.once=function(e,n){return o.closed?console.warn("You are trying to add an event listener to a closed HeosConnection."):t(e,n),o},this.onAll=function(e){return o.closed?console.warn("You are trying to add an event listener to a closed HeosConnection."):n(e),o},this.socket=r}return e.prototype.write=function(e,t,n){return this.closed?console.warn("You are trying to write to a closed HeosConnection."):this.socket.write(function(e,t,n){if(!e||!t)throw new Error("Missing arguments when creating HeosCommand");return[y,e,"/",t,w(n),g].join("")}(e,t,n)),this},e.prototype.close=function(){var e=this;return this.closed=!0,new Promise((function(t){e.socket.end("",void 0,t)}))},e.prototype.onClose=function(e){return this.socket.on("close",e),this},e.prototype.onError=function(e){return this.socket.on("error",e),this},e}();function O(e){return new Promise((function(t,r){var o=new l,i=new v((function(e){return o.put(e)}));(function(e,t){var r=!1;return new Promise((function(o,i){var s=e;try{var u=n.createConnection({port:1255,host:s,localPort:0},(function(){r=!0,o(u)}));u.on("data",(function(e){return t.put(e)})),u.on("timeout",(function(){r||(u.end(),r=!0,i(new Error("Socket timeout")))})),u.on("error",(function(e){r?console.error(e):(r=!0,i(e))}))}catch(e){i(e)}}))})(e,i).then((function(e){var n=new b((function(e,t){return o.on(e,t)}),(function(e,t){return o.once(e,t)}),(function(e){return o.onAll(e)}),e);t(n)})).catch(r)}))}var _="urn:schemas-denon-com:device:ACT-Denon:1",L=["M-SEARCH * HTTP/1.1","HOST: 239.255.255.250:1900","ST: "+_,"MX: 5",'MAN: "ssdp:discover"',"\r\n"].join("\r\n"),P=5e3;function E(e,n,r){var o="number"==typeof e?e||P:e.timeout||P,i=t.createSocket("udp4");"number"!=typeof e?i.bind(e.port,e.address):i.bind(),i.on("listening",(function(){i.send(L,1900,"239.255.255.250")}));var s=[];i.on("message",(function(e,t){e.includes(_)&&(n(t.address),s.push(t.address))}));var u=setTimeout(c,o);function c(){i.close(),r&&r(s),global.clearTimeout(u)}return c}function C(e){return void 0===e&&(e=P),new Promise((function(t,n){var r=!1;var o=E(e,(function(e){r||(r=!0,o(),t(e))}),(function(e){r||n("No devices found")}))}))}e.discoverDevices=E,e.discoverOneDevice=C,e.discoverAndConnect=function(e){return void 0===e&&(e=P),new Promise((function(t,n){C(e).then(O).then(t).catch(n)}))},e.connect=O,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=/sm/61348624cc0330fbdc0e43f10df0ce23687380ca43951a71c9fd139c816251d0.map