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

import { getSceneMigrator, migrateCollection, migrateCompendiums, getCompendiumMigrator } from "./migrationHelpers.js";

/** @type {import("./migration.js").Migration["migrate"]} */
async function migrate() {
  const actorsResult = await migrateCollection(game.actors, migrateActor);
  const scenesResult = await migrateCollection(game.scenes, migrateScene);
  const compendiumsResult = await migrateCompendiums(migrateCompendium);
  return actorsResult === "error" || scenesResult === "error" || compendiumsResult === "error" ? "error" : "success";
}

/** @type {import('./migrationHelpers.js').Migrator<Actor>} */
async function migrateActor(actor) {
  await actor.update({
    system: {
      combatValues: [
        "hitPoints",
        "defense",
        "initiative",
        "movement",
        "meleeAttack",
        "rangedAttack",
        "spellcasting",
        "targetedSpellcasting",
      ].reduce((acc, curr) => {
        acc[curr] = { "-=base": null };
        return acc;
      }, {}),
    },
  });
}

const migrateScene = getSceneMigrator(migrateActor);

const migrateCompendium = getCompendiumMigrator({ migrateActor, migrateScene });

/** @type {import("./migration.js").Migration} */
export const migration = {
  migrate,
  migrateCompendium,
};
