!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((e="undefined"!=typeof globalThis?globalThis:e||self).Datafeeds={})}(this,function(e){"use strict";function s(e){return void 0===e?"":"string"==typeof e?e:e.message}class t{constructor(e,s,t){this._datafeedUrl=e,this._requester=s,this._limitedServerResponse=t}getBars(e,t,r){const i={symbol:e.ticker||"",resolution:t,from:r.from,to:r.to};return void 0!==r.countBack&&(i.countback=r.countBack),void 0!==e.currency_code&&(i.currencyCode=e.currency_code),void 0!==e.unit_id&&(i.unitId=e.unit_id),new Promise(async(e,t)=>{try{const s=await this._requester.sendRequest(this._datafeedUrl,"history",i),t=this._processHistoryResponse(s);this._limitedServerResponse&&await this._processTruncatedResponse(t,i),e(t)}catch(e){if(e instanceof Error||"string"==typeof e){const r=s(e);console.warn(`HistoryProvider: getBars() failed, error=${r}`),t(r)}}})}async _processTruncatedResponse(e,t){let r=e.bars.length;try{for(;this._limitedServerResponse&&this._limitedServerResponse.maxResponseLength>0&&this._limitedServerResponse.maxResponseLength===r&&t.from0)){this._requestsPending=0;for(const e in this._subscribers)this._requestsPending+=1,this._updateDataForSubscriber(e).then(()=>{this._requestsPending-=1,this._requestsPending}).catch(e=>{this._requestsPending-=1,s(e),this._requestsPending})}}_updateDataForSubscriber(e){const s=this._subscribers[e],t=parseInt((Date.now()/1e3).toString()),r=t-function(e,s){let t=0;t="D"===e||"1D"===e?s:"M"===e||"1M"===e?31*s:"W"===e||"1W"===e?7*s:s*parseInt(e)/1440;return 24*t*60*60}(s.resolution,10);return this._historyProvider.getBars(s.symbolInfo,s.resolution,{from:r,to:t,countBack:2,firstDataRequest:!1}).then(s=>{this._onSubscriberDataReceived(e,s)})}_onSubscriberDataReceived(e,s){if(!this._subscribers.hasOwnProperty(e))return;const t=s.bars;if(0===t.length)return;const r=t[t.length-1],i=this._subscribers[e];if(null!==i.lastBarTime&&r.timei.lastBarTime){if(t.length<2)throw new Error("Not enough bars in history for proper pulse update. Need at least 2.");const e=t[t.length-2];i.listener(e)}i.lastBarTime=r.time,i.listener(r)}}class i{constructor(e){this._subscribers={},this._requestsPending=0,this._timers=null,this._quotesProvider=e}subscribeQuotes(e,s,t,r){this._subscribers[r]={symbols:e,fastSymbols:s,listener:t},this._createTimersIfRequired()}unsubscribeQuotes(e){delete this._subscribers[e],0===Object.keys(this._subscribers).length&&this._destroyTimers()}_createTimersIfRequired(){if(null===this._timers){const e=window.setInterval(this._updateQuotes.bind(this,1),1e4),s=window.setInterval(this._updateQuotes.bind(this,0),6e4);this._timers={fastTimer:e,generalTimer:s}}}_destroyTimers(){null!==this._timers&&(clearInterval(this._timers.fastTimer),clearInterval(this._timers.generalTimer),this._timers=null)}_updateQuotes(e){if(!(this._requestsPending>0))for(const t in this._subscribers){this._requestsPending++;const r=this._subscribers[t];this._quotesProvider.getQuotes(1===e?r.fastSymbols:r.symbols).then(e=>{this._requestsPending--,this._subscribers.hasOwnProperty(t)&&(r.listener(e),this._requestsPending)}).catch(e=>{this._requestsPending--,s(e),this._requestsPending})}}}function o(e,s,t,r){const i=e[s];return!Array.isArray(i)||r&&!Array.isArray(i[0])?i:i[t]}function n(e,s,t){return e+(void 0!==s?"_%|#|%_"+s:"")+(void 0!==t?"_%|#|%_"+t:"")}class a{constructor(e,s,t){this._exchangesList=["NYSE","FOREX","AMEX"],this._symbolsInfo={},this._symbolsList=[],this._datafeedUrl=e,this._datafeedSupportedResolutions=s,this._requester=t,this._readyPromise=this._init(),this._readyPromise.catch(e=>{console.error(`SymbolsStorage: Cannot init, error=${e.toString()}`)})}resolveSymbol(e,s,t){return this._readyPromise.then(()=>{const r=this._symbolsInfo[n(e,s,t)];return void 0===r?Promise.reject("invalid symbol"):Promise.resolve(r)})}searchSymbols(e,s,t,r){return this._readyPromise.then(()=>{const i=[],o=0===e.length;e=e.toUpperCase();for(const r of this._symbolsList){const n=this._symbolsInfo[r];if(void 0===n)continue;if(t.length>0&&n.type!==t)continue;if(s&&s.length>0&&n.exchange!==s)continue;const a=n.name.toUpperCase().indexOf(e),c=n.description.toUpperCase().indexOf(e);if(o||a>=0||c>=0){if(!i.some(e=>e.symbolInfo===n)){const e=a>=0?a:8e3+c;i.push({symbolInfo:n,weight:e})}}}const n=i.sort((e,s)=>e.weight-s.weight).slice(0,r).map(e=>{const s=e.symbolInfo;return{symbol:s.name,full_name:`${s.exchange}:${s.name}`,description:s.description,exchange:s.exchange,params:[],type:s.type,ticker:s.name}});return Promise.resolve(n)})}_init(){const e=[],s={};for(const t of this._exchangesList)s[t]||(s[t]=!0,e.push(this._requestExchangeData(t)));return Promise.all(e).then(()=>{this._symbolsList.sort()})}_requestExchangeData(e){return new Promise((t,r)=>{this._requester.sendRequest(this._datafeedUrl,"symbol_info",{group:e}).then(s=>{try{this._onExchangeDataReceived(e,s)}catch(e){return void r(e instanceof Error?e:new Error(`SymbolsStorage: Unexpected exception ${e}`))}t()}).catch(e=>{s(e),t()})})}_onExchangeDataReceived(e,s){let t=0;try{const e=s.symbol.length,r=void 0!==s.ticker;for(;t{null===e&&(e={supports_search:!1,supports_group_request:!0,supported_resolutions:["1","5","15","30","60","1D","1W","1M"],supports_marks:!1,supports_timescale_marks:!1}),this._setupWithConfiguration(e)})}onReady(e){this._configurationReadyPromise.then(()=>{e(this._configuration)})}getQuotes(e,s,t){this._quotesProvider.getQuotes(e).then(s).catch(t)}subscribeQuotes(e,s,t,r){this._quotesPulseProvider.subscribeQuotes(e,s,t,r)}unsubscribeQuotes(e){this._quotesPulseProvider.unsubscribeQuotes(e)}getMarks(e,t,r,i,o){if(!this._configuration.supports_marks)return;const n={symbol:e.ticker||"",from:t,to:r,resolution:o};this._send("marks",n).then(e=>{if(!Array.isArray(e)){const s=[];for(let t=0;t{s(e),i([])})}getTimescaleMarks(e,t,r,i,o){if(!this._configuration.supports_timescale_marks)return;const n={symbol:e.ticker||"",from:t,to:r,resolution:o};this._send("timescale_marks",n).then(e=>{if(!Array.isArray(e)){const s=[];for(let t=0;t{s(e),i([])})}getServerTime(e){this._configuration.supports_time&&this._send("time").then(s=>{const t=parseInt(s);isNaN(t)||e(t)}).catch(e=>{s(e)})}searchSymbols(e,t,r,i){if(this._configuration.supports_search){const o={limit:30,query:e.toUpperCase(),type:r,exchange:t};this._send("search",o).then(e=>{if(void 0!==e.s)return e.errmsg,void i([]);i(e)}).catch(e=>{s(e),i([])})}else{if(null===this._symbolsStorage)throw new Error("UdfCompatibleDatafeed: inconsistent configuration (symbols storage)");this._symbolsStorage.searchSymbols(e,t,r,30).then(i).catch(i.bind(null,[]))}}resolveSymbol(e,t,r,i){const o=i&&i.currencyCode,n=i&&i.unitId;function a(e){t(e)}if(this._configuration.supports_group_request){if(null===this._symbolsStorage)throw new Error("UdfCompatibleDatafeed: inconsistent configuration (symbols storage)");this._symbolsStorage.resolveSymbol(e,o,n).then(a).catch(r)}else{const t={symbol:e};void 0!==o&&(t.currencyCode=o),void 0!==n&&(t.unitId=n),this._send("symbols",t).then(e=>{if(void 0!==e.s)r("unknown_symbol");else{const s=e.name,t=e.listed_exchange??e["exchange-listed"],r=e.exchange??e["exchange-traded"];a({...e,name:s,base_name:[t+":"+s],listed_exchange:t,exchange:r,ticker:e.ticker,currency_code:e.currency_code??e["currency-code"],original_currency_code:e.original_currency_code??e["original-currency-code"],unit_id:e.unit_id??e["unit-id"],original_unit_id:e.original_unit_id??e["original-unit-id"],unit_conversion_types:e.unit_conversion_types??e["unit-conversion-types"],has_intraday:e.has_intraday??e["has-intraday"]??!1,visible_plots_set:e.visible_plots_set??e["visible-plots-set"],minmov:e.minmovement??e.minmov??0,minmove2:e.minmovement2??e.minmove2,session:e.session??e["session-regular"],session_holidays:e.session_holidays??e["session-holidays"],supported_resolutions:e.supported_resolutions??e["supported-resolutions"]??this._configuration.supported_resolutions??[],has_daily:e.has_daily??e["has-daily"]??!0,intraday_multipliers:e.intraday_multipliers??e["intraday-multipliers"]??["1","5","15","30","60"],has_weekly_and_monthly:e.has_weekly_and_monthly??e["has-weekly-and-monthly"],has_empty_bars:e.has_empty_bars??e["has-empty-bars"],volume_precision:e.volume_precision??e["volume-precision"],format:e.format??"price"})}}).catch(e=>{s(e),r("unknown_symbol")})}}getBars(e,s,t,r,i){this._historyProvider.getBars(e,s,t).then(e=>{r(e.bars,e.meta)}).catch(i)}subscribeBars(e,s,t,r,i){this._dataPulseProvider.subscribeBars(e,s,t,r)}unsubscribeBars(e){this._dataPulseProvider.unsubscribeBars(e)}_requestConfiguration(){return this._send("config").catch(e=>(s(e),null))}_send(e,s){return this._requester.sendRequest(this._datafeedURL,e,s)}_setupWithConfiguration(e){if(this._configuration=e,void 0===e.exchanges&&(e.exchanges=[]),!e.supports_search&&!e.supports_group_request)throw new Error("Unsupported datafeed configuration. Must either support search, or support group request");!e.supports_group_request&&e.supports_search||(this._symbolsStorage=new a(this._datafeedURL,e.supported_resolutions||[],this._requester)),JSON.stringify(e)}}class l{constructor(e,s){this._datafeedUrl=e,this._requester=s}getQuotes(e){return new Promise((t,r)=>{this._requester.sendRequest(this._datafeedUrl,"quotes",{symbols:e}).then(e=>{"ok"===e.s?t(e.d):r(e.errmsg)}).catch(e=>{const t=s(e);r(`network error: ${t}`)})})}}class d{constructor(e){e&&(this._headers=e)}sendRequest(e,s,t){if(void 0!==t){const e=Object.keys(t);0!==e.length&&(s+="?"),s+=e.map(e=>`${encodeURIComponent(e)}=${encodeURIComponent(t[e].toString())}`).join("&")}const r={credentials:"same-origin"};return void 0!==this._headers&&(r.headers=this._headers),fetch(`${e}/${s}`,r).then(e=>e.text()).then(e=>JSON.parse(e))}}e.UDFCompatibleDatafeed=class extends h{constructor(e,s=1e4,t){const r=new d;super(e,new l(e,r),r,s,t)}}});