!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.ElasticAppSearch={})}(this,function(e){"use strict";function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e};function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function l(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function f(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}var d=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},o=function(e,t){var n={};for(var r in e)0<=t.indexOf(r)||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n},p=function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var n=[],r=!0,i=!1,a=void 0;try{for(var s,o=e[Symbol.iterator]();!(r=(s=o.next()).done)&&(n.push(s.value),!t||n.length!==t);r=!0);}catch(e){i=!0,a=e}finally{try{!r&&o.return&&o.return()}finally{if(i)throw a}}return n}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")},i=(t(n,[{key:"getRaw",value:function(e){return(this.data[e]||{}).raw}},{key:"getSnippet",value:function(e){return(this.data[e]||{}).snippet}}]),n);function n(e){u(this,n),e._group&&0<e._group.length&&(e=d({},e,{_group:e._group.map(function(e){return new n(e)})})),this.data=e}function a(e,t){u(this,a),this.rawResults=e,this.rawInfo=t;var n=new Array;e.forEach(function(e){n.push(new i(e))}),this.results=n,this.info=t}var v=(t(s,[{key:"removeFilter",value:function(e,t){return new s(function a(s,e){var t=Object.entries(e).reduce(function(e,t){var n=p(t,2),r=n[0],i=n[1];return r===s?e:(["all","any","none"].includes(r)&&(i=i.reduce(function(e,t){var n=a(s,t);return n?e.concat(n):e},[])),d({},e,l({},r,i)))},{});if(0!==Object.keys(t).length)return t}(e,1<arguments.length&&void 0!==t?t:this.filtersJSON))}},{key:"getListOfAppliedFilters",value:function(e){var a=this,t=0<arguments.length&&void 0!==e?e:this.filtersJSON,n=Object.entries(t).reduce(function(n,e){var t=p(e,2),r=t[0],i=t[1];return["all","any","none"].includes(r)?i.forEach(function(t){Object.keys(t).forEach(function(e){["all","any","none"].includes(e)?n=new Set([].concat(f(n),f(a.getListOfAppliedFilters(t)))):n.add(e)})}):n.add(r),n},new Set);return Array.from(n.values())}}]),s);function s(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};u(this,s),this.filtersJSON=e}var h="7.5.0";function c(){u(this,c),this.cache={}}var y=new(t(c,[{key:"getKey",value:function(e,t,n){return e+t+JSON.stringify(n)}},{key:"store",value:function(e,t){this.cache[e]=t}},{key:"retrieve",value:function(e){return this.cache[e]}}]),c);function g(e,t,n,r,i,a){var s=(5<arguments.length&&void 0!==a?a:{}).additionalHeaders,o=y.getKey("POST",t+n,r);if(i){var c=y.retrieve(o);if(c)return Promise.resolve(c)}return function(e,t,n,r,i){var a=(5<arguments.length&&void 0!==arguments[5]?arguments[5]:{}).additionalHeaders,s=new Headers(d({Authorization:"Bearer "+t,"Content-Type":"application/json","X-Swiftype-Client":"elastic-app-search-javascript","X-Swiftype-Client-Version":h},a));return fetch(""+n+r,{method:e,headers:s,body:JSON.stringify(i),credentials:"include"})}("POST",e,t,n,r,{additionalHeaders:s}).then(function(n){return n.json().then(function(e){var t={response:n,json:e};return i&&y.store(o,t),t}).catch(function(){return{response:n,json:{}}})})}var m="SEARCH",S="MULTI_SEARCH";function j(e){return new a(e.results,function(e,t){if(e){e[t];return o(e,[t])}}(e,"results"))}function w(e){var t=e.response,n=e.json;if(t.ok)return n;var r=Array.isArray(n)?" "+function(e){return[].concat.apply([],e)}(n.map(function(e){return e.errors})).join(", "):n.errors?" "+n.errors:"";throw new Error("["+t.status+"]"+r)}var O=(t(b,[{key:"querySuggestion",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:{},r=Object.assign({query:e},n);return g(this.searchKey,this.apiEndpoint,this.querySuggestionPath,r,this.cacheResponses,{additionalHeaders:this.additionalHeaders}).then(w)}},{key:"search",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:{},r=n.disjunctiveFacets,i=n.disjunctiveFacetsAnalyticsTags,a=o(n,["disjunctiveFacets","disjunctiveFacetsAnalyticsTags"]),s=Object.assign({query:e},a);return r&&0<r.length?this._performDisjunctiveSearch(s,r,i).then(j):this._performSearch(s).then(j)}},{key:"multiSearch",value:function(e){var t=e.map(function(e){return d({query:e.query},e.options||{})});return this._performSearch({queries:t},S).then(function(e){return e.map(j)})}},{key:"_performDisjunctiveSearch",value:function(t,n,e){var r=this,i=2<arguments.length&&void 0!==e?e:["Facet-Only"],a=this._performSearch(t),s=new v(t.filters),o=s.getListOfAppliedFilters().filter(function(e){return n.includes(e)});if(!o.length)return a;var c=t.page||{},u=t.analytics||{};u.tags=i;var h=o.map(function(e){return r._performSearch(d({},t,{filters:s.removeFilter(e).filtersJSON,page:d({},c,{size:0}),analytics:u,facets:l({},e,t.facets[e])}))});return Promise.all([a].concat(f(h))).then(function(e){var t=function(e){return Array.isArray(e)?e:Array.from(e)}(e),i=t[0];return t.slice(1).forEach(function(e){var t=p(Object.entries(e.facets)[0],2),n=t[0],r=t[1];i.facets[n]=r}),i})}},{key:"_performSearch",value:function(e,t){var n=(1<arguments.length&&void 0!==t?t:m)===S?this.multiSearchPath:this.searchPath;return g(this.searchKey,this.apiEndpoint,n+".json",e,this.cacheResponses,{additionalHeaders:this.additionalHeaders}).then(w)}},{key:"click",value:function(e){var t=e.query,n=e.documentId,r=e.requestId,i=e.tags,a={query:t,document_id:n,request_id:r,tags:void 0===i?[]:i};return g(this.searchKey,this.apiEndpoint,this.clickPath+".json",a,this.cacheResponses,{additionalHeaders:this.additionalHeaders}).then(w)}}]),b);function b(e,t,n){var r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{},i=r.endpointBase,a=void 0===i?"":i,s=r.cacheResponses,o=void 0===s||s,c=r.additionalHeaders;u(this,b),this.additionalHeaders=c,this.searchKey=t,this.cacheResponses=o,this.engineName=n,this.apiEndpoint=a?a+"/api/as/v1/":"https://"+e+".api.swiftype.com/api/as/v1/",this.searchPath="engines/"+this.engineName+"/search",this.multiSearchPath="engines/"+this.engineName+"/multi_search",this.querySuggestionPath="engines/"+this.engineName+"/query_suggestion",this.clickPath="engines/"+this.engineName+"/click"}e.createClient=function(e){var t=e.hostIdentifier,n=e.accountHostKey,r=e.apiKey,i=e.searchKey,a=e.engineName,s=e.endpointBase,o=e.cacheResponses,c=e.additionalHeaders;return new O(t=t||n,i=i||r,a,{endpointBase:s,cacheResponses:o,additionalHeaders:c})},Object.defineProperty(e,"__esModule",{value:!0})});
