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

var Curry = require("bs-platform/lib/js/curry.js");
var IC$OptolithClient = require("./IC.bs.js");
var Int$OptolithClient = require("../../Data/Int.bs.js");
var Maybe$OptolithClient = require("../../Data/Maybe.bs.js");
var IntMap$OptolithClient = require("../../Data/IntMap.bs.js");
var Function$OptolithClient = require("../../Data/Function.bs.js");
var Traditions$OptolithClient = require("./Traditions.bs.js");

function getMissingAp(isInCharacterCreation, apAvailable, apCost) {
  if (apCost > 0 && !isInCharacterCreation && apCost > apAvailable) {
    return /* Just */{
            _0: apCost - apAvailable | 0
          };
  } else {
    return /* Nothing */0;
  }
}

function getDisAdvantagesSubtypeMax(staticData, hero, isMagical) {
  if (isMagical) {
    return Maybe$OptolithClient.maybe(50, (function (param) {
                  if (param[2].isDisAdvAPMaxHalved) {
                    return 25;
                  } else {
                    return 50;
                  }
                }), Maybe$OptolithClient.listToMaybe(Traditions$OptolithClient.Magical.getEntries(staticData, hero.specialAbilities)));
  } else {
    return 50;
  }
}

function getDisAdvantageSubtypeApSpent(apCategories, isDisadvantage, isMagical, isBlessed) {
  if (isDisadvantage) {
    if (isMagical) {
      return /* Just */{
              _0: apCategories.spentOnMagicalDisadvantages
            };
    } else if (isBlessed) {
      return /* Just */{
              _0: apCategories.spentOnBlessedDisadvantages
            };
    } else {
      return /* Nothing */0;
    }
  } else if (isMagical) {
    return /* Just */{
            _0: apCategories.spentOnMagicalAdvantages
          };
  } else if (isBlessed) {
    return /* Just */{
            _0: apCategories.spentOnBlessedAdvantages
          };
  } else {
    return /* Nothing */0;
  }
}

function getMissingApForDisAdvantage(staticData, hero, isInCharacterCreation, apCategories, isMagical, isBlessed, apCost, staticEntry) {
  var isDisadvantage;
  isDisadvantage = staticEntry.TAG ? true : false;
  var noMaxAPInfluence = staticEntry._0.noMaxAPInfluence;
  var apSpent = isDisadvantage ? apCategories.spentOnDisadvantages : apCategories.spentOnAdvantages;
  var mApSpentSubtype = getDisAdvantageSubtypeApSpent(apCategories, isDisadvantage, isMagical, isBlessed);
  var subtypeMax = getDisAdvantagesSubtypeMax(staticData, hero, isMagical);
  var absoluteApCost = Int$OptolithClient.abs(apCost);
  var missingApForSubtype = isInCharacterCreation && !noMaxAPInfluence ? Maybe$OptolithClient.Monad.$great$great$eq(mApSpentSubtype, (function (apSpentSubtype) {
            return Maybe$OptolithClient.ensure((function (param) {
                          return 0 < param;
                        }), (apSpentSubtype + absoluteApCost | 0) - subtypeMax | 0);
          })) : /* Nothing */0;
  var missingApForMain = isInCharacterCreation && !noMaxAPInfluence ? Maybe$OptolithClient.ensure((function (param) {
            return 0 < param;
          }), (apSpent + absoluteApCost | 0) - 80 | 0) : /* Nothing */0;
  var missingApForTotal = getMissingAp(isInCharacterCreation, apCategories.available, apCost);
  return {
          totalMissing: missingApForTotal,
          mainMissing: missingApForMain,
          subMissing: missingApForSubtype
        };
}

var getApSpentOnAttributes = Curry._2(IntMap$OptolithClient.Foldable.foldr, (function (x) {
        var partial_arg = IC$OptolithClient.getAPForRange(/* E */4, 8, x.value);
        return function (param) {
          return partial_arg + param | 0;
        };
      }), 0);

function getApSpentOnSkills(staticData) {
  return Curry._2(IntMap$OptolithClient.Foldable.foldr, (function (x) {
                return Maybe$OptolithClient.maybe(Function$OptolithClient.id, (function (staticEntry) {
                              var partial_arg = IC$OptolithClient.getAPForRange(staticEntry.ic, 0, x.value);
                              return function (param) {
                                return partial_arg + param | 0;
                              };
                            }), Curry._2(IntMap$OptolithClient.lookup, x.id, staticData.skills));
              }), 0);
}

function getApSpentOnCombatTechniques(staticData) {
  return Curry._2(IntMap$OptolithClient.Foldable.foldr, (function (x) {
                return Maybe$OptolithClient.maybe(Function$OptolithClient.id, (function (staticEntry) {
                              var partial_arg = IC$OptolithClient.getAPForRange(staticEntry.ic, 6, x.value);
                              return function (param) {
                                return partial_arg + param | 0;
                              };
                            }), Curry._2(IntMap$OptolithClient.lookup, x.id, staticData.combatTechniques));
              }), 0);
}

function getApSpentOnSpells(staticData) {
  return Curry._2(IntMap$OptolithClient.Foldable.foldr, (function (x) {
                var value = x.value;
                if (!value) {
                  return Function$OptolithClient.id;
                }
                var value$1 = value._0;
                return Maybe$OptolithClient.maybe(Function$OptolithClient.id, (function (staticEntry) {
                              var partial_arg = IC$OptolithClient.getAPForRange(staticEntry.ic, 0, value$1);
                              return function (param) {
                                return partial_arg + param | 0;
                              };
                            }), Curry._2(IntMap$OptolithClient.lookup, x.id, staticData.spells));
              }), 0);
}

function getApSpentOnLiturgicalChants(staticData) {
  return Curry._2(IntMap$OptolithClient.Foldable.foldr, (function (x) {
                var value = x.value;
                if (!value) {
                  return Function$OptolithClient.id;
                }
                var value$1 = value._0;
                return Maybe$OptolithClient.maybe(Function$OptolithClient.id, (function (staticEntry) {
                              var partial_arg = IC$OptolithClient.getAPForRange(staticEntry.ic, 0, value$1);
                              return function (param) {
                                return partial_arg + param | 0;
                              };
                            }), Curry._2(IntMap$OptolithClient.lookup, x.id, staticData.liturgicalChants));
              }), 0);
}

var Sum = {
  getApSpentOnAttributes: getApSpentOnAttributes,
  getApSpentOnSkills: getApSpentOnSkills,
  getApSpentOnCombatTechniques: getApSpentOnCombatTechniques,
  getApSpentOnSpells: getApSpentOnSpells,
  getApSpentOnLiturgicalChants: getApSpentOnLiturgicalChants,
  getApSpentOnCantrips: IntMap$OptolithClient.size,
  getAPSpentOnBlessings: IntMap$OptolithClient.size
};

exports.getMissingAp = getMissingAp;
exports.getDisAdvantagesSubtypeMax = getDisAdvantagesSubtypeMax;
exports.getMissingApForDisAdvantage = getMissingApForDisAdvantage;
exports.Sum = Sum;
/* getApSpentOnAttributes Not a pure module */
