"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useScreenAnimatePresence = useScreenAnimatePresence;
var _element = require("@wordpress/element");
var _compose = require("@wordpress/compose");
var _i18n = require("@wordpress/i18n");
var styles = _interopRequireWildcard(require("../styles"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

// Possible values:
// - 'INITIAL': the initial state
// - 'ANIMATING_IN': start enter animation
// - 'IN': enter animation has ended
// - 'ANIMATING_OUT': start exit animation
// - 'OUT': the exit animation has ended

// Allow an extra 20% of the total animation duration to account for potential
// event loop delays.
const ANIMATION_TIMEOUT_MARGIN = 1.2;
const isEnterAnimation = (animationDirection, animationStatus, animationName) => animationStatus === 'ANIMATING_IN' && animationName === styles.ANIMATION_END_NAMES[animationDirection].in;
const isExitAnimation = (animationDirection, animationStatus, animationName) => animationStatus === 'ANIMATING_OUT' && animationName === styles.ANIMATION_END_NAMES[animationDirection].out;
function useScreenAnimatePresence({
  isMatch,
  skipAnimation,
  isBack,
  onAnimationEnd
}) {
  const isRTL = (0, _i18n.isRTL)();
  const prefersReducedMotion = (0, _compose.useReducedMotion)();
  const [animationStatus, setAnimationStatus] = (0, _element.useState)('INITIAL');

  // Start enter and exit animations when the screen is selected or deselected.
  // The animation status is set to `IN` or `OUT` immediately if the animation
  // should be skipped.
  const becameSelected = animationStatus !== 'ANIMATING_IN' && animationStatus !== 'IN' && isMatch;
  const becameUnselected = animationStatus !== 'ANIMATING_OUT' && animationStatus !== 'OUT' && !isMatch;
  (0, _element.useLayoutEffect)(() => {
    if (becameSelected) {
      setAnimationStatus(skipAnimation || prefersReducedMotion ? 'IN' : 'ANIMATING_IN');
    } else if (becameUnselected) {
      setAnimationStatus(skipAnimation || prefersReducedMotion ? 'OUT' : 'ANIMATING_OUT');
    }
  }, [becameSelected, becameUnselected, skipAnimation, prefersReducedMotion]);

  // Animation attributes (derived state).
  const animationDirection = isRTL && isBack || !isRTL && !isBack ? 'end' : 'start';
  const isAnimatingIn = animationStatus === 'ANIMATING_IN';
  const isAnimatingOut = animationStatus === 'ANIMATING_OUT';
  let animationType;
  if (isAnimatingIn) {
    animationType = 'in';
  } else if (isAnimatingOut) {
    animationType = 'out';
  }
  const onScreenAnimationEnd = (0, _element.useCallback)(e => {
    onAnimationEnd?.(e);
    if (isExitAnimation(animationDirection, animationStatus, e.animationName)) {
      // When the exit animation ends on an unselected screen, set the
      // status to 'OUT' to remove the screen contents from the DOM.
      setAnimationStatus('OUT');
    } else if (isEnterAnimation(animationDirection, animationStatus, e.animationName)) {
      // When the enter animation ends on a selected screen, set the
      // status to 'IN' to ensure the screen is rendered in the DOM.
      setAnimationStatus('IN');
    }
  }, [onAnimationEnd, animationStatus, animationDirection]);

  // Fallback timeout to ensure that the logic is applied even if the
  // `animationend` event is not triggered.
  (0, _element.useEffect)(() => {
    let animationTimeout;
    if (isAnimatingOut) {
      animationTimeout = window.setTimeout(() => {
        setAnimationStatus('OUT');
        animationTimeout = undefined;
      }, styles.TOTAL_ANIMATION_DURATION.OUT * ANIMATION_TIMEOUT_MARGIN);
    } else if (isAnimatingIn) {
      animationTimeout = window.setTimeout(() => {
        setAnimationStatus('IN');
        animationTimeout = undefined;
      }, styles.TOTAL_ANIMATION_DURATION.IN * ANIMATION_TIMEOUT_MARGIN);
    }
    return () => {
      if (animationTimeout) {
        window.clearTimeout(animationTimeout);
        animationTimeout = undefined;
      }
    };
  }, [isAnimatingOut, isAnimatingIn]);
  return {
    animationStyles: styles.navigatorScreenAnimation,
    // Render the screen's contents in the DOM not only when the screen is
    // selected, but also while it is animating out.
    shouldRenderScreen: isMatch || animationStatus === 'IN' || animationStatus === 'ANIMATING_OUT',
    screenProps: {
      onAnimationEnd: onScreenAnimationEnd,
      'data-animation-direction': animationDirection,
      'data-animation-type': animationType,
      'data-skip-animation': skipAnimation || undefined
    }
  };
}
//# sourceMappingURL=use-screen-animate-presence.js.map