// If `opacity` exists, we apply a `color-mix` formula to blend colors
// If `multiple` exists, we apply the `calc()` function to multiply the reference value
// Otherwise, we simply replace the reference with its corresponding variable
function processTokenValue(value, refs, multiple, opacity) {
    if (opacity) {
        return `color-mix(in srgb, var(--${refs.name}), transparent calc(100% - ${opacity}))`;
    }
    else if (multiple) {
        return value.replace(refs.value, `calc(var(--${refs.name}) * ${multiple})`);
    }
    else {
        return value.replace(refs.value, `var(--${refs.name})`);
    }
}
// Helper function to resolve references for both `multiple` and `opacity` and return the updated value
function resolveTokenReferences(token, unfilteredTokens, usesReferences, getReferences) {
    var _a, _b, _c, _d;
    let multiple = (_b = (_a = token.original) === null || _a === void 0 ? void 0 : _a.attributes) === null || _b === void 0 ? void 0 : _b.multiple;
    let opacity = (_d = (_c = token.original) === null || _c === void 0 ? void 0 : _c.attributes) === null || _d === void 0 ? void 0 : _d.opacity;
    // Resolve references in `multiple` and `opacity`
    multiple = resolveAndReplaceReferences(multiple, unfilteredTokens, usesReferences, getReferences);
    opacity = resolveAndReplaceReferences(opacity, unfilteredTokens, usesReferences, getReferences);
    return { multiple, opacity };
}
// Helper function to resolve references and return the token name
function resolveAndReplaceReferences(attribute, unfilteredTokens, usesReferences, getReferences) {
    if (attribute && usesReferences(attribute, unfilteredTokens)) {
        const refs = getReferences(attribute, unfilteredTokens);
        refs.forEach(ref => {
            // Replace the reference with the resolved token name
            attribute = `var(--${ref.name})`;
        });
    }
    return attribute;
}
const cssVarCalcMixFormatter = (usesReferences, getReferences) => ({
    name: 'wm-calc-colormix-format',
    format: function ({ dictionary, options }) {
        const { selector } = options;
        // Generate CSS variable definitions
        const variables = dictionary.allTokens
            .map((token) => {
            let value = String(token.value);
            // Checking if the token has references
            if (usesReferences(token.original.value, dictionary.unfilteredTokens)) {
                const refs = getReferences(token.original.value, dictionary.unfilteredTokens);
                refs.forEach((ref) => {
                    // Resolve references for `multiple` and `opacity`
                    const { multiple, opacity } = resolveTokenReferences(token, dictionary.unfilteredTokens, usesReferences, getReferences);
                    // Process the value based on resolved references
                    value = processTokenValue(value, ref, multiple, opacity);
                });
            }
            return `--${token.name}: ${value};`;
        })
            .join(`\n`);
        // Return correctly formatted output based on selector
        return selector && selector.trim()
            ? `${selector} {\n${variables}\n}\n` // Wrap in selector
            : `${variables}\n`; // No selector, no closing bracket
    },
});
// Export the format to use it elsewhere
export { cssVarCalcMixFormatter };
//# sourceMappingURL=style-dictionary-utils.js.map