"use client";
'use strict';

var React = require('react');
var index = require('../../context/MotionContext/index.js');
var PresenceContext = require('../../context/PresenceContext.js');
var useConstant = require('../../utils/use-constant.js');
var resolveMotionValue = require('../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/value/utils/resolve-motion-value.js');
var isControllingVariants = require('../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/render/utils/is-controlling-variants.js');
var isAnimationControls = require('../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/render/utils/is-animation-controls.js');
var resolveVariants = require('../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/render/utils/resolve-variants.js');

function makeState({ scrapeMotionValuesFromProps, createRenderState, }, props, context, presenceContext) {
    const state = {
        latestValues: makeLatestValues(props, context, presenceContext, scrapeMotionValuesFromProps),
        renderState: createRenderState(),
    };
    return state;
}
function makeLatestValues(props, context, presenceContext, scrapeMotionValues) {
    const values = {};
    const motionValues = scrapeMotionValues(props, {});
    for (const key in motionValues) {
        values[key] = resolveMotionValue.resolveMotionValue(motionValues[key]);
    }
    let { initial, animate } = props;
    const isControllingVariants$1 = isControllingVariants.isControllingVariants(props);
    const isVariantNode$1 = isControllingVariants.isVariantNode(props);
    if (context &&
        isVariantNode$1 &&
        !isControllingVariants$1 &&
        props.inherit !== false) {
        if (initial === undefined)
            initial = context.initial;
        if (animate === undefined)
            animate = context.animate;
    }
    let isInitialAnimationBlocked = presenceContext
        ? presenceContext.initial === false
        : false;
    isInitialAnimationBlocked = isInitialAnimationBlocked || initial === false;
    const variantToSet = isInitialAnimationBlocked ? animate : initial;
    if (variantToSet &&
        typeof variantToSet !== "boolean" &&
        !isAnimationControls.isAnimationControls(variantToSet)) {
        const list = Array.isArray(variantToSet) ? variantToSet : [variantToSet];
        for (let i = 0; i < list.length; i++) {
            const resolved = resolveVariants.resolveVariantFromProps(props, list[i]);
            if (resolved) {
                const { transitionEnd, transition, ...target } = resolved;
                for (const key in target) {
                    let valueTarget = target[key];
                    if (Array.isArray(valueTarget)) {
                        /**
                         * Take final keyframe if the initial animation is blocked because
                         * we want to initialise at the end of that blocked animation.
                         */
                        const index = isInitialAnimationBlocked
                            ? valueTarget.length - 1
                            : 0;
                        valueTarget = valueTarget[index];
                    }
                    if (valueTarget !== null) {
                        values[key] = valueTarget;
                    }
                }
                for (const key in transitionEnd) {
                    values[key] = transitionEnd[key];
                }
            }
        }
    }
    return values;
}
const makeUseVisualState = (config) => (props, isStatic) => {
    const context = React.useContext(index.MotionContext);
    const presenceContext = React.useContext(PresenceContext.PresenceContext);
    const make = () => makeState(config, props, context, presenceContext);
    return isStatic ? make() : useConstant.useConstant(make);
};

exports.makeUseVisualState = makeUseVisualState;
