// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT

import type { CooldownDuration, DS4SpellDataSourceData } from "./spell-data-source";

export function calculateSpellPrice(data: DS4SpellDataSourceData): number | null {
  const spellPriceFactor = calculateSpellPriceFactor(data.cooldownDuration);
  const baseSpellPrices = [
    data.minimumLevels.healer !== null ? 10 + (data.minimumLevels.healer - 1) * 35 : null,
    data.minimumLevels.wizard !== null ? 10 + (data.minimumLevels.wizard - 1) * 50 : null,
    data.minimumLevels.sorcerer !== null ? 10 + (data.minimumLevels.sorcerer - 1) * 65 : null,
  ].filter((baseSpellPrice: number | null): baseSpellPrice is number => baseSpellPrice !== null);
  const baseSpellPrice = Math.min(...baseSpellPrices);
  return baseSpellPrice === Infinity ? null : baseSpellPrice * spellPriceFactor;
}

function calculateSpellPriceFactor(cooldownDuration: CooldownDuration): number {
  switch (cooldownDuration) {
    case "0r":
    case "1r":
    case "2r":
    case "5r":
    case "10r":
    case "100r":
      return 1;
    case "1d":
      return 2;
    case "d20d":
      return 3;
  }
}
