<commit_before>import {
  Props,
  Pose,
  PoserState,
  PoseMap,
  ChildPosers,
  GetTransitionProps,
  GetInstantTransition,
  StartAction,
  StopAction,
  ResolveTarget,
  TransformPose,
  AddTransitionDelay,
  ConvertTransitionDefinition,
  TransitionDefinition,
  TransitionFactory,
  TransitionMap,
  SetValue,
  SetValueNative,
  ApplyMap
} from '../types';
import { getPoseValues } from '../inc/selectors';
import { invariant } from 'hey-listen';

type AnimationsPromiseList = Array<Promise<any>>;

type SetterFactoryProps<V, A, C, P> = {
  state: PoserState<V, A, C, P>;
  poses: PoseMap<A>;
  getInstantTransition: GetInstantTransition<V, A>;
  startAction: StartAction<V, A, C>;
  stopAction: StopAction<C>;
  setValue: SetValue<V>;
  setValueNative: SetValueNative;
  addActionDelay: AddTransitionDelay<A>;
  getTransitionProps: GetTransitionProps<V>;
  resolveTarget: ResolveTarget<V>;
  convertTransitionDefinition: ConvertTransitionDefinition<V, A>;
  transformPose?: TransformPose<V, A, C, P>;
  posePriority?: string[];
};

export const resolveProp = (target: any, props: Props) =>
  typeof target === 'function' ? target(props) : target;

const poseDefault = <A>(
  pose: Pose<A>,
  prop: string,
  defaultValue: any,
  resolveProps: Props
) =>
  pose && pose[prop] !== undefined
    ? resolveProp(pose[prop], resolveProps)
    : defaultValue;

const startChildAnimations = <V, A, C, P>(
  children: ChildPosers<V, A, C, P>,
  next: string,
  pose: Pose<A>,
  props: Props
) => {
  const animations: Array<Promise<any>> = [];
  const delay = poseDefault<A>(pose, 'delayChildren', 0, props);
  const stagger = poseDefault<A>(pose, 'staggerChildren', 0, props);
  const staggerDirection = poseDefault<A>(pose, 'staggerDirection', 1, props);

  const maxStaggerDuration = (children.size - 1) * stagger;
  const generateStaggerDuration =
    staggerDirection === 1
      ? (i: number) => i * stagger
      : (i: number) => maxStaggerDuration - i * stagger;

  Array.from(children).forEach((child, i) => {
    animations.push(
      child.set(next, {
        ...props,
        delay: delay + generateStaggerDuration(i)
      })
    );
  });

  return animations;
};

const resolveTransition = <V, A>(
  transition: TransitionMap<A> | TransitionFactory<A>,
  key: string,
  value: V,
  props: Props,
  convertTransitionDefinition: ConvertTransitionDefinition<V, A>,
  getInstantTransition: GetInstantTransition<V, A>
): A => {
  let resolvedTransition: A | false | TransitionDefinition;

  /**
   * transition: () => {}<commit_msg>Don't override child props with parent props when starting child animations (#594)<commit_after>import {
  Props,
  Pose,
  PoserState,
  PoseMap,
  ChildPosers,
  GetTransitionProps,
  GetInstantTransition,
  StartAction,
  StopAction,
  ResolveTarget,
  TransformPose,
  AddTransitionDelay,
  ConvertTransitionDefinition,
  TransitionDefinition,
  TransitionFactory,
  TransitionMap,
  SetValue,
  SetValueNative,
  ApplyMap
} from '../types';
import { getPoseValues } from '../inc/selectors';
import { invariant } from 'hey-listen';

type AnimationsPromiseList = Array<Promise<any>>;

type SetterFactoryProps<V, A, C, P> = {
  state: PoserState<V, A, C, P>;
  poses: PoseMap<A>;
  getInstantTransition: GetInstantTransition<V, A>;
  startAction: StartAction<V, A, C>;
  stopAction: StopAction<C>;
  setValue: SetValue<V>;
  setValueNative: SetValueNative;
  addActionDelay: AddTransitionDelay<A>;
  getTransitionProps: GetTransitionProps<V>;
  resolveTarget: ResolveTarget<V>;
  convertTransitionDefinition: ConvertTransitionDefinition<V, A>;
  transformPose?: TransformPose<V, A, C, P>;
  posePriority?: string[];
};

export const resolveProp = (target: any, props: Props) =>
  typeof target === 'function' ? target(props) : target;

const poseDefault = <A>(
  pose: Pose<A>,
  prop: string,
  defaultValue: any,
  resolveProps: Props
) =>
  pose && pose[prop] !== undefined
    ? resolveProp(pose[prop], resolveProps)
    : defaultValue;

const startChildAnimations = <V, A, C, P>(
  children: ChildPosers<V, A, C, P>,
  next: string,
  pose: Pose<A>,
  props: Props
) => {
  const animations: Array<Promise<any>> = [];
  const delay = poseDefault<A>(pose, 'delayChildren', 0, props);
  const stagger = poseDefault<A>(pose, 'staggerChildren', 0, props);
  const staggerDirection = poseDefault<A>(pose, 'staggerDirection', 1, props);

  const maxStaggerDuration = (children.size - 1) * stagger;
  const generateStaggerDuration =
    staggerDirection === 1
      ? (i: number) => i * stagger
      : (i: number) => maxStaggerDuration - i * stagger;

  Array.from(children).forEach((child, i) => {
    animations.push(
      child.set(next, {
        delay: delay + generateStaggerDuration(i)
      })
    );
  });

  return animations;
};

const resolveTransition = <V, A>(
  transition: TransitionMap<A> | TransitionFactory<A>,
  key: string,
  value: V,
  props: Props,
  convertTransitionDefinition: ConvertTransitionDefinition<V, A>,
  getInstantTransition: GetInstantTransition<V, A>
): A => {
  let resolvedTransition: A | false | TransitionDefinition;

  /**
   * transition: () => {}
{'fraction_non_alphanumeric': 0.13470897410940239, 'fraction_numerical': 0.002530659918240218, 'mean_word_length': 4.501070663811563, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5384282', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1636, 'n_words': 400}