import * as tslib_1 from "tslib";
import { Input } from '@angular/core';
import * as _ from 'lodash';
import { fromEvent } from 'rxjs/observable/fromEvent';
import { CurrencyPipe } from '@angular/common';
import * as moment from 'moment';
/**
 * @internal
 */
export function formatScale(val, currency) {
    var n = Number(val);
    if (n === 0) {
        return '0';
    }
    var base = '';
    if (n >= 1000) {
        n = n / 1000;
        base = 'K';
    }
    if (n >= 1000) {
        n = n / 1000;
        base = 'M';
    }
    return (formatMoney(n, currency, undefined) || '').replace(/\.0+?$/, '') + base;
}
/**
 * @internal
 */
export function formatMoney(val, currency, digitInfo) {
    if (val && currency) {
        var pipe = new CurrencyPipe(moment.locale());
        return pipe.transform(val, currency, 'symbol-narrow', digitInfo);
    }
}
var BaseChart = /** @class */ (function () {
    function BaseChart(_zone) {
        var _this = this;
        this._zone = _zone;
        this._params = {};
        this._prevParams = {};
        this._wndEvSubs = fromEvent(window, 'resize').subscribe(function () {
            _this._resized = true;
        });
    }
    Object.defineProperty(BaseChart.prototype, "labels", {
        get: function () {
            return this._params.labels;
        },
        /**
         * An array of strings, corresponding to Chart.ChartConfiguration.data.labels
         * @property
         */
        set: function (l) {
            this._params.labels = l;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "type", {
        get: function () {
            return this._params.type;
        },
        /**
         * The chart type, corresponding to Chart.ChartConfiguration.type
         * @default 'bar'
         * @property
         */
        set: function (t) {
            this._params.type = t;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "datasets", {
        get: function () {
            return this._params.datasets;
        },
        /**
         * An array of Chart.ChartDataSets, corresponding to Chart.ChartConfiguration.data.datasets
         * @property
         */
        set: function (ds) {
            this._params.datasets = ds;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "colors", {
        get: function () {
            return this._params.colors;
        },
        /**
         * An array of strings in hex, rgb, or rgba format, to define the base colors of datasets, or the data points of each dataset. If this
         * property is absent, or the colors defined are less than the number of datasets (or the data points of each dataset), the missing colors will
         * be assigned from the defult palette
         * @property
         */
        set: function (c) {
            this._params.colors = c;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "colorsFor", {
        get: function () {
            return this._params.colorsFor;
        },
        /**
         * Specify what the colors (either defined or generated) are for.
         *  * 'series' - each color corresponds to a dataset
         *  * 'data' - each color corresponds to a data point of each dataset
         *  * 'auto' - use 'series' for chart types 'bar', 'horizontalBar' and 'line'; and use 'data' for other chart types
         *  * 'none' - turn off the color generator. Use this value if colors are specified via #options
         * @default 'auto'
         * @property
         */
        set: function (cf) {
            this._params.colorsFor = cf;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "ratio", {
        get: function () {
            return this._params.ratio;
        },
        /**
         * The aspect ratio of the chart
         * @default 2
         * @property
         */
        set: function (r) {
            this._params.ratio = r;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "legend", {
        get: function () {
            return this._params.legend;
        },
        /**
         * Specifiy how to display the legend
         *  * if the value is a boolean, it corresponds to Chart.ChartConfiguration.options.legend.display
         *  * if the value is 'auto', the legend will be arranged automatically. For example, the legend will be hidden if the chart area is too small
         *  * if the value is of type Chart.ChartLegendOptions, it corresponds to Chart.ChartConfiguration.options.legend.position
         *  * if the value is of type Chart.ChartLegendOptions, it corresponds to Chart.ChartConfiguration.options.legend
         * @default 'auto'
         * @property
         */
        set: function (l) {
            this._params.legend = l;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "currency", {
        get: function () {
            return this._params.currency;
        },
        /**
         * An ISO 4217 currency code. If specified, it will be used to format the scales of the main axis, as well as the values in the tooltips.
         * @property
         */
        set: function (curr) {
            this._params.currency = curr;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "digits", {
        get: function () {
            return this._params.digits;
        },
        /**
         * The digit info of the output template, used to format numbers in scales. Please refer to https://angular.io/api/common/DecimalPipe for its usage.
         * @property
         */
        set: function (d) {
            this._params.digits = d;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "options", {
        get: function () {
            return this._params.options;
        },
        /**
         * Corresponds to Chart.ChartConfiguration.options. This overrides any other settings.
         * @property
         */
        set: function (opt) {
            this._params.options = opt;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "timeFormat", {
        get: function () {
            return this._params.timeFormat;
        },
        /**
         * A moment.js format string. When specified, the X axis will be configured with time scales, and this value will be used to format the tooltips.
         * @property
         */
        set: function (tf) {
            this._params.timeFormat = tf;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "percentage", {
        get: function () {
            return this._params.percentage;
        },
        /**
         * Specify whether to show the overall percentage values in the popover tooltips.
         * @property
         */
        set: function (p) {
            this._params.percentage = p;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BaseChart.prototype, "paramsChanged", {
        get: function () {
            return !_.isEqual(this._params, this._prevParams);
        },
        enumerable: true,
        configurable: true
    });
    BaseChart.prototype.isParamChanged = function (property) {
        return !_.isEqual(_.get(this._params, property), _.get(this._prevParams, property));
    };
    BaseChart.prototype.resetParamsChangeState = function () {
        this._prevParams = _.cloneDeep(this._params);
    };
    BaseChart.prototype.ngDoCheck = function () {
        var _this = this;
        if (this._debounceTimer) {
            clearTimeout(this._debounceTimer);
        }
        this._zone.runOutsideAngular(function () {
            _this._debounceTimer = setTimeout(function () {
                _this._checkUpdate(_this._resized);
                _this._resized = false;
            }, 50);
        });
    };
    BaseChart.prototype.ngOnDestroy = function () {
        this._wndEvSubs.unsubscribe();
        this._onDestroy();
    };
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Array),
        tslib_1.__metadata("design:paramtypes", [Array])
    ], BaseChart.prototype, "labels", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String),
        tslib_1.__metadata("design:paramtypes", [String])
    ], BaseChart.prototype, "type", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object),
        tslib_1.__metadata("design:paramtypes", [Object])
    ], BaseChart.prototype, "datasets", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Array),
        tslib_1.__metadata("design:paramtypes", [Array])
    ], BaseChart.prototype, "colors", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String),
        tslib_1.__metadata("design:paramtypes", [String])
    ], BaseChart.prototype, "colorsFor", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Number),
        tslib_1.__metadata("design:paramtypes", [Number])
    ], BaseChart.prototype, "ratio", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object),
        tslib_1.__metadata("design:paramtypes", [Object])
    ], BaseChart.prototype, "legend", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String),
        tslib_1.__metadata("design:paramtypes", [String])
    ], BaseChart.prototype, "currency", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String),
        tslib_1.__metadata("design:paramtypes", [String])
    ], BaseChart.prototype, "digits", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object),
        tslib_1.__metadata("design:paramtypes", [Object])
    ], BaseChart.prototype, "options", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String),
        tslib_1.__metadata("design:paramtypes", [String])
    ], BaseChart.prototype, "timeFormat", null);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object),
        tslib_1.__metadata("design:paramtypes", [Object])
    ], BaseChart.prototype, "percentage", null);
    return BaseChart;
}());
export { BaseChart };
//# sourceMappingURL=base.chart.js.map