"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const tslib_es6 = require("../../../../../../../../tslib@2.6.2/node_modules/tslib/tslib.es6.js");
const deltaCalc = require("../../../projection/geometry/delta-calc.js");
const mix = require("../../../../../../../../popmotion@11.0.3/node_modules/popmotion/dist/es/utils/mix.js");
const progress = require("../../../../../../../../popmotion@11.0.3/node_modules/popmotion/dist/es/utils/progress.js");
const clamp = require("../../../../../../../../popmotion@11.0.3/node_modules/popmotion/dist/es/utils/clamp.js");
function applyConstraints(point, _a, elastic) {
  var min = _a.min, max = _a.max;
  if (min !== void 0 && point < min) {
    point = elastic ? mix.mix(min, point, elastic.min) : Math.max(point, min);
  } else if (max !== void 0 && point > max) {
    point = elastic ? mix.mix(max, point, elastic.max) : Math.min(point, max);
  }
  return point;
}
function calcRelativeAxisConstraints(axis, min, max) {
  return {
    min: min !== void 0 ? axis.min + min : void 0,
    max: max !== void 0 ? axis.max + max - (axis.max - axis.min) : void 0
  };
}
function calcRelativeConstraints(layoutBox, _a) {
  var top = _a.top, left = _a.left, bottom = _a.bottom, right = _a.right;
  return {
    x: calcRelativeAxisConstraints(layoutBox.x, left, right),
    y: calcRelativeAxisConstraints(layoutBox.y, top, bottom)
  };
}
function calcViewportAxisConstraints(layoutAxis, constraintsAxis) {
  var _a;
  var min = constraintsAxis.min - layoutAxis.min;
  var max = constraintsAxis.max - layoutAxis.max;
  if (constraintsAxis.max - constraintsAxis.min < layoutAxis.max - layoutAxis.min) {
    _a = tslib_es6.__read([max, min], 2), min = _a[0], max = _a[1];
  }
  return { min, max };
}
function calcViewportConstraints(layoutBox, constraintsBox) {
  return {
    x: calcViewportAxisConstraints(layoutBox.x, constraintsBox.x),
    y: calcViewportAxisConstraints(layoutBox.y, constraintsBox.y)
  };
}
function calcOrigin(source, target) {
  var origin = 0.5;
  var sourceLength = deltaCalc.calcLength(source);
  var targetLength = deltaCalc.calcLength(target);
  if (targetLength > sourceLength) {
    origin = progress.progress(target.min, target.max - sourceLength, source.min);
  } else if (sourceLength > targetLength) {
    origin = progress.progress(source.min, source.max - targetLength, target.min);
  }
  return clamp.clamp(0, 1, origin);
}
function rebaseAxisConstraints(layout, constraints) {
  var relativeConstraints = {};
  if (constraints.min !== void 0) {
    relativeConstraints.min = constraints.min - layout.min;
  }
  if (constraints.max !== void 0) {
    relativeConstraints.max = constraints.max - layout.min;
  }
  return relativeConstraints;
}
var defaultElastic = 0.35;
function resolveDragElastic(dragElastic) {
  if (dragElastic === void 0) {
    dragElastic = defaultElastic;
  }
  if (dragElastic === false) {
    dragElastic = 0;
  } else if (dragElastic === true) {
    dragElastic = defaultElastic;
  }
  return {
    x: resolveAxisElastic(dragElastic, "left", "right"),
    y: resolveAxisElastic(dragElastic, "top", "bottom")
  };
}
function resolveAxisElastic(dragElastic, minLabel, maxLabel) {
  return {
    min: resolvePointElastic(dragElastic, minLabel),
    max: resolvePointElastic(dragElastic, maxLabel)
  };
}
function resolvePointElastic(dragElastic, label) {
  var _a;
  return typeof dragElastic === "number" ? dragElastic : (_a = dragElastic[label]) !== null && _a !== void 0 ? _a : 0;
}
exports.applyConstraints = applyConstraints;
exports.calcOrigin = calcOrigin;
exports.calcRelativeAxisConstraints = calcRelativeAxisConstraints;
exports.calcRelativeConstraints = calcRelativeConstraints;
exports.calcViewportAxisConstraints = calcViewportAxisConstraints;
exports.calcViewportConstraints = calcViewportConstraints;
exports.defaultElastic = defaultElastic;
exports.rebaseAxisConstraints = rebaseAxisConstraints;
exports.resolveAxisElastic = resolveAxisElastic;
exports.resolveDragElastic = resolveDragElastic;
exports.resolvePointElastic = resolvePointElastic;
//# sourceMappingURL=constraints.js.map
