import { c as catalogue, e as extend } from './extend-BoO-6WB1.js';
import { MotionConfigContext, animate } from 'motion/react';
import { useRef, useCallback, useEffect, useMemo, useImperativeHandle, use, createElement } from 'react';

"use strict";
function useLatestFunction(thing) {
  const latestFunctionRef = useRef(thing);
  latestFunctionRef.current = thing;
  return useCallback(
    (...args) => latestFunctionRef.current(...args),
    []
  );
}

"use strict";
const toDependencyArray = (point) => [
  typeof point === "object" ? point.x : null,
  typeof point === "object" ? point.y : null,
  typeof point !== "object" ? point : null
];
const usePointCompareEffect = (callback, point) => useEffect(callback, toDependencyArray(point));
const usePointCompareMemo = (memo, point) => useMemo(memo, toDependencyArray(point));

"use strict";
const basicProps = [
  "alpha",
  "angle",
  "rotation",
  "x",
  "y",
  "width",
  "height"
];
const nestedProps = ["scale", "pivot", "position", "skew"];
const supportedProps = [...basicProps, ...nestedProps];
const filterTransition = (transition) => {
  if (transition === void 0) return void 0;
  const withRemovedKeys = Object.fromEntries(
    Object.entries(transition).filter(([key]) => !(key in supportedProps))
  );
  if (Object.keys(withRemovedKeys).length === 0) return void 0;
  return withRemovedKeys;
};
function createMotionComponent(Component) {
  return function MotionComponent({
    initial,
    animate: animate$1,
    ref: userRef,
    transition,
    ...props
  }) {
    const ref = useRef(null);
    useImperativeHandle(userRef, () => ref.current);
    const firstRenderRef = useRef(true);
    const defaultTransition = use(MotionConfigContext).transition;
    const getTransitionDetails = useLatestFunction(
      (property) => {
        const scopedVague = animate$1?.transition;
        const scopedSpecific = animate$1?.transition?.[property];
        const transitionPropertyVague = transition;
        const transitionPropertySpecific = transition?.[property];
        const motionConfigVague = defaultTransition;
        const motionConfigSpecific = defaultTransition?.[property];
        return filterTransition(scopedSpecific) ?? filterTransition(scopedVague) ?? filterTransition(transitionPropertySpecific) ?? filterTransition(transitionPropertyVague) ?? filterTransition(motionConfigSpecific) ?? filterTransition(motionConfigVague);
      }
    );
    const useInstantState = (key) => {
      const instantValue = props[key];
      const initialValue = useRef(initial?.[key] ?? instantValue ?? animate$1?.[key]).current;
      return usePointCompareMemo(() => {
        if (firstRenderRef.current) return initialValue;
        return instantValue;
      }, instantValue);
    };
    const runningAnimations = useRef({});
    const saveAnimation = useCallback((key, value) => {
      var _a;
      (_a = runningAnimations.current)[key] ?? (_a[key] = []);
      runningAnimations.current[key].push(value);
    }, []);
    const clear = useCallback((key) => {
      runningAnimations.current[key]?.forEach((animation) => animation.stop());
      runningAnimations.current[key] = [];
    }, []);
    const to = useCallback(
      (key, value) => {
        if (value === void 0) return;
        if (!ref.current) return;
        const transition2 = getTransitionDetails(key);
        if (typeof value === "object") {
          const nestedValue = ref.current?.[key];
          const { x, y } = value;
          if (!nestedValue) return;
          if (x !== void 0) {
            saveAnimation(key, animate(nestedValue, {
              x: [
                nestedValue.x,
                x
              ]
            }, transition2));
          }
          if (y !== void 0) {
            saveAnimation(key, animate(nestedValue, {
              y: [
                nestedValue.y,
                y
              ]
            }, transition2));
          }
        } else {
          saveAnimation(key, animate(ref.current, { [key]: value }, transition2));
        }
      },
      [getTransitionDetails]
    );
    const propsToPass = useRef({});
    for (const key of supportedProps) {
      propsToPass.current[key] = useInstantState(key);
      usePointCompareEffect(() => () => clear(key), props[key]);
      usePointCompareEffect(() => to(key, animate$1?.[key]), animate$1?.[key]);
    }
    useEffect(() => {
      firstRenderRef.current = false;
    }, []);
    return createElement(Component, {
      ...props,
      ...propsToPass.current,
      ref
    });
  };
}

"use strict";
const motionCatalogue = new Proxy({}, {
  get(target, prop) {
    if (!(prop in target)) {
      if (prop in catalogue) {
        throw new Error(`${prop} is part of the PIXI namespace, but is not part of the motion namespace! Did you forget to extendMotion?`);
      } else {
        throw new Error(`${prop} is not part of the motion namespace! Did you forget to extendMotion?`);
      }
    }
    return target[prop];
  },
  set(target, prop, value) {
    if (typeof prop === "string") {
      const component = createMotionComponent(`pixi${prop}`);
      target[prop.toLowerCase()] = component;
      target[`pixi${prop}`] = component;
    } else {
      target[prop] = value;
    }
    return true;
  }
});

"use strict";
function extendMotion(objects) {
  Object.assign(motionCatalogue, objects);
  extend(objects);
}

"use strict";
function useExtendMotion(objects) {
  useMemo(() => {
    extendMotion(objects);
  }, [objects]);
}

"use strict";

export { motionCatalogue as default, extendMotion, motionCatalogue as pixiMotion, useExtendMotion };
//# sourceMappingURL=pixi-react2.mjs.map
