import { world } from "@minecraft/server";
import Buffer from "../../_Utilities/Buffer";
import Vector from "../../_Utilities/Vector";
import Actor_Manager from "../Actor_Manager";
import { actor_template_mascot_biome_default } from "../Data/Templates/Quadruped/actor_template_mascot_biome_default";
import { actor_template_mascot_biome_nether } from "../Data/Templates/Quadruped/actor_template_mascot_biome_nether";
import { actor_template_mascot_biome_desert } from "../Data/Templates/Quadruped/actor_template_mascot_biome_desert";
import { actor_template_mascot_biome_forest } from "../Data/Templates/Quadruped/actor_template_mascot_biome_forest";
import { actor_template_mascot_biome_mesa } from "../Data/Templates/Quadruped/actor_template_mascot_biome_mesa";
import { actor_template_mascot_biome_mountain } from "../Data/Templates/Quadruped/actor_template_mascot_biome_mountain";
import { actor_template_mascot_biome_ocean } from "../Data/Templates/Quadruped/actor_template_mascot_biome_ocean";
import { actor_template_biome_nether } from "../Data/Templates/Humanoid/actor_template_biome_nether";
import { actor_template_biome_desert } from "../Data/Templates/Humanoid/actor_template_biome_desert";
import { actor_template_biome_mesa } from "../Data/Templates/Humanoid/actor_template_biome_mesa";
import { actor_template_biome_mountain } from "../Data/Templates/Humanoid/actor_template_biome_mountain";
import { actor_template_biome_forest } from "../Data/Templates/Humanoid/actor_template_biome_forest";
import { actor_template_biome_ocean } from "../Data/Templates/Humanoid/actor_template_biome_ocean";
export default class Actor_Persistent_Manager {
    static runtime(dimension_ID, location) {
        const actors = Buffer.get_Data_Array("118405c7-7a8f-4be4-9ab7-f8867ed02692");
        if (actors == undefined)
            return;
        const persistent = actors.filter((actor) => Vector.distance(actor.persistent.location, location) <= 64);
        const ids = new Array();
        for (let i = 0; i < persistent.length; i++) {
            if (persistent[i].entity == undefined)
                continue;
            ids.push(persistent[i].entity.id);
        }
        for (let i = 0; i < persistent.length; i++) {
            const entities = world.getDimension(dimension_ID).getEntitiesAtBlockLocation(persistent[i].persistent.location);
            if (entities.length == 0) {
                Actor_Manager.spawn(dimension_ID, persistent[i], persistent[i].persistent.location);
            }
            else {
                for (let j = 0; j < entities.length; j++) {
                    if (entities[j].typeId == "bgs:actor") {
                        const actor = Actor_Manager.get(entities[j].id);
                        if (actor == undefined) {
                            Actor_Manager.spawn(dimension_ID, persistent[i], persistent[i].persistent.location);
                            entities[j].remove();
                        }
                    }
                }
            }
        }
    }
    static set(template) {
        if (template.persistent == undefined)
            return;
        const buffer = Buffer.get("118405c7-7a8f-4be4-9ab7-f8867ed02692");
        const id = buffer == undefined ? 1 : buffer.size + 1;
        Buffer.set_Data("118405c7-7a8f-4be4-9ab7-f8867ed02692", String(id), template);
    }
    static initialize() {
        world.getDimension("overworld").runCommand("kill @e[type=bgs:actor]");
        Buffer.set("118405c7-7a8f-4be4-9ab7-f8867ed02692");
        this.set(actor_template_mascot_biome_default);
        this.set(actor_template_mascot_biome_desert);
        this.set(actor_template_mascot_biome_forest);
        this.set(actor_template_mascot_biome_mesa);
        this.set(actor_template_mascot_biome_mountain);
        this.set(actor_template_mascot_biome_nether);
        this.set(actor_template_mascot_biome_ocean);
        this.set(actor_template_biome_desert);
        this.set(actor_template_biome_forest);
        this.set(actor_template_biome_mesa);
        this.set(actor_template_biome_mountain);
        this.set(actor_template_biome_nether);
        this.set(actor_template_biome_ocean);
    }
}
