
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/


/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { __extends } from "tslib";
import * as zrUtil from 'tvrender/lib/core/util.js';
import Scale from './Scale.js';
import * as numberUtil from '../util/number.js';
import * as scaleHelper from './helper.js';
// Use some method of IntervalScale
import IntervalScale from './Interval.js';
var scaleProto = Scale.prototype;
// FIXME:TS refactor: not good to call it directly with `this`?
var intervalScaleProto = IntervalScale.prototype;
var roundingErrorFix = numberUtil.round;
var PercentageScale = /** @class */function (_super) {
  __extends(PercentageScale, _super);
  function PercentageScale(setting) {
    var _this = _super.call(this, setting) || this;
    _this.type = 'percentage';
    // base = 10;
    _this._originalScale = new IntervalScale();
    // FIXME:TS actually used by `IntervalScale`
    _this._interval = 0;
    _this._baseDim = _this.getSetting('baseDim') || 'close';
    return _this;
  }
  /**
   * @param Whether expand the ticks to niced extent.
   */
  PercentageScale.prototype.getTicks = function (expandToNicedExtent) {
    var originalScale = this._originalScale;
    var extent = this._extent;
    var originalExtent = originalScale.getExtent();
    var ticks = intervalScaleProto.getTicks.call(this, expandToNicedExtent);
    var labels = zrUtil.map(ticks, function (tick) {
      var val = tick.value;
      // 这里转义为百分比也没关系格式化时又会转回来
      var powVal = numberUtil.round(this.restorePercentageValue(val, this._fromValue));
      // Fix #4158
      powVal = val === extent[0] && this._fixMin ? fixRoundingError(powVal, originalExtent[0]) : powVal;
      powVal = val === extent[1] && this._fixMax ? fixRoundingError(powVal, originalExtent[1]) : powVal;
      return {
        value: powVal
      };
    }, this);
    return labels;
  };
  PercentageScale.prototype.getFormattedLabel = function (tick, baseValue
  // labelFormatter: (label: string) => string
  ) {
    var value = tick.value;
    var percent = this.parsePercentageValue(value, this._fromValue);
    return percent.toFixed(2) + '%';
  };
  PercentageScale.prototype.setExtent = function (start, end) {
    // start = this.parsePercentageValue(start, this._fromValue);
    // end = this.parsePercentageValue(end, this._fromValue);
    intervalScaleProto.setExtent.call(this, start, end);
    // scaleProto.setExtent.call(this, start, end);
  };

  PercentageScale.prototype.parsePercentageValue = function (value, baseValue) {
    var fromValue = baseValue !== void 0 ? +baseValue : this._fromValue;
    var result = (value - fromValue) / fromValue * 100;
    return fromValue < 0 ? -result : result;
  };
  PercentageScale.prototype.restorePercentageValue = function (value, baseValue) {
    var fromValue = baseValue !== void 0 ? +baseValue : this._fromValue;
    if (fromValue < 0) {
      value = -value;
    }
    return value / 100 * fromValue + fromValue;
  };
  /**
   * @return {number} end
   */
  PercentageScale.prototype.getExtent = function () {
    // const base = this.base;
    var extent = scaleProto.getExtent.call(this);
    // extent[0] = this.restorePercentageValue(extent[0], this._fromValue);
    // extent[1] = this.restorePercentageValue(extent[1], this._fromValue);
    // Fix #4158
    var originalScale = this._originalScale;
    var originalExtent = originalScale.getExtent();
    this._fixMin && (extent[0] = fixRoundingError(extent[0], originalExtent[0]));
    this._fixMax && (extent[1] = fixRoundingError(extent[1], originalExtent[1]));
    return extent;
  };
  PercentageScale.prototype.unionExtent = function (extent, baseValue) {
    extent[0] = this.parsePercentageValue(extent[0], baseValue);
    extent[1] = this.parsePercentageValue(extent[1], baseValue);
    scaleProto.unionExtent.call(this, extent, baseValue);
    this._originalScale.unionExtent(extent);
  };
  PercentageScale.prototype.unionExtentFromData = function (data, dim, baseValue) {
    var _a;
    if (dim === this._baseDim || !this._fromValue) {
      this._fromValue = (_a = data.get(this._baseDim, 0)) !== null && _a !== void 0 ? _a : +baseValue;
    }
    // TODO
    // filter value that <= 0
    this.unionExtent(data.getApproximateExtent(dim), baseValue);
  };
  /**
   * Update interval and extent of intervals for nice ticks
   * @param approxTickNum default 10 Given approx tick number
   */
  // calcNiceTicks(approxTickNum: number): void {
  //     approxTickNum = approxTickNum || 10;
  //     const extent = this._extent;
  //     const span = extent[1] - extent[0];
  //     if (span === Infinity || span <= 0) {
  //         return;
  //     }
  //     let interval = numberUtil.quantity(span);
  //     const err = approxTickNum / span * interval;
  //     console.log('%c [ err ]-168', 'font-size:13px; background:pink; color:#bf2c9f;', err, span)
  //     // Filter ticks to get closer to the desired count.
  //     if (err <= 0.5) {
  //         interval *= 10;
  //     }
  //     // Interval should be integer
  //     while (!isNaN(interval) && Math.abs(interval) < 1 && Math.abs(interval) > 0) {
  //         interval *= 10;
  //     }
  //     const niceExtent = [
  //         numberUtil.round(mathCeil(extent[0] / interval) * interval),
  //         numberUtil.round(mathFloor(extent[1] / interval) * interval)
  //     ] as [number, number];
  //     this._interval = interval;
  //     this._niceExtent = niceExtent;
  // }
  PercentageScale.prototype.calcNiceExtent = function (opt) {
    intervalScaleProto.calcNiceExtent.call(this, opt);
    this._fixMin = opt.fixMin;
    this._fixMax = opt.fixMax;
  };
  PercentageScale.prototype.parse = function (val) {
    return val;
  };
  PercentageScale.prototype.contain = function (val, baseValue) {
    // const originalScale = this._originalScale;
    // const originalExtent = originalScale.getExtent();
    // return scaleHelper.contain(val, originalExtent);
    val = this.parsePercentageValue(val, baseValue);
    return scaleHelper.contain(val, this._extent);
  };
  PercentageScale.prototype.normalize = function (val, baseValue) {
    // const originalScale = this._originalScale;
    // const originalExtent = originalScale.getExtent();
    // return scaleHelper.normalize(val, originalExtent);
    val = this.parsePercentageValue(val, baseValue);
    return scaleHelper.normalize(val, this._extent);
  };
  PercentageScale.prototype.scale = function (val, baseValue) {
    // const originalScale = this._originalScale;
    // const originalExtent = originalScale.getExtent();
    // return scaleHelper.scale(val, originalExtent);
    val = scaleHelper.scale(val, this._extent);
    return this.restorePercentageValue(val, baseValue);
  };
  PercentageScale.prototype.getLabel = function (data, _a) {
    var _b = _a === void 0 ? {} : _a,
      baseValue = _b.baseValue;
    if (Number.isNaN(data.value)) {
      return '';
    }
    var value = data.value;
    var percent = this.parsePercentageValue(value, baseValue !== null && baseValue !== void 0 ? baseValue : this._fromValue);
    return percent.toFixed(2) + '%';
    // return this.getFormattedLabel(data, baseValue);
  };

  ;
  PercentageScale.prototype.getIsBaseTick = function (val) {
    var value = this.parsePercentageValue(val, this._fromValue);
    return Object.is(value, 0);
  };
  PercentageScale.type = 'percentage';
  return PercentageScale;
}(Scale);
var proto = PercentageScale.prototype;
proto.getMinorTicks = intervalScaleProto.getMinorTicks;
// proto.getLabel = intervalScaleProto.getLabel;
proto.calcNiceTicks = intervalScaleProto.calcNiceTicks;
function fixRoundingError(val, originalVal) {
  return roundingErrorFix(val, numberUtil.getPrecision(originalVal));
}
Scale.registerClass(PercentageScale);
export default PercentageScale;