"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.create = void 0;

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

var _utils = require("./utils");

var _d3Scale = require("d3-scale");

var createSnapMinAndMax = function createSnapMinAndMax(_ref) {
  var min = _ref.min,
      max = _ref.max,
      step = _ref.step;
  // for graphing, if step is a value with decimals, we have to calculate the min & max for the grid taking in consideration that 0 has to be exactly in the middle
  // for example, if min: -5 & max: 5 & step: 0.75, in order to keep 0 in the middle we have to set min: -4.5 & max: 4.5
  return {
    step: step,
    min: parseInt(min / step) * step,
    max: parseInt(max / step) * step
  };
};

var create = function create(domain, range, size, getRootNode) {
  (0, _invariant["default"])(domain.min < domain.max, 'domain: min must be less than max');
  (0, _invariant["default"])(range.min < range.max, 'range: min must be less than max');
  var domainMinMax = createSnapMinAndMax(domain);
  var rangeMinMax = createSnapMinAndMax(range);
  var scale = {
    x: (0, _d3Scale.scaleLinear)().domain([domain.min, domain.max]).range([0, size.width]),
    y: (0, _d3Scale.scaleLinear)().domain([range.max, range.min]).range([0, size.height])
  };
  var snap = {
    x: _utils.snapTo.bind(null, domainMinMax.min, domainMinMax.max, domainMinMax.step),
    y: _utils.snapTo.bind(null, rangeMinMax.min, rangeMinMax.max, rangeMinMax.step)
  };
  return {
    scale: scale,
    snap: snap,
    domain: domain,
    range: range,
    size: size,
    getRootNode: getRootNode
  };
};

exports.create = create;
//# sourceMappingURL=graph-props.js.map