'use strict';

var isCssVariable = require('../../animation/utils/is-css-variable.js');
var index = require('../../value/types/color/index.js');
var index$1 = require('../../value/types/complex/index.js');
var color = require('./color.js');
var immediate = require('./immediate.js');
var number = require('./number.js');
var visibility = require('./visibility.js');
var pipe = require('../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/pipe.js');
var errors = require('../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/errors.js');

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] !== undefined && b[key] !== undefined) {
            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) {
    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 = origin.values[originIndex] ?? 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.`, "complex-values-different");
        return immediate.mixImmediate(origin, target);
    }
};

exports.getMixer = getMixer;
exports.mixArray = mixArray;
exports.mixComplex = mixComplex;
exports.mixObject = mixObject;
