function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

/**
 * @fileOverview The measurement of linear data scale function
 * @author dxq613@gmail.com
 */
var fecha = require('fecha');

var each = require('@antv/util/lib/each');

var isNil = require('@antv/util/lib/type/is-nil');

var isString = require('@antv/util/lib/type/is-string');

var Base = require('./base');

var Linear = require('./linear');

var timeAuto = require('./auto/time');

var TimeUtil = require('./time-util');
/**
 * 时间度量的构造函数
 * @class Scale.Time
 */


var Time =
/*#__PURE__*/
function (_Linear) {
  _inheritsLoose(Time, _Linear);

  function Time() {
    return _Linear.apply(this, arguments) || this;
  }

  var _proto = Time.prototype;

  _proto._initDefaultCfg = function _initDefaultCfg() {
    _Linear.prototype._initDefaultCfg.call(this);

    this.type = 'time';
    this.mask = 'YYYY-MM-DD';
  }
  /**
   * @override
   */
  ;

  _proto.init = function init() {
    var self = this;
    var values = self.values;

    if (values && values.length) {
      // 重新计算最大最小值
      var timeStamps = [];
      var min = Infinity; // 最小值

      var secondMin = min; // 次小值

      var max = 0; // 使用一个循环，计算min,max,secondMin

      each(values, function (v) {
        var timeStamp = self._toTimeStamp(v);

        if (isNaN(timeStamp)) {
          throw new TypeError("Invalid Time: " + v);
        }

        if (min > timeStamp) {
          secondMin = min;
          min = timeStamp;
        } else if (secondMin > timeStamp) {
          secondMin = timeStamp;
        }

        if (max < timeStamp) {
          max = timeStamp;
        }

        timeStamps.push(timeStamp);
      }); // 存在多个值时，设置最小间距

      if (values.length > 1) {
        self.minTickInterval = secondMin - min;
      }

      if (isNil(self.min) || self._toTimeStamp(self.min) > min) {
        self.min = min;
      }

      if (isNil(self.max) || self._toTimeStamp(self.max) < max) {
        self.max = max;
      }
    }

    _Linear.prototype.init.call(this);
  };

  _proto.calculateTicks = function calculateTicks() {
    var self = this;
    var min = self.min;
    var max = self.max;
    var count = self.tickCount;
    var interval = self.tickInterval;
    var tmp = timeAuto({
      min: min,
      max: max,
      minCount: count,
      maxCount: count,
      interval: interval,
      minInterval: self.minTickInterval
    });
    return tmp.ticks;
  }
  /**
   * @override
   */
  ;

  _proto.getText = function getText(value) {
    var formatter = this.formatter;
    value = this.translate(value);
    value = formatter ? formatter(value) : fecha.format(value, this.mask);
    return value;
  }
  /**
   * @override
   */
  ;

  _proto.scale = function scale(value) {
    if (isString(value)) {
      value = this.translate(value);
    }

    return _Linear.prototype.scale.call(this, value);
  }
  /**
   * @override
   */
  ;

  _proto.translate = function translate(value) {
    return this._toTimeStamp(value);
  } // 将时间转换为时间戳
  ;

  _proto._toTimeStamp = function _toTimeStamp(value) {
    return TimeUtil.toTimeStamp(value);
  };

  return Time;
}(Linear);

Base.Time = Time;
module.exports = Time;