"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const Database_1 = require("./Database");
const MinecraftUtilities_1 = require("./MinecraftUtilities");
class SpawnRulesBehaviorDefinition {
    constructor() {
        this._isLoaded = false;
        this._onLoaded = new ste_events_1.EventDispatcher();
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    set file(newFile) {
        this._file = newFile;
    }
    get id() {
        return this._id;
    }
    set id(newId) {
        this._id = newId;
        if (this.dataInner && this.dataInner.description && newId) {
            this.dataInner.description.identifier = newId;
        }
    }
    get shortId() {
        if (this._id !== undefined) {
            if (this._id.startsWith("minecraft:")) {
                return this._id.substring(10, this._id.length);
            }
            return this._id;
        }
        return undefined;
    }
    async getFormatVersionIsCurrent() {
        const fv = this.getFormatVersion();
        if (fv === undefined || fv.length !== 3) {
            return false;
        }
        return await Database_1.default.isRecentVersionFromVersionArray(fv);
    }
    getFormatVersion() {
        if (!this.data || !this.data.format_version) {
            return undefined;
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this.data.format_version);
    }
    setFormatVersion(versionStr) {
        this._ensureDataInitialized();
        if (this.data) {
            this.data.format_version = versionStr;
        }
    }
    _ensureDataInitialized() {
        if (this.data === undefined) {
            this.data = { "minecraft:spawn_rules": { description: { identifier: this._id ? this._id : "" } } };
        }
    }
    static async ensureOnFile(file, loadHandler) {
        let srb;
        if (file.manager === undefined) {
            srb = new SpawnRulesBehaviorDefinition();
            srb.file = file;
            file.manager = srb;
        }
        if (file.manager !== undefined && file.manager instanceof SpawnRulesBehaviorDefinition) {
            srb = file.manager;
            if (!srb.isLoaded && loadHandler) {
                srb.onLoaded.subscribe(loadHandler);
            }
            await srb.load();
        }
        return srb;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const bpString = JSON.stringify(this.data, null, 2);
        this._file.setContent(bpString);
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        await this._file.loadContent();
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this.data = StorageUtilities_1.default.getJsonObject(this._file);
        this.dataInner = this.data?.["minecraft:spawn_rules"];
        if (this.dataInner && this.dataInner.description) {
            this._id = this.dataInner.description.identifier;
        }
        this._isLoaded = true;
    }
}
exports.default = SpawnRulesBehaviorDefinition;

//# sourceMappingURL=../maps/minecraft/SpawnRulesBehaviorDefinition.js.map
