import { Injectable, EventEmitter, LOCALE_ID, Inject } from '@angular/core';
var GoogleChartsLoaderService = /** @class */ (function () {
function GoogleChartsLoaderService(localeId) {
this.chartPackage = {
AnnotationChart: 'annotationchart',
AreaChart: 'corechart',
Bar: 'bar',
BarChart: 'corechart',
BubbleChart: 'corechart',
Calendar: 'calendar',
CandlestickChart: 'corechart',
ColumnChart: 'corechart',
ComboChart: 'corechart',
PieChart: 'corechart',
Gantt: 'gantt',
Gauge: 'gauge',
GeoChart: 'geochart',
Histogram: 'corechart',
Line: 'line',
LineChart: 'corechart',
Map: 'map',
OrgChart: 'orgchart',
Sankey: 'sankey',
Scatter: 'scatter',
ScatterChart: 'corechart',
SteppedAreaChart: 'corechart',
Table: 'table',
Timeline: 'timeline',
TreeMap: 'treemap',
WordTree: 'wordtree'
};
this.googleScriptLoadingNotifier = new EventEmitter();
this.googleScriptIsLoading = false;
this.localeId = localeId;
}
GoogleChartsLoaderService.prototype.load = function (chartType, apiKey) {
var _this = this;
return new Promise(function (resolve, reject) {
if (resolve === void 0) { resolve = Function.prototype; }
if (reject === void 0) { reject = Function.prototype; }
_this.loadGoogleChartsScript().then(function () {
var initializer = {
packages: [_this.chartPackage[chartType]],
language: _this.localeId,
callback: resolve
};
if (apiKey) {
initializer.mapsApiKey = apiKey;
}
google.charts.load('45.2', initializer);
}).catch(function (err) { return reject(); });
});
};
GoogleChartsLoaderService.prototype.loadGoogleChartsScript = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (resolve === void 0) { resolve = Function.prototype; }
if (reject === void 0) { reject = Function.prototype; }
if (typeof google !== 'undefined' && google.charts) {
resolve();
}
else if (!_this.googleScriptIsLoading) {
_this.googleScriptIsLoading = true;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.gstatic.com/charts/loader.js';
script.async = true;
script.defer = true;
script.onload = function () {
_this.googleScriptIsLoading = false;
_this.googleScriptLoadingNotifier.emit(true);
resolve();
};
script.onerror = function () {
_this.googleScriptIsLoading = false;
_this.googleScriptLoadingNotifier.emit(false);
reject();
};
document.getElementsByTagName('head')[0].appendChild(script);
}
else {
_this.googleScriptLoadingNotifier.subscribe(function (loaded) {
if (loaded) {
resolve();
}
else {
reject();
}
});
}
});
};
GoogleChartsLoaderService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
GoogleChartsLoaderService.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [LOCALE_ID,] },] },
]; };
return GoogleChartsLoaderService;
}());
export { GoogleChartsLoaderService };