"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.tickCount = exports.point = exports.getTopPadding = exports.getTickValues = exports.getScale = exports.getRotateAngle = exports.getGridLinesAndAxisByChartType = exports.getDomainAndRangeByChartType = exports.getAdjustedX = exports.dataToXBand = exports.bounds = exports.bandKey = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _scale = require("@vx/scale");

var _plot = require("../plot");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var tickCount = _plot.utils.tickCount;
exports.tickCount = tickCount;
var bounds = _plot.utils.bounds;
exports.bounds = bounds;
var point = _plot.utils.point;
exports.point = point;

var bandKey = function bandKey(d, index) {
  return "".concat(index, "-").concat(d.label || '-');
};

exports.bandKey = bandKey;

var dataToXBand = function dataToXBand(scaleX, data, width, type) {
  switch (type) {
    case 'bar':
    case 'dotPlot':
    case 'linePlot':
      return (0, _scale.scaleBand)({
        rangeRound: [0, width],
        domain: data && data.map(bandKey),
        padding: 0.2
      });

    case 'histogram':
      return (0, _scale.scaleBand)({
        rangeRound: [0, width],
        domain: data && data.map(bandKey),
        padding: 0
      });

    case 'lineCross':
    case 'lineDot':
      return (0, _scale.scalePoint)({
        domain: data && data.map(bandKey),
        rangeRound: [0, width]
      });

    default:
      return (0, _scale.scaleBand)({
        range: [0, width],
        domain: data && data.map(bandKey),
        padding: 0
      });
  }
};

exports.dataToXBand = dataToXBand;

var getTickValues = function getTickValues() {
  var prop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var tickValues = [];
  var tickVal = prop.min;

  while (tickVal <= prop.max) {
    tickValues.push(tickVal);
    tickVal = Math.round((tickVal + prop.step) * 10000) / 10000;
  }

  return tickValues;
};

exports.getTickValues = getTickValues;

var getDomainAndRangeByChartType = function getDomainAndRangeByChartType(domain, range, chartType) {
  var _ref = range || {},
      step = _ref.step,
      labelStep = _ref.labelStep,
      min = _ref.min,
      max = _ref.max;

  if (!min) {
    min = 0;
  }

  if (!max || max < 0) {
    max = range.min + 1;
  }

  if (!step) {
    step = labelStep || 1;
  }

  if (!labelStep || isNaN(labelStep) && step) {
    labelStep = step || 1;
  }

  range.max = max;

  switch (chartType) {
    // if chart is dot plot or line plot, we should ignore step and make sure that min & max are integer values
    case 'dotPlot':
    case 'linePlot':
      {
        var intMin = Math.round(min);
        var intMax = Math.round(max);
        return {
          domain: _objectSpread(_objectSpread({}, domain), {}, {
            step: 1,
            labelStep: 1,
            min: 0,
            max: 1
          }),
          range: _objectSpread(_objectSpread({}, range), {}, {
            min: intMin,
            max: intMin === intMax ? intMin + 1 : intMax,
            labelStep: labelStep,
            step: 1
          })
        };
      }

    default:
      return {
        domain: _objectSpread(_objectSpread({}, domain), {}, {
          step: 1,
          labelStep: 1,
          min: 0,
          max: 1
        }),
        range: _objectSpread(_objectSpread({}, range), {}, {
          labelStep: labelStep,
          step: step
        })
      };
  }
};

exports.getDomainAndRangeByChartType = getDomainAndRangeByChartType;

var getGridLinesAndAxisByChartType = function getGridLinesAndAxisByChartType(range, chartType) {
  switch (chartType) {
    case 'lineDot':
    case 'lineCross':
      return {
        verticalLines: undefined,
        horizontalLines: getTickValues(range),
        leftAxis: true
      };

    case 'dotPlot':
    case 'linePlot':
      return {
        verticalLines: [],
        horizontalLines: [],
        leftAxis: false
      };

    default:
      return {
        verticalLines: [],
        horizontalLines: getTickValues(range),
        leftAxis: true
      };
  }
};

exports.getGridLinesAndAxisByChartType = getGridLinesAndAxisByChartType;

var getRotateAngle = function getRotateAngle(fontSize, height) {
  if (height >= fontSize * 2) {
    return 25;
  }

  return 0;
};

exports.getRotateAngle = getRotateAngle;

var getTopPadding = function getTopPadding(barWidth) {
  if (barWidth < 30) {
    return 50;
  }

  if (barWidth < 40) {
    return 30;
  }

  if (barWidth < 60) {
    return 15;
  }

  return 0;
}; // This function calculates the transformation scale for SVG and the icon's vertical distance from its category


exports.getTopPadding = getTopPadding;

var getScale = function getScale(width) {
  var scale, deltay;

  if (width > 91) {
    scale = 1.3;
    deltay = -55;
  } else if (width > 45) {
    scale = 1.1;
    deltay = -45;
  } else if (width > 40) {
    scale = 0.5 + (width - 34) * 0.02;
    deltay = -25;
  } else if (width > 30) {
    scale = 0.5 + (width - 34) * 0.02;
    deltay = -20;
  } else {
    scale = 0.5 * Math.pow(0.98, 34 - width); // 0.98 is the reduction factor. Adjust to control scaling.

    deltay = -15;
  }

  return {
    scale: scale,
    deltay: deltay
  };
};

exports.getScale = getScale;

var getAdjustedX = function getAdjustedX(width, scaleValue) {
  var innerWidthOriginal = 57;
  var effectiveInnerWidth = innerWidthOriginal * scaleValue;
  return (width - effectiveInnerWidth) / 2;
};

exports.getAdjustedX = getAdjustedX;
//# sourceMappingURL=utils.js.map