/**
 * move to serverside earliest past 2023‑07‑03
 */

import SavestateConverter from "/GameTrackerJS/savestate/SavestateConverter.js";

const URL = import.meta.url;
const VER_REGEX = /([0-9]+)\.js$/;

// LOAD PREVIOUS CONVERTER
const FORMER_CONVERTER_URL = URL.replace(VER_REGEX, (_, ver) => `${parseInt(ver) - 1}.js`);
await import(FORMER_CONVERTER_URL);

// REGISTER CONVERTER
SavestateConverter.register(function(state) {
    state = state ?? {};

    // remove read of altered data
    const res = {
        meta: state.meta ?? {},
        data: {
            // GameTrackerJS
            "": state.data?.[""] ?? {},
            items: state.data?.items ?? {},
            locations: state.data?.locations ?? {},
            exitBindings: {},
            areaHints: state.data?.areaHints ?? {},
            locationItems: state.data?.locationItems ?? {},
            startItems: state.data?.startItems ?? {},
            options: state.data?.options ?? {},
            filter: state.data?.filter ?? {},
            // Track-OOT
            dungeonRewards: state.data?.dungeonRewards ?? {},
            dungeonTypes: state.data?.dungeonTypes ?? {},
            shopItems: state.data?.shopItems ?? {},
            shopItemsPrice: state.data?.shopItemsPrice ?? {},
            shopItemsBought: state.data?.shopItemsBought ?? {},
            shopItemsName: state.data?.shopItemsName ?? {},
            songNotes: state.data?.songNotes ?? {},
            gossipstoneLocations: state.data?.gossipstoneLocations ?? {},
            gossipstoneItems: state.data?.gossipstoneItems ?? {},
            parseSpoiler: state.data?.parseSpoiler ?? {}
        },
        notes: state.notes ?? "",
        autosave: state.autosave ?? false,
        timestamp: state.timestamp ?? new Date(),
        name: state.name ?? ""
    };

    // exitBindings

    for (const [key, value] of Object.entries(state.data?.exitBindings ?? {})) {
        const transKey = key.split(" -> ").map((e) => TRANSLATION[e] ?? e).join(" -> ");
        const transValue = value?.split(" -> ").map((e) => TRANSLATION[e] ?? e).join(" -> ");
        res.data.exitBindings[transKey] = transValue;
    }

    return res;
});

const TRANSLATION = {
    // dungeons
    "bottom_of_the_well_gateway": "well_kakriko_gateway",
    "bottom_of_the_well_entrance": "kakariko_well_gateway",
    "deku_tree_gateway": "deku_kokiri_gateway",
    "deku_tree_entrance": "kokiri_deku_gateway",
    "dodongos_cavern_gateway": "dodongo_mountain_gateway",
    "dodongos_cavern_entrance": "mountain_dodongo_gateway",
    "jabu_jabus_belly_gateway": "jabu_fountain_gateway",
    "jabu_jabus_belly_entrance": "fountain_jabu_gateway",
    "ice_cavern_gateway": "ice_cavern_fountain_gateway",
    "ice_cavern_entrance": "fountain_ice_cavern_gateway",
    "gerudo_training_grounds_gateway": "training_grounds_fortress_gateway",
    "gerudo_training_grounds_entrance": "fortress_training_grounds_gateway",
    "forest_temple_gateway": "forest_temple_meadow_gateway",
    "forest_temple_entrance": "meadow_forest_temple_gateway",
    "fire_temple_gateway": "fire_temple_crater_gateway",
    "fire_temple_entrance": "crater_fire_temple_gateway",
    "water_temple_gateway": "water_temple_lake_gateway",
    "water_temple_entrance": "lake_water_temple_gateway",
    "shadow_temple_gateway": "shadow_temple_graveyard_gateway",
    "shadow_temple_entrance": "graveyard_shadow_temple_gateway",
    "spirit_temple_gateway": "spirit_temple_colossus_gateway",
    "spirit_temple_entrance": "colossus_spirit_temple_gateway",
    // grottos
    "hf_tektite_grotto_entrance": "field_tektite_grotto_gateway",
    "hf_tektite_grotto": "tektite_grotto_field_gateway",
    "hf_near_kak_grotto_entrance": "field_near_kak_grotto_gateway",
    "hf_near_kak_grotto": "near_kak_grotto_field_gateway",
    "hf_fairy_grotto_entrance": "field_fairy_grotto_gateway",
    "hf_fairy_grotto": "fairy_grotto_field_gateway",
    "hf_near_market_grotto_entrance": "field_near_market_grotto_gateway",
    "hf_near_market_grotto": "near_market_grotto_field_gateway",
    "hf_cow_grotto_entrance": "field_cow_grotto_gateway",
    "hf_cow_grotto": "cow_grotto_field_gateway",
    "hf_inside_fence_grotto_entrance": "field_inside_fence_grotto_gateway",
    "hf_inside_fence_grotto": "inside_fence_grotto_field_gateway",
    "hf_open_grotto_entrance": "field_open_grotto_gateway",
    "hf_open_grotto": "open_grotto_field_gateway",
    "hf_southeast_grotto_entrance": "field_southeast_grotto_gateway",
    "hf_southeast_grotto": "southeast_grotto_field_gateway",
    "kf_storms_grotto_entrance": "kokiri_storms_grotto_gateway",
    "kf_storms_grotto": "storms_grotto_kokiri_gateway",
    "lw_near_shortcuts_grotto_entrance": "woods_near_shortcuts_grotto_gateway",
    "lw_near_shortcuts_grotto": "near_shortcuts_grotto_woods_gateway",
    "lw_scrubs_grotto_entrance": "woods_scrubs_grotto_gateway",
    "lw_scrubs_grotto": "near_scrubs_woods_gateway",
    "deku_theater_entrance": "woods_deku_theater_gateway",
    "deku_theater": "deku_theater_woods_gateway",
    "sfm_wolfos_grotto_entrance": "meadow_wolfos_grotto_gateway",
    "sfm_wolfos_grotto": "wolfos_grotto_meadow_gateway",
    "sfm_storms_grotto_entrance": "meadow_storms_grotto_gateway",
    "sfm_storms_grotto": "storms_grotto_meadow_gateway",
    "sfm_fairy_grotto_entrance": "meadow_fairy_grotto_gateway",
    "sfm_fairy_grotto": "fairy_grotto_meadow_gateway",
    "llr_grotto_entrance": "ranch_grotto_gateway",
    "llr_grotto": "grotto_ranch_gateway",
    "hc_storms_grotto_entrance": "grounds_storms_grotto_gateway",
    "hc_storms_grotto": "storms_grotto_grounds_gateway",
    "kak_open_grotto_entrance": "kakariko_open_grotto_gateway",
    "kak_open_grotto": "open_grotto_kakariko_gateway",
    "kak_redead_grotto_entrance": "kakariko_redead_grotto_gateway",
    "kak_redead_grotto": "redead_grotto_kakariko_gateway",
    "graveyard_shield_grave_entrance": "graveyard_shield_grave_gateway",
    "graveyard_shield_grave": "shield_grave_graveyard_gateway",
    "graveyard_heart_piece_grave_entrance": "graveyard_redead_grave_gateway",
    "graveyard_heart_piece_grave": "redead_grave_graveyard_gateway",
    "graveyard_composers_grave_entrance": "graveyard_composers_grave_gateway",
    "graveyard_royal_familys_tomb": "composers_grave_graveyard_gateway",
    "graveyard_dampes_grave_entrance": "graveyard_dampes_grave_gateway",
    "graveyard_dampes_grave": "dampes_grave_graveyard_gateway",
    "dmt_storms_grotto_entrance": "mountain_storms_grotto_gateway",
    "dmt_storms_grotto": "storms_grotto_mountain_gateway",
    "dmt_cow_grotto_entrance": "mountain_cow_grotto_gateway",
    "dmt_cow_grotto": "cow_grotto_mountain_gateway",
    "dmc_hammer_grotto_entrance": "crater_hammer_grotto_gateway",
    "dmc_hammer_grotto": "hammer_grotto_crater_gateway",
    "dmc_upper_grotto_entrance": "crater_bomb_grotto_gateway",
    "dmc_upper_grotto": "bomb_grotto_crater_gateway",
    "gc_grotto_entrance": "goron_grotto_gateway",
    "gc_grotto": "grotto_goron_gateway",
    "zr_storms_grotto_entrance": "river_storms_grotto_gateway",
    "zr_storms_grotto": "storms_grotto_river_gateway",
    "zr_fairy_grotto_entrance": "river_fairy_grotto_gateway",
    "zr_fairy_grotto": "fairy_grotto_river_gateway",
    "zr_open_grotto_entrance": "river_open_grotto_gateway",
    "zr_open_grotto": "open_grotto_river_gateway",
    "zd_storms_grotto_entrance": "domain_storms_grotto_gateway",
    "zd_storms_grotto": "storms_grotto_domain_gateway",
    "lh_grotto_entrance": "lake_grotto_gateway",
    "lh_grotto": "grotto_lake_gateway",
    "gv_storms_grotto_entrance": "valley_storms_grotto_gateway",
    "gv_storms_grotto": "storms_grotto_valley_gateway",
    "gv_octorok_grotto_entrance": "valley_octorok_grotto_gateway",
    "gv_octorok_grotto": "octorok_grotto_valley_gateway",
    "gf_storms_grotto_entrance": "fortress_storms_grotto_gateway",
    "gf_storms_grotto": "storms_grotto_fortress_gateway",
    "colossus_grotto_entrance": "colossus_grotto_gateway",
    "colossus_grotto": "grotto_colossus_gateway",
    // interiors
    "kf_midos_house_entrance": "kokiri_midos_house_gateway",
    "kf_midos_house": "midos_house_kokiri_gateway",
    "kf_sarias_house_entrance": "kokiri_sarias_house_gateway",
    "kf_sarias_house": "sarias_house_kokiri_gateway",
    "kf_house_of_twins_entrance": "kokiri_house_of_twins_gateway",
    "kf_house_of_twins": "house_of_twins_kokiri_gateway",
    "kf_know_it_all_house_entrance": "kokiri_know_it_all_house_gateway",
    "kf_know_it_all_house": "know_it_all_house_kokiri_gateway",
    "kf_kokiri_shop_entrance": "kokiri_kokiri_shop_gateway",
    "kf_kokiri_shop": "kokiri_shop_kokiri_gateway",
    "kf_links_house_entrance": "kokiri_links_house_gateway",
    "kf_links_house": "links_house_kokiri_gateway",
    "market_guard_house_entrance": "market_guard_house_gateway",
    "market_guard_house": "guard_house_market_gateway",
    "market_mask_shop_entrance": "market_mask_shop_gateway",
    "market_mask_shop": "mask_shop_market_gateway",
    "market_bombchu_bowling_entrance": "market_bombchu_bowling_gateway",
    "market_bombchu_bowling": "bombchu_bowling_market_gateway",
    "market_potion_shop_entrance": "market_potion_shop_gateway",
    "market_potion_shop": "potion_shop_market_gateway",
    "market_treasure_chest_game_entrance": "market_treasure_chest_game_gateway",
    "market_treasure_chest_game": "treasure_chest_game_market_gateway",
    "market_bombchu_shop_entrance": "market_bombchu_shop_gateway",
    "market_bombchu_shop": "bombchu_shop_market_gateway",
    "market_man_in_green_house_entrance": "market_back_house_gateway",
    "market_man_in_green_house": "back_house_market_gateway",
    "market_bazaar_entrance": "market_bazaar_gateway",
    "market_bazaar": "bazaar_market_gateway",
    "market_shooting_gallery_entrance": "market_shooting_gallery_gateway",
    "market_shooting_gallery": "shooting_gallery_market_gateway",
    "llr_talons_house_entrance": "ranch_talons_house_gateway",
    "llr_talons_house": "talons_house_ranch_gateway",
    "llr_stables_entrance": "ranch_stables_gateway",
    "llr_stables": "stables_ranch_gateway",
    "llr_tower_entrance": "ranch_tower_gateway",
    "llr_tower": "tower_ranch_gateway",
    "hc_great_fairy_fountain_entrance": "grounds_great_fairy_gateway",
    "hc_great_fairy_fountain": "great_fairy_grounds_gateway",
    "ogc_great_fairy_fountain_entrance": "ganon_grounds_great_fairy_gateway",
    "ogc_great_fairy_fountain": "great_fairy_ganon_grounds_gateway",
    "kak_carpenter_boss_house_entrance": "kakariko_carpenter_house_gateway",
    "kak_carpenter_boss_house": "carpenter_house_kakariko_gateway",
    "kak_house_of_skulltula_entrance": "kakariko_skulltula_house_gateway",
    "kak_house_of_skulltula": "skulltula_house_kakariko_gateway",
    "kak_impas_house_entrance": "kakariko_impas_house_gateway",
    "kak_impas_house": "impas_house_kakariko_gateway",
    "kak_impas_house_back_entrance": "kakariko_impas_back_gateway",
    "kak_impas_house_back": "impas_back_kakariko_gateway",
    "kak_odd_medicine_building_entrance": "kakariko_medicine_building_gateway",
    "kak_odd_medicine_building": "medicine_building_kakariko_gateway",
    "kak_bazaar_entrance": "kakariko_bazaar_gateway",
    "kak_bazaar": "bazaar_kakariko_gateway",
    "kak_shooting_gallery_entrance": "kakariko_shooting_gallery_gateway",
    "kak_shooting_gallery": "shooting_gallery_kakariko_gateway",
    "kak_windmill_entrance": "kakariko_windmill_gateway",
    "kak_windmill": "windmill_kakariko_gateway",
    "kak_potion_shop_front_entrance": "kakariko_potion_shop_gateway",
    "kak_potion_shop_front": "potion_shop_kakariko_gateway",
    "kak_potion_shop_back_entrance": "kakariko_potion_back_gateway",
    "kak_potion_shop_back": "potion_back_kakariko_gateway",
    "graveyard_dampes_house_entrance": "graveyard_dampes_house_gateway",
    "graveyard_dampes_house": "dampes_house_graveyard_gateway",
    "dmt_great_fairy_fountain_entrance": "mountain_great_fairy_gateway",
    "dmt_great_fairy_fountain": "great_fairy_mountain_gateway",
    "dmc_great_fairy_fountain_entrance": "crater_great_fairy_gateway",
    "dmc_great_fairy_fountain": "great_fairy_crater_gateway",
    "gc_shop_entrance": "goron_shop_gateway",
    "gc_shop": "shop_goron_gateway",
    "zd_shop_entrance": "zora_shop_gateway",
    "zd_shop": "shop_zora_gateway",
    "zf_great_fairy_fountain_entrance": "fountain_great_fairy_gateway",
    "zf_great_fairy_fountain": "great_fairy_fountain_gateway",
    "lh_lab_entrance": "lake_lab_gateway",
    "lh_lab": "lab_lake_gateway",
    "lh_fishing_hole_entrance": "lake_fishing_hole_gateway",
    "lh_fishing_hole": "fishing_hole_lake_gateway",
    "gv_carpenter_tent_entrance": "valley_carpenter_tent_gateway",
    "gv_carpenter_tent": "carpenter_tent_valley_gateway",
    "colossus_great_fairy_fountain_entrance": "colossus_great_fairy_gateway",
    "colossus_great_fairy_fountain": "great_fairy_colossus_gateway"
};
