// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';

var Curry = require("bs-platform/lib/js/curry.js");
var Js_math = require("bs-platform/lib/js/js_math.js");
var Int$OptolithClient = require("../../Data/Int.bs.js");

function rollDie(sides) {
  return Int$OptolithClient.inc(Js_math.floor(Math.random() * sides));
}

function $neg$pipe(a, b) {
  if (a >= 0) {
    return a - b | 0;
  } else {
    return a + b | 0;
  }
}

function rollDiceAux(amount, sides) {
  if (Int$OptolithClient.abs(amount) <= 1) {
    return {
            hd: rollDie(sides),
            tl: /* [] */0
          };
  } else {
    return {
            hd: rollDie(sides),
            tl: rollDiceAux($neg$pipe(amount, 1), sides)
          };
  }
}

function rollDice(param) {
  return rollDiceAux(param.amount, param.sides);
}

function rollDiceSumAux(amount, sides) {
  if (Int$OptolithClient.abs(amount) <= 1) {
    return rollDie(sides);
  } else {
    return rollDie(sides) + rollDiceSumAux($neg$pipe(amount, 1), sides) | 0;
  }
}

function rollDiceSum(param) {
  return rollDiceSumAux(param.amount, param.sides);
}

function rollDiceSumMapAux(map, amount, sides) {
  if (Int$OptolithClient.abs(amount) <= 1) {
    return Curry._2(map, {
                amount: amount,
                sides: sides
              }, rollDie(sides));
  } else {
    return Curry._2(map, {
                amount: amount,
                sides: sides
              }, rollDie(sides)) + rollDiceSumMapAux(map, $neg$pipe(amount, 1), sides) | 0;
  }
}

function rollDiceSumMap(f, param) {
  return rollDiceSumMapAux(f, param.amount, param.sides);
}

exports.rollDice = rollDice;
exports.rollDiceSum = rollDiceSum;
exports.rollDiceSumMap = rollDiceSumMap;
/* No side effect */
