var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/************************************************
Period Over Period Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var PeriodOverPeriodGadget = (function (_super) {
            __extends(PeriodOverPeriodGadget, _super);
            function PeriodOverPeriodGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PeriodOverPeriodRender();
                _super.call(this, gadgetOptions);
            }
            PeriodOverPeriodGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PeriodOverPeriodGadget(gadgetOptions);
                return instance;
            };
            PeriodOverPeriodGadget.prototype.formatData = function (data) {
                // PoP does not actually need to format the data differently than the ChartingDataGadget currently
                // performs.  We want to take this opportunity to place in 2 fake entries into the SeriesMap.
                // these 2 series are the localized pointers for the "this" and "last" periods that will map to
                // the Series1 and Series2 data.
                // This is later used by the Export logic to map data as well as getting
                // the series display names.
                data = _super.prototype.formatData.call(this, data);
                var viewPeriod = this.getViewPeriod(this.getHandler());
                var realSeriesProperty = Object.keys(this.seriesMap)[0];
                this.seriesMap["Series1"] = _.clone(this.seriesMap[realSeriesProperty]);
                this.seriesMap["Series1"].name = "Series1";
                if (viewPeriod && viewPeriod.Formulas) {
                    this.seriesMap["Series1"].title = this.buildSeriesLabel(viewPeriod.Name, 1);
                }
                else {
                    this.seriesMap["Series1"].title = this.seriesMap[realSeriesProperty].title;
                }
                this.seriesMap["Series2"] = _.clone(this.seriesMap[realSeriesProperty]);
                this.seriesMap["Series2"].name = "Series2";
                if (viewPeriod && viewPeriod.Formulas) {
                    this.seriesMap["Series2"].title = this.buildSeriesLabel(viewPeriod.Name, 2);
                }
                else {
                    this.seriesMap["Series1"].title = this.seriesMap[realSeriesProperty].title;
                }
                return data;
            };
            PeriodOverPeriodGadget.prototype.buildSeriesLabel = function (viewingPeriodName, seriesNumber) {
                // build a localization key from the viewing period name and the series number
                var seriesLabelKey = StringUtil.format("PeriodOverPeriodGadget_{0}{1}", viewingPeriodName, seriesNumber);
                var seriesLabel = i18n.loc.unencoded("Gadgets", seriesLabelKey, false);
                // if not found use the built-in generic custom period labels
                if (StringUtil.isNullOrEmpty(seriesLabel)) {
                    // Resolving Bug: 27075: Dashboard: Period over Period Gadget shows blank series labels for custom date ranges.
                    // Adding default values for custom date range support.
                    seriesLabelKey = StringUtil.format("PeriodOverPeriodGadget_CustomPeriod{0}", seriesNumber);
                    seriesLabel = i18n.loc.unencoded("Gadgets", seriesLabelKey);
                }
                return seriesLabel;
            };
            PeriodOverPeriodGadget.TypeId = "3116f03a-28f0-417e-94c9-0c94d409e989";
            return PeriodOverPeriodGadget;
        }(Models.ChartingDataGadget));
        Models.PeriodOverPeriodGadget = PeriodOverPeriodGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PeriodOverPeriodRender = (function (_super) {
            __extends(PeriodOverPeriodRender, _super);
            function PeriodOverPeriodRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            PeriodOverPeriodRender.prototype.destroy = function () {
                _super.prototype.destroy.call(this);
                this.gadget = null;
            };
            PeriodOverPeriodRender.prototype.updateGadget = function (handler) {
                var chartOptions = Dashboards.Views.ChartHelper.GetDefaultChartOptions(this);
                chartOptions.axes = [Dashboards.Views.ChartHelper.GetStandardBottomXAxis(this)];
                var leftAxis = Dashboards.Views.ChartHelper.GetStandardLeftYAxis(this);
                leftAxis.title = handler.byName(Settings.Gadget.LeftAxisTitle.name);
                Dashboards.Views.ChartHelper.BindChartLeftAxisLimits(leftAxis, handler);
                chartOptions.axes.push(leftAxis);
                Dashboards.Views.ChartHelper.BindTargetLines(this, chartOptions.axes, handler);
                var dataPoints = $.extend(true, {}, this.gadget.dataSet.ChartingData.DataPoints); // Deep copy of data
                if (dataPoints.Series1 == null)
                    dataPoints.Series1 = [];
                if (dataPoints.Series2 == null)
                    dataPoints.Series2 = [];
                // It is very important that each series has the same number of data points for jqChart 
                // to render sanely.
                // We will pad out any missing values.
                var series1Count = dataPoints.Series1.length;
                var series2Count = dataPoints.Series2.length;
                var maxDataPoints = Math.max(series1Count, series2Count);
                for (var dpIdx = 0; dpIdx < maxDataPoints; dpIdx++) {
                    if (!dataPoints.Series1[dpIdx]) {
                        dataPoints.Series1[dpIdx] = [
                            dataPoints.Series2[dpIdx][Dashboards.Models.ChartingDataGadget.AxisLabelIdx],
                            null // Do not pass in a value
                        ];
                    }
                }
                var chartSeries = [];
                // Special case: Period over Period always returns "Series1" and "Series2"
                var seriesProperty = Object.keys(this.gadget.seriesMap)[0];
                var curSeries = this.gadget.seriesMap[seriesProperty];
                var currentPeriodColorTc = tinycolor(curSeries.color);
                var currentPeriodColor = currentPeriodColorTc.toRgbString();
                var previousPeriodColorTc = tinycolor(curSeries.color).lighten().lighten();
                previousPeriodColorTc.setAlpha(0.6); // Set opacity
                var previousPeriodColor = previousPeriodColorTc.toRgbString();
                // We must lookup legend titles, using viewing period.
                var seriesName1 = curSeries.title;
                var seriesName2 = curSeries.title;
                var viewPeriod = this.gadget.getViewPeriod(handler);
                if (viewPeriod && viewPeriod.Formulas) {
                    seriesName1 = this.gadget.seriesMap["Series1"].title;
                    seriesName2 = this.gadget.seriesMap["Series2"].title;
                }
                var defaultAxisType = handler.byName(Settings.Gadget.LeftAxisType.name);
                // Setup the "previous" series (i.e. last month)
                var jqSeries2 = Dashboards.Views.ChartHelper.SetupChartSeries(this, curSeries, curSeries.seriesType ? curSeries.seriesType : defaultAxisType, previousPeriodColor);
                jqSeries2.title = seriesName2;
                jqSeries2.axisY = 'y1';
                jqSeries2.data = dataPoints['Series2'];
                chartSeries.push(jqSeries2);
                // Setup the "current" series (i.e. this month)
                var jqSeries1 = Dashboards.Views.ChartHelper.SetupChartSeries(this, curSeries, curSeries.seriesType ? curSeries.seriesType : defaultAxisType, currentPeriodColor);
                jqSeries1.title = seriesName1;
                jqSeries1.axisY = 'y1';
                jqSeries1.data = dataPoints['Series1'];
                chartSeries.push(jqSeries1);
                chartOptions.series = chartSeries;
                var existingSeries = this.$getChart().jqChart('option', 'series');
                Views.ChartHelper.UpdateSeriesVisibility(chartSeries, existingSeries);
                // Create the chart
                this.destroyChart(); // Destroy any previous chart instance
                Views.ChartHelper.BindChartAxisLabelFormat(this, ['left']);
                this.$getChart().jqChart(chartOptions);
                Views.ChartHelper.BindTimeValueTitledTooltipFormat(this, this.gadget, handler, curSeries.title);
                Views.ChartHelper.ShowMarkerForNoDataPlotted(this);
            };
            return PeriodOverPeriodRender;
        }(Views.ViewPeriodChartRender));
        Views.PeriodOverPeriodRender = PeriodOverPeriodRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PeriodOverPeriodGadget.TypeId,
    create: Dashboards.Models.PeriodOverPeriodGadget.CreateInstance,
    resourceSet: 'Gadgets'
});
//# sourceMappingURL=PeriodOverPeriod.js.map