"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const progress = require("./progress.js");
const mix = require("./mix.js");
const mixColor = require("./mix-color.js");
const mixComplex = require("./mix-complex.js");
const clamp = require("./clamp.js");
const pipe = require("./pipe.js");
const heyListen_es = require("../../../../../../hey-listen@1.0.8/node_modules/hey-listen/dist/hey-listen.es.js");
const index = require("../../../../../../style-value-types@5.0.0/node_modules/style-value-types/dist/es/color/index.js");
const mixNumber = (from, to) => (p) => mix.mix(from, to, p);
function detectMixerFactory(v) {
  if (typeof v === "number") {
    return mixNumber;
  } else if (typeof v === "string") {
    if (index.color.test(v)) {
      return mixColor.mixColor;
    } else {
      return mixComplex.mixComplex;
    }
  } else if (Array.isArray(v)) {
    return mixComplex.mixArray;
  } else if (typeof v === "object") {
    return mixComplex.mixObject;
  }
}
function createMixers(output, ease, customMixer) {
  const mixers = [];
  const mixerFactory = customMixer || detectMixerFactory(output[0]);
  const numMixers = output.length - 1;
  for (let i = 0; i < numMixers; i++) {
    let mixer = mixerFactory(output[i], output[i + 1]);
    if (ease) {
      const easingFunction = Array.isArray(ease) ? ease[i] : ease;
      mixer = pipe.pipe(easingFunction, mixer);
    }
    mixers.push(mixer);
  }
  return mixers;
}
function fastInterpolate([from, to], [mixer]) {
  return (v) => mixer(progress.progress(from, to, v));
}
function slowInterpolate(input, mixers) {
  const inputLength = input.length;
  const lastInputIndex = inputLength - 1;
  return (v) => {
    let mixerIndex = 0;
    let foundMixerIndex = false;
    if (v <= input[0]) {
      foundMixerIndex = true;
    } else if (v >= input[lastInputIndex]) {
      mixerIndex = lastInputIndex - 1;
      foundMixerIndex = true;
    }
    if (!foundMixerIndex) {
      let i = 1;
      for (; i < inputLength; i++) {
        if (input[i] > v || i === lastInputIndex) {
          break;
        }
      }
      mixerIndex = i - 1;
    }
    const progressInRange = progress.progress(input[mixerIndex], input[mixerIndex + 1], v);
    return mixers[mixerIndex](progressInRange);
  };
}
function interpolate(input, output, { clamp: isClamp = true, ease, mixer } = {}) {
  const inputLength = input.length;
  heyListen_es.invariant(inputLength === output.length, "Both input and output ranges must be the same length");
  heyListen_es.invariant(!ease || !Array.isArray(ease) || ease.length === inputLength - 1, "Array of easing functions must be of length `input.length - 1`, as it applies to the transitions **between** the defined values.");
  if (input[0] > input[inputLength - 1]) {
    input = [].concat(input);
    output = [].concat(output);
    input.reverse();
    output.reverse();
  }
  const mixers = createMixers(output, ease, mixer);
  const interpolator = inputLength === 2 ? fastInterpolate(input, mixers) : slowInterpolate(input, mixers);
  return isClamp ? (v) => interpolator(clamp.clamp(input[0], input[inputLength - 1], v)) : interpolator;
}
exports.interpolate = interpolate;
//# sourceMappingURL=interpolate.js.map
