"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const number = require("./dist-es-utils-mix-number.cjs");
const color = require("./dist-es-utils-mix-color.cjs");
const pipe = require("./dist-es-utils-pipe.cjs");
const errors = require("../motion-utils/dist-es-errors.cjs");
const index = require("./dist-es-value-types-color-index.cjs");
const index$1 = require("./dist-es-value-types-complex-index.cjs");
const isCssVariable = require("./dist-es-render-dom-utils-is-css-variable.cjs");
const visibility = require("./dist-es-utils-mix-visibility.cjs");
const immediate = require("./dist-es-utils-mix-immediate.cjs");
function mixNumber(a, b) {
  return (p) => number.mixNumber(a, b, p);
}
function getMixer(a) {
  if (typeof a === "number") {
    return mixNumber;
  } else if (typeof a === "string") {
    return isCssVariable.isCSSVariableToken(a) ? immediate.mixImmediate : index.color.test(a) ? color.mixColor : mixComplex;
  } else if (Array.isArray(a)) {
    return mixArray;
  } else if (typeof a === "object") {
    return index.color.test(a) ? color.mixColor : mixObject;
  }
  return immediate.mixImmediate;
}
function mixArray(a, b) {
  const output = [...a];
  const numValues = output.length;
  const blendValue = a.map((v, i) => getMixer(v)(v, b[i]));
  return (p) => {
    for (let i = 0; i < numValues; i++) {
      output[i] = blendValue[i](p);
    }
    return output;
  };
}
function mixObject(a, b) {
  const output = { ...a, ...b };
  const blendValue = {};
  for (const key in output) {
    if (a[key] !== void 0 && b[key] !== void 0) {
      blendValue[key] = getMixer(a[key])(a[key], b[key]);
    }
  }
  return (v) => {
    for (const key in blendValue) {
      output[key] = blendValue[key](v);
    }
    return output;
  };
}
function matchOrder(origin, target) {
  var _a;
  const orderedOrigin = [];
  const pointers = { color: 0, var: 0, number: 0 };
  for (let i = 0; i < target.values.length; i++) {
    const type = target.types[i];
    const originIndex = origin.indexes[type][pointers[type]];
    const originValue = (_a = origin.values[originIndex]) !== null && _a !== void 0 ? _a : 0;
    orderedOrigin[i] = originValue;
    pointers[type]++;
  }
  return orderedOrigin;
}
const mixComplex = (origin, target) => {
  const template = index$1.complex.createTransformer(target);
  const originStats = index$1.analyseComplexValue(origin);
  const targetStats = index$1.analyseComplexValue(target);
  const canInterpolate = originStats.indexes.var.length === targetStats.indexes.var.length && originStats.indexes.color.length === targetStats.indexes.color.length && originStats.indexes.number.length >= targetStats.indexes.number.length;
  if (canInterpolate) {
    if (visibility.invisibleValues.has(origin) && !targetStats.values.length || visibility.invisibleValues.has(target) && !originStats.values.length) {
      return visibility.mixVisibility(origin, target);
    }
    return pipe.pipe(mixArray(matchOrder(originStats, targetStats), targetStats.values), template);
  } else {
    errors.warning(true, `Complex values '${origin}' and '${target}' too different to mix. Ensure all colors are of the same type, and that each contains the same quantity of number and color values. Falling back to instant transition.`);
    return immediate.mixImmediate(origin, target);
  }
};
exports.getMixer = getMixer;
exports.mixArray = mixArray;
exports.mixComplex = mixComplex;
exports.mixObject = mixObject;
//# sourceMappingURL=dist-es-utils-mix-complex.cjs.map
