import { Entity, Player } from "@minecraft/server";
import { LOG_MEMORY_PROPERTY_READS, LOG_MEMORY_PROPERTY_WRITES } from "../FSConstants";
import FSUtil from "../FSUtil";
export default class FSMemoryProperty {
    constructor() {
        this.inMemoryData = [];
    }
    static sharedInstance() {
        if (!FSMemoryProperty.instance) {
            FSMemoryProperty.instance = new FSMemoryProperty();
        }
        return FSMemoryProperty.instance;
    }
    static getInMemoryEntityId(entity) {
        return entity instanceof Entity ? entity.id : entity instanceof Player ? entity.id : "world";
    }
    static clearInMemoryData(entity) {
        let entityId = FSMemoryProperty.getInMemoryEntityId(entity);
        FSMemoryProperty.sharedInstance().inMemoryData = FSMemoryProperty.sharedInstance().inMemoryData.filter(data => data.entityId !== entityId);
    }
    static clearInMemoryDataProperty(entity, property) {
        let entityId = FSMemoryProperty.getInMemoryEntityId(entity);
        let inMemoryData = FSMemoryProperty.sharedInstance().inMemoryData.find(data => data.entityId === entityId);
        if (!inMemoryData) {
            return;
        }
        delete inMemoryData.properties[FSUtil.namespacedString(property, "_")];
    }
    static setInMemoryData(entity, property, value) {
        let entityId = FSMemoryProperty.getInMemoryEntityId(entity);
        let inMemoryData = FSMemoryProperty.sharedInstance().inMemoryData.find(data => data.entityId === entityId);
        if (!inMemoryData) {
            inMemoryData = {
                entityId: entityId,
                properties: {}
            };
            FSMemoryProperty.sharedInstance().inMemoryData.push(inMemoryData);
        }
        if (LOG_MEMORY_PROPERTY_WRITES) {
            console.error(`Setting memory property: ${FSUtil.namespacedString(property, "_")} = ${value}`);
        }
        inMemoryData.properties[FSUtil.namespacedString(property, "_")] = value;
    }
    static getInMemoryData(entity, property) {
        let entityId = FSMemoryProperty.getInMemoryEntityId(entity);
        let inMemoryData = FSMemoryProperty.sharedInstance().inMemoryData.find(data => data.entityId === entityId);
        if (!inMemoryData) {
            return undefined;
        }
        if (LOG_MEMORY_PROPERTY_READS) {
            console.error(`Reading memory property: ${FSUtil.namespacedString(property, "_")} = ${inMemoryData.properties[FSUtil.namespacedString(property, "_")]}`);
        }
        return inMemoryData.properties[FSUtil.namespacedString(property, "_")];
    }
    static clear(entity) {
        FSMemoryProperty.clearInMemoryData(entity);
    }
    static clearProperty(entity, property) {
        FSMemoryProperty.clearInMemoryDataProperty(entity, property);
    }
    static setBoolean(entity, property, boolean, noopIfSame = true) {
        if (noopIfSame && FSMemoryProperty.getBoolean(entity, property) === boolean) {
            return;
        }
        FSMemoryProperty.setInMemoryData(entity, property, boolean);
    }
    static getBoolean(entity, property) {
        return FSMemoryProperty.getInMemoryData(entity, property) || false;
    }
    static setNumber(entity, property, number, noopIfSame = true) {
        if (noopIfSame && FSMemoryProperty.getNumber(entity, property) === number) {
            return;
        }
        FSMemoryProperty.setInMemoryData(entity, property, number);
    }
    static getNumber(entity, property) {
        return FSMemoryProperty.getInMemoryData(entity, property) || 0;
    }
    static incrementNumber(entity, property, increment) {
        let number = FSMemoryProperty.getInMemoryData(entity, property) || 0;
        if (isNaN(number)) {
            number = 0;
        }
        if (isNaN(increment) || increment === 0) {
            return;
        }
        FSMemoryProperty.setInMemoryData(entity, property, number + increment);
    }
    static setString(entity, property, string, noopIfSame = true) {
        if (noopIfSame && FSMemoryProperty.getString(entity, property) === string) {
            return;
        }
        FSMemoryProperty.setInMemoryData(entity, property, string);
    }
    static getString(entity, property) {
        return FSMemoryProperty.getInMemoryData(entity, property) || "";
    }
    static setVector3(entity, property, vector3, noopIfSame = true) {
        if (noopIfSame && FSMemoryProperty.getVector3(entity, property) === vector3) {
            return;
        }
        FSMemoryProperty.setInMemoryData(entity, property, vector3);
    }
    static getVector3(entity, property) {
        return FSMemoryProperty.getInMemoryData(entity, property);
    }
}

//# sourceMappingURL=../../../../../_SBUNK_BPDebug/fs_sbunk/fs/libs/FSMemoryProperty.js.map
