/*
IT License

Copyright (c) 2023 ConMaster2112

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//NOTE: The code below has been heavily modified and converted to .js instead of ts and made some improvements to for my use case here.
// StealthyX
import { world, World, Entity } from "@minecraft/server";
const mc_world = world;
const { setDynamicProperty: wSDP, getDynamicProperty: wGDP, getDynamicPropertyIds: wGDPI } = World.prototype;
const { isValid: isValidEntity, setDynamicProperty: eSDP, getDynamicProperty: eGDP, getDynamicPropertyIds: eGDPI, } = Entity.prototype;
const DYNAMIC_DB_PREFIX = "\u1221\u2112";
const SOURCE_INSTANCES = new WeakMap();
const DDB_SUBINSTANCES = new WeakMap();
const STRING_LIMIT = 32e3;
const eP = {
    gDP: eGDP,
    sDP: eSDP,
    gDPI: eGDPI,
};
const wP = {
    gDP: wGDP,
    sDP: wSDP,
    gDPI: wGDPI,
};
class DynamicSource {
    /**@param {World | Entity} source  */
    constructor(source) {
        this.source = source;
        if (SOURCE_INSTANCES.has(source))
            return SOURCE_INSTANCES.get(source);
        if (source === mc_world)
            Object.assign(this, wP);
        else if (isValidEntity.call(source))
            Object.assign(this, eP);
        else
            throw new ReferenceError("Invald source type: " + source);
        SOURCE_INSTANCES.set(source, this);
    }
    /**@returns {string[]} */
    getDynamicPropertyIds() {
        if (this.source instanceof World) {
            return this.source.getDynamicPropertyIds();
        }
        else if (this.source instanceof Entity) {
            return this.source.getDynamicPropertyIds();
        }
        else {
            throw new Error("Unsupported source type");
        }
    }
    /**@param {string} key  @returns {number | boolean | string | import("@minecraft/server").Vector3}*/
    getDynamicProperty(key) {
        if (this.source instanceof World || this.source instanceof Entity) {
            return this.source.getDynamicProperty(key);
        }
        else {
            throw new Error("Unsupported source type");
        }
    }
    /**@param {string} key */
    setDynamicProperty(key, value) {
        if (this.source instanceof World || this.source instanceof Entity) {
            this.source.setDynamicProperty(key, value);
        }
        else {
            throw new Error("Unsupported source type");
        }
    }
    /**@param {string} key @returns {boolean}  */
    deleteDynamicProperty(key) {
        if (this.source instanceof World || this.source instanceof Entity) {
            this.source.setDynamicProperty(key, undefined);
            return true;
        }
        else {
            throw new Error("Unsupported source type");
        }
    }
    /**@returns {boolean}  */
    isValid() {
        return this.source === world || isValidEntity.call(this.source);
    }
}
class DynamicDatabase extends Map {
    /**@param {World | Entity} source @param {string} id @param {string} kind   */
    constructor(source, id, kind, parser) {
        super();
        this._source = new DynamicSource(source);
        const PRE = `${kind}${DYNAMIC_DB_PREFIX}${id}${DYNAMIC_DB_PREFIX}`, LENGTH = PRE.length, SOURCE = this._source, PARSE = parser.parse;
        const MAP_INSTANCES = DDB_SUBINSTANCES.get(SOURCE) ?? new Map();
        if (MAP_INSTANCES.has(PRE))
            return MAP_INSTANCES.get(PRE);
        MAP_INSTANCES.set(PRE, this);
        DDB_SUBINSTANCES.set(SOURCE, MAP_INSTANCES);
        if (!SOURCE.isValid())
            throw new ReferenceError("Source is no longer valid: " + SOURCE.source);
        this._prefix = PRE;
        this._prefixLength = LENGTH;
        this._STRINGIFY = parser.stringify;
        this._PARSE = PARSE;
        this._notDisposed = true;
        for (const K of SOURCE.getDynamicPropertyIds())
            if (K.startsWith(PRE)) {
                const key = K.substring(LENGTH);
                const value = SOURCE.getDynamicProperty(K);
                if (typeof value === "string")
                    super.set(key, PARSE(value));
            }
    }
    /**@param {string} key @param {any} value */
    set(key, value) {
        if (!this.isValid())
            throw new ReferenceError("This database instance is no longer valid");
        if (key.length + this._prefixLength > STRING_LIMIT)
            throw new TypeError("Key is too long: " + key.length);
        if (value === undefined) {
            this.delete(key);
            return this;
        }
        const data = this._STRINGIFY(value);
        if (data.length > STRING_LIMIT)
            throw new TypeError("Size of data in string is too long: " + data.length);
        this._source.setDynamicProperty(this._prefix + key, data);
        return super.set(key, value);
    }
    /**@param {string} key  */
    delete(key) {
        if (!this.isValid())
            throw new ReferenceError("This database instance is no longer valid");
        if (!this.has(key))
            return false;
        this._source.deleteDynamicProperty(this._prefix + key);
        return super.delete(key);
    }
    clear() {
        if (!this.isValid())
            throw new ReferenceError("This database instance is no longer valid");
        const P = this._prefix;
        const s = this._source;
        for (const key of this.keys())
            s.deleteDynamicProperty(P + key);
        return super.clear();
    }
    /**@returns {boolean} */
    isValid() {
        return this._source.isValid() && this._notDisposed;
    }
    dispose() {
        this._notDisposed = false;
        DDB_SUBINSTANCES.get(this._source)?.delete?.(this._prefix);
        super.clear();
    }
    /**@readonly @type {boolean} */
    get isDisposed() {
        return !this._notDisposed;
    }
}
class DynamicWrapper {
    constructor(source, id, kind, parser) {
        this._source = source;
        const PRE = `${kind}${DYNAMIC_DB_PREFIX}${id}${DYNAMIC_DB_PREFIX}`;
        this._prefix = PRE;
        this._prefixLength = PRE.length;
        this._STRINGIFY = parser.stringify;
        this._PARSE = parser.parse;
    }
    clear() {
        for (const k of this.__getKeys()) {
            this._source.setDynamicProperty(k, undefined);
        }
    }
    /**
     * @returns true if an element in the Map existed and has been removed, or false if the element does not exist.
     */
    delete(key) {
        const has = this.has(key);
        this._source.setDynamicProperty(this._prefix + key, undefined);
        return has;
    }
    /**
     * Executes a provided function once per each key/value pair in the Map, in insertion order.
     * @param {(value: any, key: string, map: DynamicWrapper) => void} callbackfn
     * @param {any} thisArg
     */
    forEach(callbackfn, thisArg = null) {
        for (const k of this.keys()) {
            try {
                callbackfn.call(thisArg ?? null, k, this.get(k), this);
            }
            catch (error) { }
        }
    }
    /**
     * Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
     * @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
     */
    get(key) {
        const a = this._source.getDynamicProperty(this._prefix + key);
        typeof a === "string" ? this._PARSE(a) : a;
    }
    /**
     * @returns boolean indicating whether an element with the specified key exists or not.
     */
    has(key) {
        return this._source.getDynamicProperty(this._prefix + key) !== undefined;
    }
    /**
     * Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
     */
    set(key, value) {
        this._source.setDynamicProperty(this._prefix + key, this._STRINGIFY(value));
        return this;
    }
    /**
     * @returns the number of elements in the Map.
     */
    get size() {
        return [...this.__getKeys()].length;
    }
    /** Returns an iterable of entries in the map. */
    [Symbol.iterator]() {
        return this.entries();
    }
    /**
     * Returns an iterable of key, value pairs for every entry in the map.
     */
    *entries() {
        for (const key of this.__getKeys()) {
            const propertyValue = this._source.getDynamicProperty(key);
            // Check if the property value is defined and is a string before parsing
            if (typeof propertyValue === "string") {
                yield [key.substring(this._prefixLength), this._PARSE(propertyValue)];
            }
            else {
                // Handle the undefined or non-string property value as per your logic
                // For example, yield undefined or skip the iteration
                yield [key.substring(this._prefixLength), propertyValue];
            }
        }
    }
    /**
     * Returns an iterable of keys in the map
     */
    *keys() {
        for (const k of this.__getKeys())
            yield k.substring(this._prefixLength);
    }
    /**
     * Returns an iterable of values in the map
     */
    *values() {
        for (const key of this.__getKeys()) {
            const propertyValue = this._source.getDynamicProperty(key);
            // Check if the property value is defined and is a string before parsing
            if (typeof propertyValue === "string") {
                yield this._PARSE(propertyValue);
            }
            else {
                // Handle the undefined or non-string property value as per your logic
                // For example, yield undefined or skip the iteration
                yield propertyValue;
            }
        }
    }
    *__getKeys() {
        for (const K of this._source.getDynamicPropertyIds())
            if (K.startsWith(this._prefix))
                yield K;
    }
}
export class JsonDatabase extends DynamicDatabase {
    /**@param {World | Entity} source @param {string} id  */
    constructor(source, id) {
        super(source, id, "JSON", JSON);
    }
}
export class WorldDatabase extends JsonDatabase {
    /**@param {string} id  */
    constructor(id) {
        super(mc_world, id);
    }
}
export class JSONDynamicWrapper extends DynamicWrapper {
    constructor(source, id) {
        super(source, id, "JSON", JSON);
    }
}

//# sourceMappingURL=../../../../../_KiCuMountsBPDebug/kicu/mmad/U/database.js.map
