// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import "dart:_error_utils";
import "dart:_internal" show mix64, patch, unsafeCast;
import "dart:_wasm";

/// There are no parts of this patch library.

@patch
@pragma('wasm:prefer-inline')
T min<T extends num>(T a, T b) {
  if (a is int && b is int) return unsafeCast<T>((a as int).minS(b));
  if (a is double && b is double) return unsafeCast<T>((a as double).min(b));
  return _minSlow<T>(a, b);
}

@patch
@pragma('wasm:prefer-inline')
T max<T extends num>(T a, T b) {
  if (a is int && b is int) return unsafeCast<T>((a as int).maxS(b));
  if (a is double && b is double) return unsafeCast<T>((a as double).max(b));
  return _maxSlow<T>(a, b);
}

T _minSlow<T extends num>(T a, T b) {
  if (a > b) return b;
  if (a < b) return a;
  if (b is double) {
    if (a == 0 && b.isNegative || b.isNaN) return b;
    return a;
  }
  return a;
}

T _maxSlow<T extends num>(T a, T b) {
  if (a > b) return a;
  if (a < b) return b;
  if (b is double) {
    if (b.isNaN) return b;
    return a;
  }
  if (b == 0 && a.isNegative) return b;
  return a;
}

// If [x] is an [int] and [exponent] is a non-negative [int], the result is
// an [int], otherwise the result is a [double].
@patch
num pow(num x, num exponent) {
  if ((x is int) && (exponent is int) && (exponent >= 0)) {
    return _intPow(x, exponent);
  }

  double xDouble = x.toDouble();

  if (xDouble == 1.0) {
    return 1.0;
  }

  double exponentDouble = exponent.toDouble();

  if (xDouble == -1.0 && exponent.isInfinite) {
    return 1.0;
  }

  return _doublePow(xDouble, exponentDouble);
}

int _intPow(int base, int exponent) {
  // Exponentiation by squaring.
  int result = 1;
  while (exponent != 0) {
    if ((exponent & 1) == 1) {
      result *= base;
    }
    exponent >>= 1;
    // Skip unnecessary operation (can overflow to Mint).
    if (exponent != 0) {
      base *= base;
    }
  }
  return result;
}

// TODO(iposva): Handle patch methods within a patch class correctly.
@patch
class Random {
  static final Random _secureRandom = _SecureRandom();

  @patch
  factory Random([int? seed]) {
    var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed);
    // Crank a couple of times to distribute the seed bits a bit further.
    return _Random._withState(state)
      .._nextState()
      .._nextState()
      .._nextState()
      .._nextState();
  }

  @patch
  factory Random.secure() => _secureRandom;
}

class _Random implements Random {
  // Internal state of the random number generator.
  int _state;

  int get _stateLow => _state & 0xFFFFFFFF;
  int get _stateHigh => _state >>> 32;

  _Random._withState(this._state);

  // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32.
  // http://en.wikipedia.org/wiki/Multiply-with-carry
  // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1.

  // Implements:
  //   const _A = 0xffffda61;
  //   var state =
  //       ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1);
  //   _state[_kSTATE_LO] = state & ((1 << 32) - 1);
  //   _state[_kSTATE_HI] = state >> 32;
  // This is a native to prevent 64-bit operations in Dart, which
  // fail with --throw_on_javascript_int_overflow.
  // TODO(regis): Implement in Dart and remove Random_nextState in math.cc.
  void _nextState() {
    const _A = 0xffffda61;
    _state = _A * _stateLow + _stateHigh;
  }

  int nextInt(int max) {
    RangeErrorUtils.checkValueInInterval(
      max,
      1,
      _POW2_32,
      "max",
      "Must be positive and <= 2^32",
    );
    if ((max & -max) == max) {
      // Fast case for powers of two.
      _nextState();
      return _state & (max - 1);
    }

    int rnd32;
    int result;
    do {
      _nextState();
      rnd32 = _stateLow;
      result = rnd32 % max;
    } while ((rnd32 - result + max) > _POW2_32);
    return result;
  }

  double nextDouble() {
    return ((nextInt(1 << 26) * _POW2_27_D) + nextInt(1 << 27)) / _POW2_53_D;
  }

  bool nextBool() {
    return nextInt(2) == 0;
  }

  // Constants used by the algorithm.
  static const _POW2_32 = 1 << 32;
  static const _POW2_53_D = 1.0 * (1 << 53);
  static const _POW2_27_D = 1.0 * (1 << 27);

  // Use a singleton Random object to get a new seed if no seed was passed.
  static final _prng = _Random._withState(_initialSeed());

  static int _setupSeed(int seed) => mix64(seed);

  external static int _initialSeed();

  static int _nextSeed() {
    // Trigger the PRNG once to change the internal state.
    _prng._nextState();
    return _prng._stateLow;
  }
}
