'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var extend = require('./extend-Cc5IUQqW.js');
var react = require('motion/react');
var o = require('react');

"use strict";
function useLatestFunction(thing) {
  const latestFunctionRef = o.useRef(thing);
  latestFunctionRef.current = thing;
  return o.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) => o.useEffect(callback, toDependencyArray(point));
const usePointCompareMemo = (memo, point) => o.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,
    ref: userRef,
    transition,
    ...props
  }) {
    const ref = o.useRef(null);
    o.useImperativeHandle(userRef, () => ref.current);
    const firstRenderRef = o.useRef(true);
    const defaultTransition = o.use(react.MotionConfigContext).transition;
    const getTransitionDetails = useLatestFunction(
      (property) => {
        const scopedVague = animate?.transition;
        const scopedSpecific = animate?.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 = o.useRef(initial?.[key] ?? instantValue ?? animate?.[key]).current;
      return usePointCompareMemo(() => {
        if (firstRenderRef.current) return initialValue;
        return instantValue;
      }, instantValue);
    };
    const runningAnimations = o.useRef({});
    const saveAnimation = o.useCallback((key, value) => {
      var _a;
      (_a = runningAnimations.current)[key] ?? (_a[key] = []);
      runningAnimations.current[key].push(value);
    }, []);
    const clear = o.useCallback((key) => {
      runningAnimations.current[key]?.forEach((animation) => animation.stop());
      runningAnimations.current[key] = [];
    }, []);
    const to = o.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, react.animate(nestedValue, {
              x: [
                nestedValue.x,
                x
              ]
            }, transition2));
          }
          if (y !== void 0) {
            saveAnimation(key, react.animate(nestedValue, {
              y: [
                nestedValue.y,
                y
              ]
            }, transition2));
          }
        } else {
          saveAnimation(key, react.animate(ref.current, { [key]: value }, transition2));
        }
      },
      [getTransitionDetails]
    );
    const propsToPass = o.useRef({});
    for (const key of supportedProps) {
      propsToPass.current[key] = useInstantState(key);
      usePointCompareEffect(() => () => clear(key), props[key]);
      usePointCompareEffect(() => to(key, animate?.[key]), animate?.[key]);
    }
    o.useEffect(() => {
      firstRenderRef.current = false;
    }, []);
    return o.createElement(Component, {
      ...props,
      ...propsToPass.current,
      ref
    });
  };
}

"use strict";
const motionCatalogue = new Proxy({}, {
  get(target, prop) {
    if (!(prop in target)) {
      if (prop in extend.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.extend(objects);
}

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

"use strict";

exports.default = motionCatalogue;
exports.extendMotion = extendMotion;
exports.pixiMotion = motionCatalogue;
exports.useExtendMotion = useExtendMotion;
//# sourceMappingURL=pixi-react2.js.map
