'use strict';
import type {
  AnimationConfigFunction,
  EntryAnimationsValues,
  EntryExitAnimationFunction,
  ExitAnimationsValues,
  IEntryAnimationBuilder,
  IEntryExitAnimationBuilder,
  IExitAnimationBuilder,
} from '../../commonTypes';
import type { BaseAnimationBuilder } from '../animationBuilder';
import { ComplexAnimationBuilder } from '../animationBuilder';
import type {
  Perspective,
  RotateX,
  RotateY,
  TransformsConfig,
  TranslateX,
  TranslateY,
} from './types';
import { animateTransformToValues, pickTransformValues } from './utils';

/**
 * Rotate from top on the X axis. You can modify the behavior by chaining
 * methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `entering` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipInXUp
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateX, TranslateY]>
  >
  implements IEntryAnimationBuilder
{
  static presetName = 'FlipInXUp';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipInXUp() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<EntryAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [
              { perspective: 500 },
              { rotateX: '90deg' },
              { translateY: -values.targetHeight },
            ],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateX: '0deg' }, { translateY: 0 }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Rotate from left on the Y axis. You can modify the behavior by chaining
 * methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `entering` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipInYLeft
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateY, TranslateX]>
  >
  implements IEntryAnimationBuilder
{
  static presetName = 'FlipInYLeft';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipInYLeft() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<EntryAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [
              { perspective: 500 },
              { rotateY: '-90deg' },
              { translateX: -values.targetWidth },
            ],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateY: '0deg' }, { translateX: 0 }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Rotate from bottom on the X axis. You can modify the behavior by chaining
 * methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `entering` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipInXDown
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateX, TranslateY]>
  >
  implements IEntryAnimationBuilder
{
  static presetName = 'FlipInXDown';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipInXDown() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<EntryAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [
              { perspective: 500 },
              { rotateX: '-90deg' },
              { translateY: values.targetHeight },
            ],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateX: '0deg' }, { translateY: 0 }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Rotate from right on the Y axis. You can modify the behavior by chaining
 * methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `entering` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipInYRight
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateY, TranslateX]>
  >
  implements IEntryAnimationBuilder
{
  static presetName = 'FlipInYRight';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipInYRight() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<EntryAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [
              { perspective: 500 },
              { rotateY: '90deg' },
              { translateX: values.targetWidth },
            ],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateY: '0deg' }, { translateX: 0 }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Eased rotate in on the X axis. You can modify the behavior by chaining
 * methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `entering` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipInEasyX
  extends ComplexAnimationBuilder<TransformsConfig<[Perspective, RotateX]>>
  implements IEntryExitAnimationBuilder
{
  static presetName = 'FlipInEasyX';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipInEasyX() as InstanceType<T>;
  }

  build = (): EntryExitAnimationFunction => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return () => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateX: '90deg' }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateX: '0deg' }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Eased rotate in on the Y axis. You can modify the behavior by chaining
 * methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `entering` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipInEasyY
  extends ComplexAnimationBuilder<TransformsConfig<[Perspective, RotateY]>>
  implements IEntryExitAnimationBuilder
{
  static presetName = 'FlipInEasyY';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipInEasyY() as InstanceType<T>;
  }

  build = (): EntryExitAnimationFunction => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return () => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateY: '90deg' }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateY: '0deg' }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Rotate to top animation on the X axis. You can modify the behavior by
 * chaining methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `exiting` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipOutXUp
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateX, TranslateY]>
  >
  implements IExitAnimationBuilder
{
  static presetName = 'FlipOutXUp';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipOutXUp() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<ExitAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateX: '0deg' }, { translateY: 0 }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [
              { perspective: 500 },
              { rotateX: '90deg' },
              { translateY: -values.currentHeight },
            ],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Rotate to left on the Y axis. You can modify the behavior by chaining methods
 * like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `exiting` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipOutYLeft
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateY, TranslateX]>
  >
  implements IExitAnimationBuilder
{
  static presetName = 'FlipOutYLeft';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipOutYLeft() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<ExitAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateY: '0deg' }, { translateX: 0 }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [
              { perspective: 500 },
              { rotateY: '-90deg' },
              { translateX: -values.currentWidth },
            ],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Rotate to bottom on the X axis. You can modify the behavior by chaining
 * methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `exiting` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipOutXDown
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateX, TranslateY]>
  >
  implements IExitAnimationBuilder
{
  static presetName = 'FlipOutXDown';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipOutXDown() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<ExitAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateX: '0deg' }, { translateY: 0 }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [
              { perspective: 500 },
              { rotateX: '-90deg' },
              { translateY: values.currentHeight },
            ],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Rotate to right animation on the Y axis. You can modify the behavior by
 * chaining methods like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `exiting` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipOutYRight
  extends ComplexAnimationBuilder<
    TransformsConfig<[Perspective, RotateY, TranslateX]>
  >
  implements IExitAnimationBuilder
{
  static presetName = 'FlipOutYRight';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipOutYRight() as InstanceType<T>;
  }

  build = (): AnimationConfigFunction<ExitAnimationsValues> => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return (values) => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateY: '0deg' }, { translateX: 0 }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [
              { perspective: 500 },
              { rotateY: '90deg' },
              { translateX: values.currentWidth },
            ],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Eased rotate on the X axis. You can modify the behavior by chaining methods
 * like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `exiting` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipOutEasyX
  extends ComplexAnimationBuilder<TransformsConfig<[Perspective, RotateX]>>
  implements IEntryExitAnimationBuilder
{
  static presetName = 'FlipOutEasyX';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipOutEasyX() as InstanceType<T>;
  }

  build = (): EntryExitAnimationFunction => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return () => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateX: '0deg' }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateX: '90deg' }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}

/**
 * Eased rotate on the Y axis. You can modify the behavior by chaining methods
 * like `.springify()` or `.duration(500)`.
 *
 * You pass it to the `exiting` prop on [an Animated
 * component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
 *
 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations#flip
 */
export class FlipOutEasyY
  extends ComplexAnimationBuilder<TransformsConfig<[Perspective, RotateY]>>
  implements IEntryExitAnimationBuilder
{
  static presetName = 'FlipOutEasyY';

  static createInstance<T extends typeof BaseAnimationBuilder>(
    this: T
  ): InstanceType<T> {
    return new FlipOutEasyY() as InstanceType<T>;
  }

  build = (): EntryExitAnimationFunction => {
    const delayFunction = this.getDelayFunction();
    const animationAndConfig = this.getAnimationAndConfig();
    const delay = this.getDelay();
    const callback = this.callbackV;
    const initialValues = this.initialValues;
    const targetValues = this.targetValues;

    return () => {
      'worklet';
      return {
        initialValues: {
          transform: pickTransformValues(
            [{ perspective: 500 }, { rotateY: '0deg' }],
            initialValues
          ),
        },
        animations: {
          transform: animateTransformToValues(
            [{ perspective: 500 }, { rotateY: '90deg' }],
            targetValues,
            animationAndConfig,
            delayFunction,
            delay
          ),
        },
        callback,
      };
    };
  };
}
