"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.buildSizeArray = exports.bounds = exports.bandKey = exports.amountToIncreaseWidth = void 0;
exports.buildTickModel = buildTickModel;
exports.getDelta = exports.findLongestWord = void 0;
exports.getInterval = getInterval;
exports.polygonToArea = exports.point = exports.lineToArea = exports.isDomainRangeEqual = void 0;
exports.snapTo = snapTo;
exports.xy = exports.tickCount = void 0;

var _invariant = _interopRequireDefault(require("invariant"));

var _range = _interopRequireDefault(require("lodash/range"));

var _pointGeometry = _interopRequireDefault(require("@mapbox/point-geometry"));

var _head = _interopRequireDefault(require("lodash/head"));

var _tail = _interopRequireDefault(require("lodash/tail"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var xy = function xy(x, y) {
  return {
    x: x,
    y: y
  };
};

exports.xy = xy;

var buildSizeArray = function buildSizeArray(size, padding) {
  padding = Number.isFinite(padding) ? Math.max(padding, 0) : 0;
  (0, _invariant["default"])(padding < size, 'padding must be less than size');
  var half = Math.round(padding * 0.5);
  return [half, size - half];
};

exports.buildSizeArray = buildSizeArray;

var tickCount = function tickCount(min, max, step) {
  (0, _invariant["default"])(min < max, 'min must be less than max');
  var size = Math.abs(min - max);
  return Math.round(size / step);
};

exports.tickCount = tickCount;

function getInterval(domain, ticks) {
  var min = domain.min,
      max = domain.max;
  var major = ticks.major,
      minor = ticks.minor;

  if (min >= max) {
    throw new Error("min is > max: ".concat(min, " > ").concat(max));
  }

  var distance = max - min;
  var minorTicks = minor > 0 ? minor + 1 : 1;
  var normalizedMajor = major - 1;

  if (isNaN(normalizedMajor)) {
    throw new Error('Tick Frequency must be 2 or higher');
  }

  if (normalizedMajor <= 0) {
    throw new Error('Tick Frequency must be 2 or higher');
  }

  var divider = normalizedMajor * minorTicks;
  var raw = distance / divider;
  return parseFloat(Number(raw).toFixed(4));
}

var mkRange = function mkRange(min, max, interval) {
  var raw = (0, _range["default"])(min, max, interval);
  /* Fix the last step due to rounding errors */

  raw.splice(raw.length, 1, max);
  return raw;
};

function snapTo(min, max, interval, value) {
  if (value >= max) {
    return max;
  }

  if (value <= min) {
    return min;
  }

  var rng = mkRange(min, max, interval);
  rng = rng.filter(function (v) {
    return Math.abs(value - v) <= interval;
  });
  return rng.length && rng.reduce(function (prev, curr) {
    var currentDistance = Math.abs(curr - value);
    var previousDistance = Math.abs(prev - value);
    return currentDistance <= previousDistance ? curr : prev;
  });
}

function buildTickModel(domain, ticks, interval, scaleFn) {
  var rng = mkRange(domain.min, domain.max, interval);
  return rng.map(function (r, index) {
    var isMajor = index % (ticks.minor + 1) === 0;
    return {
      value: r,
      major: isMajor,
      x: scaleFn(r)
    };
  });
}

var polygonToArea = function polygonToArea(points) {
  var h = (0, _head["default"])(points);
  var area = {
    left: h.x,
    top: h.y,
    bottom: h.y,
    right: h.x
  };
  return (0, _tail["default"])(points).reduce(function (a, p) {
    a.left = Math.min(a.left, p.x);
    a.top = Math.max(a.top, p.y);
    a.bottom = Math.min(a.bottom, p.y);
    a.right = Math.max(a.right, p.x);
    return a;
  }, area);
};

exports.polygonToArea = polygonToArea;

var lineToArea = function lineToArea(from, to) {
  var left = Math.min(from.x, to.x);
  var top = Math.max(from.y, to.y);
  var bottom = Math.min(from.y, to.y);
  var right = Math.max(from.x, to.x);
  return {
    left: left,
    top: top,
    bottom: bottom,
    right: right
  };
};

exports.lineToArea = lineToArea;

var bounds = function bounds(area, domain, range) {
  return {
    left: domain.min - area.left,
    right: Math.abs(area.right - domain.max),
    top: Math.abs(area.top - range.max),
    bottom: range.min - area.bottom
  };
};

exports.bounds = bounds;

var point = function point(o) {
  return new _pointGeometry["default"](o.x, o.y);
};

exports.point = point;

var getDelta = function getDelta(from, to) {
  return point(to).sub(point(from));
};

exports.getDelta = getDelta;

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

exports.bandKey = bandKey;

var isDomainRangeEqual = function isDomainRangeEqual(graphProps, nextGraphProps) {
  return (0, _isEqual["default"])(graphProps.domain, nextGraphProps.domain) && (0, _isEqual["default"])(graphProps.range, nextGraphProps.range);
}; // findLongestWord is also used in graphing


exports.isDomainRangeEqual = isDomainRangeEqual;

var findLongestWord = function findLongestWord(label) {
  var longestWord = (label || '').replace(/<[^>]+>/g, '').split(' ').sort(function (a, b) {
    return b.length - a.length;
  });
  return longestWord[0].length;
}; // amountToIncreaseWidth is also used in graphing


exports.findLongestWord = findLongestWord;

var amountToIncreaseWidth = function amountToIncreaseWidth(longestWord) {
  if (!longestWord) {
    return 0;
  }

  return longestWord * 20;
};

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