import { Scale, LinearScale } from 'chart.js';
import { scaleSymlog } from 'd3-scale';

class SymlogAxis extends Scale {
  constructor() {
    super(...arguments);
    this._scale = scaleSymlog();
  }
  parse(raw, index) {
    return LinearScale.prototype.parse.apply(this, [raw, index]);
  }
  determineDataLimits() {
    const { min, max } = this.getMinMax(true);
    this.min = isFinite(min) ? min : 0;
    this.max = isFinite(max) ? max : 1;
  }
  buildTicks() {
    this._scale.domain([this.min, this.max]);
    const ticks = this._scale.ticks().map((value) => ({ value }));
    if (this.min < 1) {
      ticks.unshift({ value: 0 });
    }
    return ticks;
  }
  /**
   * @protected
   */
  configure() {
    super.configure();
    this._scale.domain([this.min, this.max]);
  }
  getPixelForValue(value) {
    return this.getPixelForDecimal(this._scale(value ?? this.min));
  }
  getValueForPixel(pixel) {
    return this._scale.invert(this.getDecimalForPixel(pixel));
  }
}
SymlogAxis.id = "symlog";
SymlogAxis.defaults = {};

export { SymlogAxis as default };
