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

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

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

/** @type {import('./migrationHelpers.js').Migrator<Item>} */
async function migrateItem(item) {
  if (item.type === "spell") {
    await item.update({ system: { allowsDefense: false } });
  }
}

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

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