#pragma once

#include <reanimated/Compat/WorkletsApi.h>
#include <reanimated/LayoutAnimations/LayoutAnimationsManager.h>
#include <reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.h>
#include <reanimated/LayoutAnimations/LayoutAnimationsUtils.h>
#include <reanimated/LayoutAnimations/StaleSynchronousPropsTracker.h>
#include <reanimated/Tools/PlatformDepMethodsHolder.h>

#include <react/renderer/componentregistry/ComponentDescriptorFactory.h>
#include <react/renderer/mounting/MountingOverrideDelegate.h>
#include <react/renderer/mounting/ShadowTreeRevision.h>
#include <react/renderer/mounting/ShadowView.h>
#include <react/renderer/scheduler/Scheduler.h>
#include <react/renderer/uimanager/UIManagerBinding.h>
#include <reanimated/Compat/ReactNativeGraphics.h>

#include <array>
#include <chrono>
#include <cstdint>
#include <deque>
#include <memory>
#include <optional>
#include <ranges>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace reanimated {

class LayoutAnimationsProxyRegistry;

using namespace facebook;

struct SubtreeRemovalConfig {
  bool shouldRemoveSubviewsWithoutAnimations;
  bool shouldAnimate;
  bool isScreenPop;
};

struct PendingNodeAnimation {
  std::shared_ptr<LightNode> node;
  std::shared_ptr<Serializable> config;
};

// The faded view that a pull mounted, and the values that it comes from. The next pull uses it as the old view.
struct MountedFade {
  ShadowView source;
  double ownOpacity;
  double contentOpacity;
  ShadowView view;
};

struct FadedChild {
  std::shared_ptr<LightNode> node;
  std::optional<MountedFade> mounted;
};

struct PendingLayoutTransition {
  std::shared_ptr<LightNode> node;
  std::shared_ptr<Serializable> config;
  ShadowView before;
};

// The children of the carrier fade with the progress of the run. When the carrier lands, the children
// of the target fade in.
struct ContentFade {
  SharedTransitionContentFade options;
  ContentOpacityRange range;
  double mountedOpacity = 1;
  std::unordered_map<Tag, FadedChild> fadedChildren;
};

// The time of the animation moves the carrier, or the progress of a back gesture moves it.
enum class SharedTransitionDriver : std::uint8_t {
  TIME,
  GESTURE,
};

// A retiring run has no reason to continue, but the frames of its carrier are not in the mount queue yet. It ends in
// the first pull that can remove views after those frames are queued. A finished run stays in the registry until
// the end of the pull, so that a finish does not invalidate the runs that a caller holds.
enum class SharedTransitionLifecycle : std::uint8_t {
  ACTIVE,
  RETIRING,
  // The carrier stays where it is in a held modal until React Native removes the modal.
  STOPPED,
  FINISHED,
};

// Clips the carrier like the ancestors of the shared views do. The content inside the clip cancels its offset, so the
// carrier keeps the frames of its host.
struct CarrierClip {
  ShadowView view;
  ShadowView content;
  AncestorClipRange range;
};

enum class ScrollOffsets : std::uint8_t {
  APPLIED,
  IGNORED,
};

struct ScrollPosition {
  Tag tag;
  react::Point position;

  bool operator==(const ScrollPosition &other) const = default;
};

// The frame and the clip that the layout gives a target, without the offsets of the scroll views around it.
struct TargetLayout {
  react::Rect frame;
  AncestorClip clip;

  bool operator==(const TargetLayout &other) const = default;
};

// The carrier is the view that the run animates, moved from its parent to the carrier layer of its layout root for the
// time of the run. A new run moves its source. A run that reverses keeps its carrier, which is then its target.
// When an ancestor of either end clips the view, a carrier in a carrier layer moves inside a clip.
struct SharedTransitionRun {
  // The run reports this callback once, when it ends. It gets true only when the run lands.
  std::shared_ptr<Serializable> callback;
  SharedTag sharedTag;
  std::shared_ptr<LightNode> carrier;
  ShadowView carrierStartView;
  std::shared_ptr<LightNode> source;
  std::shared_ptr<LightNode> target;
  // Only a change of this layout starts a new move. A scroll or a transform of a parent moves the target on screen, but
  // keeps this layout.
  std::optional<TargetLayout> targetLayout;
  // The source can be released while the run follows its target, which releases the configs of the source.
  std::shared_ptr<Serializable> config;
  // The view that gave config. A run that keeps its target takes the config changes of this view.
  std::shared_ptr<LightNode> configSource;
  // The animation of config aims at the current geometry of the target.
  bool configFollowsTarget = false;
  // The queued or started animation of the carrier aims at the current geometry of the target, so it does not wait for
  // the target window.
  bool followsTarget = false;
  SharedTransitionDriver driver = SharedTransitionDriver::TIME;
  SharedTransitionLifecycle lifecycle = SharedTransitionLifecycle::ACTIVE;
  std::optional<ContentFade> contentFade;
  bool targetOffWindow = false;
  bool targetMovedByContainer = false;
  // UIKit can move a target in a presented view without a React commit. Such a run, and a run that has no sample yet,
  // samples its target on each frame.
  std::optional<bool> targetPlacedNatively;
  std::chrono::steady_clock::time_point targetWindowDeadline;
  bool restoreCarrierOnFinish = false;
  std::optional<CarrierClip> clip;
  // The scroll positions on the path of the target that the end of the run includes.
  std::vector<ScrollPosition> targetScroll;
  bool sourceDrawsWithinFrame = false;
  // The opacity of the ancestors of the target that the end of the run has. A built-in run follows its changes.
  Float targetAncestorOpacity = 1;

  bool holdsStart() const {
    return targetOffWindow && !followsTarget;
  }

  bool movesPathWithScroll() const {
    return driver == SharedTransitionDriver::TIME && !followsTarget;
  }
};

// The first reason, in this order, why a run does not land in a pull.
enum class LandingBlocker : std::uint8_t {
  NONE,
  CARRIER_NOT_ENDED,
  NO_STRUCTURAL_FLUSH,
  CARRIER_FRAMES_UNQUEUED,
  LANDS_WITH_OTHER_RUNS,
  TARGET_IN_HIDDEN_VIEW,
  TARGET_OFF_WINDOW,
  TARGET_MOVED_BY_CONTAINER_TRANSITION,
};

// Asks to show a view that the shared tag hid. The request is stale when the
// view is hidden for a different shared tag now. A finished run asks to show
// its target, and shows its source if the target is removed or exiting.
struct RestoreRequest {
  SharedTag sharedTag;
  std::shared_ptr<LightNode> node;
  std::shared_ptr<LightNode> sourceIfTargetGone = nullptr;
};

struct HiddenView {
  std::shared_ptr<LightNode> node;
  bool restoreDeferred = false;
  bool hiddenAtMount = false;
};

struct InsertedSharedView {
  SharedTag sharedTag;
  std::shared_ptr<LightNode> node;
};

// A view in hiddenViews stays at opacity 0 until it is restored or removed,
// also after the run ends. A view is in the hiddenViews of one shared tag only,
// and only while its node is mapped.
struct SharedElement {
  std::vector<HiddenView> hiddenViews;
};

struct ProgressTransitionEndpoint {
  SharedTag sharedTag;
  std::shared_ptr<LightNode> source;
  std::shared_ptr<LightNode> target;
  bool targetWasHidden;
};

// One in-flight back gesture. Created on the first progress event, destroyed
// when a pull processes its END or CANCELLED state.
struct ProgressTransition {
  TransitionState state = TransitionState::START;
  std::shared_ptr<LightNode> sourceScreen;
  std::shared_ptr<LightNode> targetScreen;
  std::vector<ProgressTransitionEndpoint> endpoints;
  double progress = 0;
  bool updated = true;
};

// A finished gesture whose hidden source views wait for React to commit the
// navigation. The wait ends when React deletes sourceScreen, or a late cancel
// sets cancelled and the next pull restores the endpoint visibility.
struct UncommittedScreenPop {
  std::shared_ptr<LightNode> sourceScreen;
  std::vector<ProgressTransitionEndpoint> endpoints;
  std::vector<std::shared_ptr<Serializable>> progressCallbacks;
  bool cancelled = false;
};

// A node on the path from a shared element up to the root, as it was when the element was collected.
struct Ancestor {
  Tag tag;
  react::Point scrollPosition;
  Transform transform;
  TransformOrigin origin;
  react::Size ownSize;
  Float opacity = 1;
  bool clipsContent = false;
  Float borderRadius = 0;
  // The rotation, scale and skew of the native views between the node and its React parent.
  std::optional<Transform> nativeTransform;
};

// The first ancestor, from the view up, that the platform shows outside the view of its React parent, and its position
// in the absolute positions of the path.
struct AncestorOutsideReactParent {
  size_t index;
  react::Point position;
  Transform transform;
};

struct NativePathPlacement {
  bool placed = false;
  std::optional<AncestorOutsideReactParent> outsideReactParent;
};

struct TargetAncestry {
  std::vector<Ancestor> ancestors;
  std::vector<react::Point> absolutePositions;
  NativePathPlacement placement;
};

struct CollectedTransition {
  Transition transition;
  std::array<std::shared_ptr<LightNode>, 2> nodes;
  std::array<std::vector<react::Point>, 2> absolutePositions;
  std::array<std::vector<Ancestor>, 2> ancestors;
};

using CollectedTransitionMap = std::unordered_map<SharedTag, CollectedTransition>;
using CollectedTransitions = std::vector<std::pair<SharedTag, CollectedTransition>>;

#ifdef __APPLE__
struct HeldModal {
  std::shared_ptr<LightNode> node;
  std::chrono::steady_clock::time_point removableAt;
};
#endif

struct TransactionMeta {
  ShadowViewMutationList filteredMutations;
  ShadowViewMutationList teardownMutations;
  bool surfaceDropped = false;
  CollectedTransitionMap transitionMap;
  CollectedTransitions transitions;
  std::vector<PendingLayoutTransition> layout;
  std::vector<PendingNodeAnimation> entering;
  std::vector<PendingNodeAnimation> exiting;
  std::vector<std::shared_ptr<LightNode>> removedSubtrees;
#ifdef __APPLE__
  std::vector<std::shared_ptr<LightNode>> startedSources;
#endif
  std::vector<RestoreRequest> restoreRequests;
  std::vector<InsertedSharedView> insertedSharedViews;
  // the place in filteredMutations of the first Remove of a screen that React Native Screens snapshots
  std::optional<size_t> screenSnapshotRemoval;
  std::unordered_map<Tag, Tag> staleSnapshots;
  std::unordered_set<Tag> dueRemovals;
  std::vector<std::shared_ptr<LightNode>> modalsToHold;
  std::unordered_set<Tag> recreatedTags;
  // Whatever changes a node's children or their exiting states during updateLightTree must reset its cursors.
  std::unordered_map<Tag, IndexCursors> indexCursors;
};

struct LayoutAnimationsProxy : public LayoutAnimationsProxyCommon {
  mutable std::optional<ProgressTransition> transition_;
  mutable std::optional<UncommittedScreenPop> uncommittedScreenPop_;
  mutable std::shared_ptr<LightNode> topScreen_;
  // The top boundary under each screen when another screen covered it, by screen tag.
  mutable std::unordered_map<Tag, std::weak_ptr<LightNode>> coveredTopBoundaries_;
  // Set whenever the active boundary might no longer be `topScreen_`: when React adds, moves or deletes a boundary or
  // moves one of its ancestors, when the `isActive` prop of a boundary changes, or when a boundary leaves the light
  // tree. A light node never becomes live again, so nothing else changes it.
  mutable bool activeBoundaryMayHaveChanged_ = true;
  mutable std::unordered_set<std::shared_ptr<LightNode>> boundaries_;
  // screens that forceScreenSnapshot_ switched to snapshots after updates; React Native Screens never switches them
  // back
  mutable std::unordered_set<Tag> snapshottedScreens_;
  // A screen becomes visible when its appear transition ends or its dismiss gesture is cancelled.
  // It stays visible until its disappear transition ends. Only a visible screen can be the source
  // of a back transition.
  mutable std::unordered_set<Tag> visibleScreens_;
  mutable std::unordered_map<SharedTag, SharedElement> sharedElements_;
  // The runs by the tag of their carrier. A view carries one run, and a shared tag has one active run.
  mutable std::unordered_map<Tag, SharedTransitionRun> sharedTransitionRuns_;
  mutable CarrierPlacements carrierPlacements_;
  // The views that hold the carriers at the root, by their layout root. A layer takes no touches and hides its
  // carriers from accessibility.
  mutable std::unordered_map<std::shared_ptr<LightNode>, ShadowView> carrierLayers_;
  // Entering animations of target children that wait until their run lands.
  mutable std::vector<PendingNodeAnimation> heldEnterings_;
  std::shared_ptr<SharedTransitionManager> sharedTransitionManager_;
  std::weak_ptr<WorkletRuntime> uiRuntimeWeak_;
  std::function<void(std::function<void()>)> scheduleOnNextUIFrame_;
  void finishCallback(SharedTransitionRun &run, bool finished) const;
  void scheduleSharedTransitionCallback(std::shared_ptr<Serializable> callback, bool finished) const;
  void skipSharedTransition(
      const SharedTag &sharedTag,
      const CollectedTransition &collected,
      std::shared_ptr<Serializable> callback,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  bool isLayoutRoot(const std::shared_ptr<LightNode> &node) const;
  std::shared_ptr<LightNode> layoutRootOf(const std::shared_ptr<LightNode> &node) const;
  bool hasSharedViewInLayoutRoot(
      const SharedTag &sharedTag,
      const std::shared_ptr<LightNode> &node,
      const std::unordered_map<Tag, SharedTag> &tagToName,
      std::unordered_map<SharedTag, std::vector<Tag>> &tagsBySharedTag) const;
  void filterSharedTransitionStarts(TransactionMeta &transaction, const PropsParserContext &propsParserContext) const;
  mutable std::unordered_map<Tag, std::shared_ptr<LightNode>> lightNodes_;
  mutable std::unordered_set<Tag> unmappedContentFadeTags_;
  // removed components whose views are still shown; their shared transition configs are released when they unmap
  mutable std::unordered_set<Tag> deferredSharedTransitionConfigReleases_;
  mutable std::vector<std::pair<ShadowTreeRevision::Number, ShadowViewMutationList>> pendingTransactions_;
  mutable bool surfaceToRemove_ = false;
#ifdef ANDROID
  ReleaseViewTransitionsFunction releaseViewTransitions_;
  void releaseViewTransitionsOfMovedCarriers(const ShadowViewMutationList &mutations) const;
  bool isInRemovedScreen(const std::shared_ptr<LightNode> &node, const ShadowViewMutationList &mutations) const;
  mutable bool cleanupPullScheduled_ = false;
  // tags of JS-thread batches that are not in the mount queue yet
  mutable std::unordered_map<Tag, int> unqueuedBatchTags_;
  // carriers whose last UI-thread frames are not in the mount queue yet
  mutable std::unordered_map<Tag, int> unqueuedCarrierFrames_;
  // frames in `unqueuedCarrierFrames_` that no release round trip includes yet
  mutable std::unordered_map<Tag, int> carrierFramesWithoutRelease_;
  // a pull found a frame in `unqueuedCarrierFrames_` and waits for its release
  mutable bool pullWaitsForCarrierRelease_ = false;
  // deletes of carriers that left a removed layout root, until their last frames are in the mount queue, and of
  // the carrier layers that held them
  mutable std::deque<std::pair<Tag, ShadowViewMutationList>> carrierDeletesAfterQueuedFrames_;
#endif

#ifdef __APPLE__
  mutable std::vector<HeldModal> heldModals_;
  ForceScreenSnapshotFunction forceScreenSnapshot_;
  SetSharedTransitionOverlayFunction setSharedTransitionOverlay_;
  NativeViewPlacementFunction nativeViewPlacement_;
  IsMovedByContainerTransitionFunction isMovedByContainerTransition_;
#endif
  mutable StaleSynchronousPropsTracker staleSynchronousProps_;
  // It starts above the initial LightNode::followedInSynchronousBatch, so a view that no batch marked never matches.
  mutable uint64_t synchronousBatch_ = 1;
  void warnAboutStaleSynchronousProps(Tag tag, Tag staleTag, LayoutAnimationType type) const;
  void warnIfSnapshotIsStale(const ShadowView &snapshot, const TransactionMeta &transaction) const;

  LayoutAnimationsProxy(SurfaceId surfaceId, const LayoutAnimationsProxyDependencies &dependencies);

  void startEnteringAnimation(
      const std::shared_ptr<LightNode> &node,
      const std::shared_ptr<Serializable> &config,
      bool isContentFadeIn = false) const;
  void startExitingAnimation(const std::shared_ptr<LightNode> &node, const std::shared_ptr<Serializable> &config) const;
  void startLayoutAnimation(
      const std::shared_ptr<LightNode> &node,
      const std::shared_ptr<Serializable> &config,
      const ShadowView &before) const;
  void startSharedTransition(
      SharedTransitionRun &run,
      const ShadowView &before,
      const ShadowView &after,
      const Props::Shared &frameProps,
      const std::shared_ptr<Serializable> &config,
      std::optional<ContentOpacityRange> contentOpacity,
      std::optional<AncestorClipRange> clip = std::nullopt) const;
  std::optional<ContentOpacityRange> restartContentFade(SharedTransitionRun &run) const;
  double contentOpacityOnScreen(const SharedTransitionRun &run) const;
  std::vector<std::shared_ptr<LightNode>> fadingChildren(
      const std::shared_ptr<LightNode> &node,
      const std::unordered_set<Tag> &hiddenTags) const;
  ShadowView withContentOpacity(
      const std::shared_ptr<LightNode> &child,
      const ShadowView &view,
      double contentOpacity,
      const PropsParserContext &propsParserContext) const;
  double ownOpacity(const std::shared_ptr<LightNode> &node, const ShadowView &animatedView) const;
  void applyContentOpacity(
      FadedChild &child,
      double fromContentOpacity,
      double toContentOpacity,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  std::vector<SharedTransitionRun *> contentFadeRuns() const;
  void composeCarrierContent(ShadowViewMutationList &filteredMutations, const PropsParserContext &propsParserContext)
      const;
  void restoreFadedChildren(
      ContentFade &contentFade,
      const std::vector<std::shared_ptr<LightNode>> &keptChildren,
      const std::unordered_set<Tag> &hiddenTags,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  void holdTargetEntering(TransactionMeta &transaction, const PropsParserContext &propsParserContext) const;
  std::unordered_set<std::shared_ptr<LightNode>> fadingTargetContent() const;
  void startHeldEntering(
      const PendingNodeAnimation &held,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  void fadeInTargetContent(
      const SharedTransitionRun &run,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  void startProgressTransition(
      const std::shared_ptr<LightNode> &carrier,
      const ShadowView &before,
      const ShadowView &after,
      const Props::Shared &frameProps,
      const std::shared_ptr<Serializable> &config) const;
  void applyProgressAnimations();
  bool followSharedTransitionTargets(const PropsParserContext &propsParserContext, bool onlyPlacedNatively) const;
  bool followSharedTransitionTarget(SharedTransitionRun &run, const PropsParserContext &propsParserContext) const;
  bool carriesRunningRun(const SharedTransitionRun &run) const;
  void followTargetsPlacedNatively() const override;
  LayoutAnimation *liveGeometryAnimation(const SharedTransitionRun &run) const;
  LayoutAnimation *timedAnimation(const SharedTransitionRun &run) const;
  void moveWithScrolledTargets() const;
  void moveWithScrolledTarget(SharedTransitionRun &run, LayoutAnimation &animation) const;
  bool followsWithoutRestart(const SharedTransitionRun &run, const ShadowView &carrierTarget) const;
  bool hasLiveTarget(const SharedTransitionRun &run) const;
  void markViewsFollowedBySharedTransitions() const;
  bool isFollowedBySharedTransition(const LightNode &node) const;
  ShadowView
  withGeometryOf(const ShadowView &view, const ShadowView &target, const PropsParserContext &propsParserContext) const;
  void handleProgressTransition(
      TransactionMeta &transaction,
      const ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext,
      bool popSettledThisPull) const;
  void resolveTransitionLifecycle(
      TransactionMeta &transaction,
      const ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  bool settleUncommittedScreenPop(TransactionMeta &transaction, const PropsParserContext &propsParserContext) const;
  void restoreCancelledProgressTransition(
      const std::vector<ProgressTransitionEndpoint> &endpoints,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  void resolveDeferredSourceScreen() const;
  bool isLightNodeMapped(const std::shared_ptr<LightNode> &node) const;
  void unmapLightNode(const std::shared_ptr<LightNode> &node) const;
  void releaseUnmappedContentFadeConfigs() const;

  bool mayChangeActiveBoundary(const ShadowViewMutation &mutation) const;
  bool holdsBoundary(const std::shared_ptr<LightNode> &node) const;
  std::optional<std::unordered_set<SharedTag>> sharedTagsAffectedBy(const ShadowViewMutationList &mutations) const;
  void updateLightTree(
      const PropsParserContext &propsParserContext,
      ShadowViewMutationList &mutations,
      TransactionMeta &transaction) const;

  std::optional<ShadowView>
  reparentLayoutAnimation(Tag tag, Tag parentTag, const ShadowView &newView, react::Point offset) const;
  void moveLayoutAnimationTarget(Tag tag, LayoutAnimation &animation, const ShadowView &newView, react::Point offset)
      const;
  const ShadowView &mountedView(const std::shared_ptr<LightNode> &node) const;

  void applyInitialMutationsToLightTree(const ShadowViewMutationList &mutations) const;
  void updateLightNodeProps(
      const std::shared_ptr<LightNode> &node,
      const ShadowView &oldView,
      const ShadowView &newView) const;
  void resolveLightNodeProps(const std::shared_ptr<LightNode> &node) const;
  void initializeLightTree(const ShadowTreeRevision &baseRevision);
  bool isLightTreeInitialized() const {
    return lightNodes_.contains(surfaceId_);
  }

  void applySynchronousProps(const UpdatesBatch &updatesBatch, bool trackInLightTree) const override;
  void composeSynchronousProps(UpdatesBatch &updatesBatch) const override;

  void reconcileContradictedRemovals(
      ShadowViewMutationList &mutations,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
#ifdef ANDROID
  ShadowView resetPropsMissingFrom(
      const ShadowView &view,
      const ShadowView &mounted,
      const PropsParserContext &propsParserContext) const;
#endif

  void handleSharedTransitionsStart(
      const std::shared_ptr<LightNode> &afterTopScreen,
      const std::shared_ptr<LightNode> &beforeTopScreen,
      TransactionMeta &transaction,
      const ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;

  bool shouldFlushStructuralMutations() const;
  void cleanupAnimations(
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext,
      bool flushStructuralMutations) const;
  void eraseFinishedSharedTransitionRuns() const;
  void cleanupSharedTransitions(TransactionMeta &transaction, const PropsParserContext &propsParserContext) const;
#ifdef ANDROID
  bool hasPendingStructuralCleanup() const;
  bool isCarrierOfUncoveredRun(Tag tag, const std::vector<std::shared_ptr<LightNode>> &coveringViews) const;
  void maybeScheduleCleanupPull(bool flushedStructuralMutations) const;
  void holdFramesUntilBatchIsQueued(const ShadowViewMutationList &mutations) const;
  void releaseFramesOfQueuedBatch(const std::vector<Tag> &tags) const;
  void keepCarriersDetachedUntilFramesAreQueued(const ShadowViewMutationList &mutations) const;
  void maybeStartCarrierRelease() const;
  bool isCarrierAnimating(Tag carrierTag) const;
  void releaseCarriersOfQueuedFrames(const std::unordered_map<Tag, int> &frames) const;
  void removeCarrierBeforeQueuedFrames(const std::shared_ptr<LightNode> &carrier, ShadowViewMutationList &mutations)
      const;
  void flushCarrierDeletesAfterQueuedFrames(ShadowViewMutationList &mutations) const;
#endif

  bool defersLayoutAnimationStart(Tag tag) const override;

  void hideTransitioningViews(
      BeforeOrAfter index,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;

  void keepTransitioningViewsHidden(
      ShadowViewMutationList &filteredMutations,
      const PropsParserContext &propsParserContext) const;
  std::optional<SurfaceId> endLayoutAnimation(int tag, bool shouldRemove) const override;
  void startSurface(
      const facebook::react::ShadowTree &shadowTree,
      std::weak_ptr<const facebook::react::MountingOverrideDelegate> mountingOverrideDelegate) override;
  std::optional<SurfaceId> onTransitionProgress(int tag, double progress, bool isClosing, bool isGoingForward) override;
  std::optional<SurfaceId> onGestureCancel(int tag) override;
  void onSharedTransitionConfigured(const std::vector<Tag> &viewTags) override;
  bool deferSharedTransitionConfigRelease(Tag tag) override;
  void updateVisibleScreens(int tag, double progress, bool isClosing) const;
  void markScreenVisible(int tag) const;
  bool isUncommittedScreenPopSource(int tag) const;
  std::optional<SurfaceId> cancelUncommittedScreenPop() const;
  void shadowTreeWillCommit(bool isSurfaceRemoval) override;
  void clearSurfaceState() const override;

  std::shared_ptr<LightNode> findActiveBoundary(const std::shared_ptr<LightNode> &node) const;
  std::shared_ptr<LightNode> findBoundaryGuess(
      const std::shared_ptr<LightNode> &node,
      const std::shared_ptr<LightNode> &screen) const;
  std::shared_ptr<LightNode> findRevealedBoundary(const std::shared_ptr<LightNode> &screen) const;
  bool isShownBy(const std::shared_ptr<LightNode> &boundary, const std::shared_ptr<LightNode> &screen) const;
  void rememberCoveredTopBoundary(
      const std::shared_ptr<LightNode> &beforeTopScreen,
      const std::shared_ptr<LightNode> &afterTopScreen) const;

  void findSharedElements(
      const std::shared_ptr<LightNode> &screen,
      BeforeOrAfter index,
      const std::optional<std::unordered_set<SharedTag>> &sharedTags,
      TransactionMeta &transaction) const;
  void findSharedElementsOnScreen(
      const std::shared_ptr<LightNode> &node,
      BeforeOrAfter index,
      TransactionMeta &transaction) const;
  void findSharedElementsWithTags(
      const std::shared_ptr<LightNode> &screen,
      BeforeOrAfter index,
      const std::unordered_set<SharedTag> &sharedTags,
      TransactionMeta &transaction) const;
  void collectSharedElement(
      const std::shared_ptr<LightNode> &node,
      const SharedTag &sharedTag,
      BeforeOrAfter index,
      TransactionMeta &transaction) const;

  void detachCarrier(
      SharedTransitionRun &run,
      const ShadowView &startView,
      bool nested,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  bool canDetachBeforeScreenSnapshot(const std::shared_ptr<LightNode> &node, const TransactionMeta &transaction) const;
  void attachCarrier(
      SharedTransitionRun &run,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  void stopSharedTransitions(
      const std::vector<SharedTransitionRun *> &runs,
      const std::shared_ptr<LightNode> &node,
      TransactionMeta &transaction) const;
  void moveCarrierIntoModal(
      SharedTransitionRun &run,
      const std::shared_ptr<LightNode> &modalContent,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  react::Point nativeOriginOnScreen(const std::shared_ptr<LightNode> &node) const;
  std::vector<SharedTransitionRun *> runsEndedBy(const std::shared_ptr<LightNode> &node) const;
  void showCarrierReactProps(
      SharedTransitionRun &run,
      const Props::Shared &previousProps,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  ShadowView withFrameBase(
      const ShadowView &frame,
      const Props::Shared &previousBase,
      const Props::Shared &base,
      const PropsParserContext &propsParserContext) const;
  void retainRemovedCarriers(const std::shared_ptr<LightNode> &node, TransactionMeta &transaction) const;
  void removeCarriersHostedIn(const std::shared_ptr<LightNode> &node, TransactionMeta &transaction) const;
  void retainCarrierContent(const std::shared_ptr<LightNode> &node) const;
  bool isRetainedContent(const std::shared_ptr<LightNode> &node) const;
  bool defersSubtreeRemoval(const std::shared_ptr<LightNode> &node) const;
  void flushSubtreeRemovals(TransactionMeta &transaction) const;
  void scheduleTransitionLifecycleCleanup() const;
  std::shared_ptr<LightNode> detachedCarrierOf(const std::shared_ptr<LightNode> &node) const;
  std::shared_ptr<LightNode> carrierAncestorOf(const std::shared_ptr<LightNode> &node) const;
  bool isCarriedBy(const std::shared_ptr<LightNode> &carrier, const std::shared_ptr<LightNode> &ancestor) const;
  void moveNestedCarriersOutOf(const std::shared_ptr<LightNode> &node, TransactionMeta &transaction) const;
  bool keepsNesting(const std::shared_ptr<LightNode> &carrier, const std::shared_ptr<LightNode> &target) const;
  void moveNestedCarrierToRoot(
      SharedTransitionRun &run,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  bool restartsAtRoot(const SharedTransitionRun &run) const;
  react::Point carriedDistance(const std::shared_ptr<LightNode> &carrier) const;
  ShadowView movedCarrierTarget(Tag tag, react::Point offset) const;
  ShadowView inParentSpace(const ShadowView &snapshot, const std::shared_ptr<LightNode> &node) const;
  react::Point originOnScreen(const std::shared_ptr<LightNode> &node) const;
  Tag carrierParentTag(const std::shared_ptr<LightNode> &carrier) const;
  ShadowView targetInCarrierSpace(
      const SharedTransitionRun &run,
      const ShadowView &target,
      const PropsParserContext &propsParserContext) const;
  std::optional<Transform> transformsAboveNestedCarrier(
      const std::shared_ptr<LightNode> &carrier,
      const std::shared_ptr<LightNode> &ancestor,
      const ShadowView &view) const;
  react::Point positionIn(
      const std::shared_ptr<LightNode> &node,
      const std::shared_ptr<LightNode> &ancestor,
      bool useViewsOnScreen) const;
  bool isRetiringContent(Tag tag) const;
  void retireOrphanedSharedTransitions() const;
  void finishCoveredSharedTransitions(
      const std::shared_ptr<LightNode> &topScreen,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  void retireSharedTransition(SharedTransitionRun &run) const;
  void restoreEndsOf(const SharedTransitionRun &run, TransactionMeta &transaction) const;
  void finishRetiringSharedTransitions(
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext,
      bool flushStructuralMutations) const;
  void removeFromParent(
      const std::shared_ptr<LightNode> &node,
      ShadowViewMutationList &mutations,
      ShadowViewMutationList &rootMutations) const;
  int carrierHostIndex(const std::shared_ptr<LightNode> &node) const;
  Tag carrierHostTag(const std::shared_ptr<LightNode> &node) const;
  void insertCarrierIntoHost(const SharedTransitionRun &run, const ShadowView &view, ShadowViewMutationList &mutations)
      const;
  void removeCarrierFromHost(
      const SharedTransitionRun &run,
      const ShadowView &mounted,
      ShadowViewMutationList &removals,
      ShadowViewMutationList &deletes) const;
  void ensureCarrierLayer(
      const std::shared_ptr<LightNode> &host,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  void prepareCarrierLayerInWindow(TransactionMeta &transaction, const PropsParserContext &propsParserContext) const;
  void removeCarrierLayersIn(const std::shared_ptr<LightNode> &node, ShadowViewMutationList &mutations) const;
  bool keepsCarrierLayerInWindow() const;
  NativeViewPlacement::Kind nativePlacementKind(const std::shared_ptr<LightNode> &node) const;
  NativeViewPlacement::Kind nativePlacementKind(
      const std::shared_ptr<LightNode> &node,
      const std::shared_ptr<LightNode> &ancestor) const;
  NativeViewPlacement::Kind targetBoundaryPlacementKind(const std::shared_ptr<LightNode> &node) const;
  bool isOffWindow(const std::shared_ptr<LightNode> &node) const;
  bool isInOtherWindow(const std::shared_ptr<LightNode> &target) const;
#ifdef __APPLE__
  bool isMovedByContainerTransition(const std::shared_ptr<LightNode> &target) const;
#endif
  void waitForTargetWindow(SharedTransitionRun &run, const std::shared_ptr<LightNode> &target) const;
  void endSharedTransitionOf(
      const SharedTag &sharedTag,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  ShadowView targetOnScreen(
      const SharedTransitionRun &run,
      const PropsParserContext &propsParserContext,
      AncestorClip *clip = nullptr) const;
  ShadowView targetOnScreen(
      const SharedTransitionRun &run,
      const PropsParserContext &propsParserContext,
      bool &placedNatively,
      AncestorClip *clip = nullptr,
      Float *ancestorOpacity = nullptr) const;
  AncestorClip fitTargetClip(const SharedTransitionRun &run, const react::Rect &frame, const AncestorClip &clip) const;
  bool followsClipTarget(SharedTransitionRun &run, const AncestorClip &clip) const;
#ifdef __APPLE__
  void waitForContainerTransitions() const;
#endif
  void followOffWindowTargets(TransactionMeta &transaction, const PropsParserContext &propsParserContext) const;
  void followProgressTargets(const PropsParserContext &propsParserContext) const;
  const LayoutAnimation &carrierAnimation(const SharedTransitionRun &run) const;
  const ShadowView &carrierViewOnScreen(const SharedTransitionRun &run) const;
  SharedTransitionRun *runOfCarrier(const std::shared_ptr<LightNode> &node) const;
  SharedTransitionRun *activeSharedTransitionRun(const SharedTag &sharedTag) const;
  std::optional<SharedTransitionConfig> configOfSource(const SharedTransitionRun &run) const;
  SharedTransitionRun *sharedTransitionRun(Tag carrierTag, SharedTransitionLifecycle lifecycle) const;
  // A loop over this view must not change the lifecycle of a run.
  auto activeSharedTransitionRuns() const {
    return sharedTransitionRuns_ | std::views::values | std::views::filter([](const SharedTransitionRun &run) {
             return run.lifecycle == SharedTransitionLifecycle::ACTIVE;
           });
  }
  ShadowView asCarrierView(
      const ShadowView &view,
      const std::shared_ptr<LightNode> &carrier,
      const PropsParserContext &propsParserContext) const;
  bool targetEndChanged(const SharedTransitionRun &run, const ShadowView &carrierTarget, bool layoutKept) const;
  TargetLayout targetLayoutOf(const std::shared_ptr<LightNode> &target) const;
  TargetAncestry ancestryOf(const SharedTransitionRun &run) const;
  void takeTargetScroll(SharedTransitionRun &run) const;
  std::vector<ScrollPosition> scrollPositionsOf(const std::shared_ptr<LightNode> &target) const;
  react::Point scrolledDistance(const SharedTransitionRun &run, const TargetAncestry &ancestry) const;
  static TargetAncestry withScrollPositions(TargetAncestry ancestry, const std::vector<ScrollPosition> &positions);
  react::Point translationOnScreen(const TargetAncestry &ancestry) const;
  AncestorClip targetClipOf(const SharedTransitionRun &run, const TargetAncestry &ancestry) const;
  ShadowView ownCarrierView(
      const ShadowView &view,
      const std::shared_ptr<LightNode> &carrier,
      const PropsParserContext &propsParserContext) const;
  ShadowView carrierViewWithStyleOf(
      const ShadowView &source,
      const std::shared_ptr<LightNode> &carrier,
      const PropsParserContext &propsParserContext) const;
  Props::Shared carrierFrameProps(
      const SharedTransitionRun &run,
      const ShadowView &target,
      const ShadowView &legStart,
      const PropsParserContext &propsParserContext) const;
  ShadowView withStyleOf(const ShadowView &view, const ShadowView &source, const PropsParserContext &propsParserContext)
      const;
  ShadowView withChangedStyleOf(
      ShadowView view,
      const ShadowView &source,
      const ShadowView &reference,
      const PropsParserContext &propsParserContext) const;
  bool endsPathOnScreen(
      const std::shared_ptr<LightNode> &pathNode,
      const std::shared_ptr<LightNode> &node,
      bool useViewsOnScreen) const;
  const ShadowView &pathView(
      const std::shared_ptr<LightNode> &pathNode,
      const std::shared_ptr<LightNode> &node,
      bool useViewsOnScreen) const;

  SharedTransitionRun *continueSharedTransitionRun(
      const SharedTag &sharedTag,
      const std::array<std::shared_ptr<LightNode>, 2> &nodes,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  SharedTransitionRun &startSharedTransitionRun(
      const ShadowView &before,
      const AncestorClip *clip,
      bool sourceDrawsWithinFrame,
      const SharedTag &sharedTag,
      const std::array<std::shared_ptr<LightNode>, 2> &nodes,
      const std::shared_ptr<Serializable> &config,
      SharedTransitionDriver driver,
      const std::optional<SharedTransitionContentFade> &contentFade,
      std::shared_ptr<Serializable> callback,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext) const;
  bool prepareSharedTransition(
      CollectedTransition &collectedTransition,
      const SharedTransitionRun *continuingRun,
      bool sourceDrawsWithinFrame,
      const PropsParserContext &propsParserContext) const;
  std::optional<AncestorClipRange> moveClipTo(SharedTransitionRun &run, const AncestorClip &clip) const;
  void finishEndedSharedTransitions(
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext,
      bool flushStructuralMutations,
      std::unordered_set<Tag> &preservedTags) const;
  LandingBlocker landingBlocker(
      const SharedTransitionRun &run,
      bool flushStructuralMutations,
      const std::vector<std::shared_ptr<LightNode>> &coveringViews) const;
  bool waitsForCoveringRun(const SharedTransitionRun &run, const std::vector<std::shared_ptr<LightNode>> &coveringViews)
      const;
  bool runContinues(Tag carrierTag) const;
  bool landsWithOtherRuns(const SharedTransitionRun &run) const;
  std::vector<std::shared_ptr<LightNode>> viewsHiddenByContinuingRuns() const;
  void finishSharedTransition(
      SharedTransitionRun &run,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext,
      bool landed = false) const;
  void finishNestedSharedTransitions(
      const std::shared_ptr<LightNode> &carrier,
      TransactionMeta &transaction,
      const PropsParserContext &propsParserContext,
      bool landed) const;
  bool hasUnqueuedCarrierFrame(Tag tag) const;
  std::optional<SharedTag> findSharedTag(const ShadowView &view) const;
  void hideInSharedElement(
      const SharedTag &sharedTag,
      const std::shared_ptr<LightNode> &node,
      bool hiddenAtMount = false) const;
  void hideInsertedSharedViewsBesideTheirTag(TransactionMeta &transaction, const PropsParserContext &propsParserContext)
      const;
  bool isSharedTagShownOrMoved(const SharedTag &sharedTag, const std::shared_ptr<LightNode> &except) const;
  bool isSharedTagMoved(const SharedTag &sharedTag) const;
  RestoreRequest withShownEndpoint(const RestoreRequest &request) const;
  bool showHiddenView(const RestoreRequest &request) const;
  void deferRestore(const RestoreRequest &request) const;
  void forgetHiddenView(Tag tag) const;
  std::unordered_set<Tag> hiddenViewTags() const;
  std::unordered_set<Tag> viewTagsKeptHidden() const;
  void queuePendingRestores(TransactionMeta &transaction) const;
  bool isOnTopScreen(const std::shared_ptr<LightNode> &node) const;
  bool isInSharedTransition(const std::shared_ptr<LightNode> &node) const;

  std::vector<react::Point> getAbsolutePositionsForRootPathView(
      const std::shared_ptr<LightNode> &node,
      bool useViewsOnScreen,
      ScrollOffsets scrollOffsets = ScrollOffsets::APPLIED) const;
  std::vector<Ancestor> collectAncestors(
      const std::shared_ptr<LightNode> &node,
      bool useViewsOnScreen,
      ScrollOffsets scrollOffsets = ScrollOffsets::APPLIED) const;
  NativePathPlacement
  placeNatively(std::vector<Ancestor> &ancestors, std::vector<react::Point> &absolutePositions, bool settled) const;
  void adjustForNativePlacement(CollectedTransition &collectedTransition, BeforeOrAfter index) const;
  void adjustForNativePlacement(CollectedTransitions &transitions) const;
  const ShadowView &viewOnScreen(const std::shared_ptr<LightNode> &node) const;

  void overrideTransform(
      ShadowView &shadowView,
      const std::optional<Transform> &transform,
      const PropsParserContext &propsParserContext) const;
  void applyAncestorEffects(
      ShadowView &shadowView,
      const Transition &transition,
      BeforeOrAfter index,
      const PropsParserContext &propsParserContext) const;
  void fitClipOfUnclippedSide(
      CollectedTransition &collectedTransition,
      bool sourceDrawsWithinFrame,
      const std::shared_ptr<LightNode> &carrier) const;
  bool drawsWithinFrame(const ShadowView &view, const std::optional<Transform> &ancestorTransform) const;
  const react::Rect &windowFrame() const;
  AncestorClip carrierClipOnScreen(const SharedTransitionRun &run) const;
  CarrierClip createCarrierClip(
      const AncestorClip &clip,
      const ShadowView &source,
      const PropsParserContext &propsParserContext) const;
  CarrierClip
  clipAt(const CarrierClip &carrierClip, const AncestorClip &clip, const PropsParserContext &propsParserContext) const;
  AncestorClip interpolateClip(const AncestorClipRange &range, double progress) const;
  void updateCarrierClip(
      Tag carrierTag,
      const LayoutAnimation &animation,
      ShadowViewMutationList &mutations,
      const PropsParserContext &propsParserContext) const;
  static AncestorClip withAnimatedClip(AncestorClip clip, const LayoutAnimation &animation);
  void parseAncestorOpacityAndClip(
      const std::vector<Ancestor> &ancestors,
      const std::vector<react::Point> &absolutePositions,
      Float &opacity,
      AncestorClip &clip) const;

  std::optional<Transform> parseParentTransforms(
      const std::vector<Ancestor> &ancestors,
      const std::vector<react::Point> &absolutePositions,
      const std::optional<AncestorOutsideReactParent> &nativePlacement = std::nullopt) const;
  std::optional<Transform> parentTransformsOf(
      const std::vector<Ancestor> &ancestors,
      const std::vector<react::Point> &absolutePositions,
      const std::optional<AncestorOutsideReactParent> &nativePlacement) const;
  static bool readsZ(const Transform &transform);
  static Transform withFlatZAxis(Transform transform);
  react::Transform resolveTransform(
      const react::Size &originFrameSize,
      const react::Size &ownSize,
      const Transform &transform,
      const TransformOrigin &transformOrigin) const;
  std::array<float, 3>
  getTranslateForTransformOrigin(float viewWidth, float viewHeight, const TransformOrigin &transformOrigin) const;

  void handleSubtreeRemoval(const std::shared_ptr<LightNode> &node, TransactionMeta &transaction) const;
  bool holdsSnapshottedScreen(const std::shared_ptr<LightNode> &node) const;
  bool hasRunningAnimations() const;
#ifdef __APPLE__
  bool holdsModal(
      const std::shared_ptr<LightNode> &node,
      const std::vector<SharedTransitionRun *> &endedRuns,
      const TransactionMeta &transaction) const;
#endif
  std::shared_ptr<LightNode> modalContentOfRun(const SharedTransitionRun &run, const std::shared_ptr<LightNode> &root)
      const;
  std::shared_ptr<LightNode> modalContentOf(
      const std::shared_ptr<LightNode> &view,
      const std::shared_ptr<LightNode> &root) const;
  bool isWindowLayerHost(const std::shared_ptr<LightNode> &host) const;
  bool isInHeldModal(const std::shared_ptr<LightNode> &node) const;
  void collectDueRemovals(TransactionMeta &transaction) const;
  void tearDown(const std::unordered_set<Tag> &removals, ShadowViewMutationList &mutations) const;

  void addOngoingAnimations(TransactionMeta &transaction) const;
  ShadowView cloneViewWithoutOpacity(const ShadowView &shadowView, const PropsParserContext &propsParserContext) const;

  bool planSubtreeRemoval(
      const std::shared_ptr<LightNode> &node,
      TransactionMeta &transaction,
      SubtreeRemovalConfig config,
      std::vector<std::shared_ptr<LightNode>> &removals) const;
  void endAnimationsRecursively(
      const std::shared_ptr<LightNode> &node,
      ShadowViewMutationList &mutations,
      ShadowViewMutationList &rootMutations,
      bool removeBeforeChildren = false) const;

  // MountingOverrideDelegate

  bool shouldOverridePullTransaction() const override;
  std::optional<MountingTransaction> pullTransaction(
      SurfaceId surfaceId,
      MountingTransaction::Number number,
      const TransactionTelemetry &telemetry,
      ShadowViewMutationList mutations) const override;
};

std::shared_ptr<LayoutAnimationsProxyRegistry> createLayoutAnimationsProxyDefaultRegistry(
    const LayoutAnimationsProxyDependencies &dependencies);

} // namespace reanimated
