"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupLifecycle_IPlugin = exports.killLifecycle = exports.setupLifecycle = exports.onCreateResources = exports.onViUpdate = exports.onPostTick = exports.onTick = exports.Postinit = exports.Init = exports.Preinit = exports.lifecyclebus = exports.LifecycleEventBus = void 0;
const events_1 = require("events");
class LifecycleEventBus extends events_1.EventEmitter {
}
exports.LifecycleEventBus = LifecycleEventBus;
exports.lifecyclebus = new LifecycleEventBus();
class LifecycleContainer {
    key;
    bound;
    constructor(key) {
        this.key = key;
    }
}
function Preinit() {
    return function (target, propertyKey, descriptor) {
        if (target.ModLoader === undefined) {
            target['ModLoader'] = {};
        }
        if (target.ModLoader.Lifecycle === undefined) {
            target.ModLoader['Lifecycle'] = {};
        }
        if (target.ModLoader.Lifecycle.Preinit === undefined) {
            target.ModLoader.Lifecycle['Preinit'] = new Map();
        }
        target.ModLoader.Lifecycle.Preinit.set("Preinit", new LifecycleContainer(propertyKey));
    };
}
exports.Preinit = Preinit;
function Init() {
    return function (target, propertyKey, descriptor) {
        if (target.ModLoader === undefined) {
            target['ModLoader'] = {};
        }
        if (target.ModLoader.Lifecycle === undefined) {
            target.ModLoader['Lifecycle'] = {};
        }
        if (target.ModLoader.Lifecycle.Init === undefined) {
            target.ModLoader.Lifecycle['Init'] = new Map();
        }
        target.ModLoader.Lifecycle.Init.set("Init", new LifecycleContainer(propertyKey));
    };
}
exports.Init = Init;
function Postinit() {
    return function (target, propertyKey, descriptor) {
        if (target.ModLoader === undefined) {
            target['ModLoader'] = {};
        }
        if (target.ModLoader.Lifecycle === undefined) {
            target.ModLoader['Lifecycle'] = {};
        }
        if (target.ModLoader.Lifecycle.Postinit === undefined) {
            target.ModLoader.Lifecycle['Postinit'] = new Map();
        }
        target.ModLoader.Lifecycle.Postinit.set("Postinit", new LifecycleContainer(propertyKey));
    };
}
exports.Postinit = Postinit;
function onTick() {
    return function (target, propertyKey, descriptor) {
        if (target.ModLoader === undefined) {
            target['ModLoader'] = {};
        }
        if (target.ModLoader.Lifecycle === undefined) {
            target.ModLoader['Lifecycle'] = {};
        }
        if (target.ModLoader.Lifecycle.onTick === undefined) {
            target.ModLoader.Lifecycle['onTick'] = new Map();
        }
        target.ModLoader.Lifecycle.onTick.set("onTick", new LifecycleContainer(propertyKey));
    };
}
exports.onTick = onTick;
function onPostTick() {
    return function (target, propertyKey, descriptor) {
        if (target.ModLoader === undefined) {
            target['ModLoader'] = {};
        }
        if (target.ModLoader.Lifecycle === undefined) {
            target.ModLoader['Lifecycle'] = {};
        }
        if (target.ModLoader.Lifecycle.onPostTick === undefined) {
            target.ModLoader.Lifecycle['onPostTick'] = new Map();
        }
        target.ModLoader.Lifecycle.onPostTick.set("onPostTick", new LifecycleContainer(propertyKey));
    };
}
exports.onPostTick = onPostTick;
function onViUpdate() {
    return function (target, propertyKey, descriptor) {
        if (target.ModLoader === undefined) {
            target['ModLoader'] = {};
        }
        if (target.ModLoader.Lifecycle === undefined) {
            target.ModLoader['Lifecycle'] = {};
        }
        if (target.ModLoader.Lifecycle.onViUpdate === undefined) {
            target.ModLoader.Lifecycle['onViUpdate'] = new Map();
        }
        target.ModLoader.Lifecycle.onViUpdate.set("onViUpdate", new LifecycleContainer(propertyKey));
    };
}
exports.onViUpdate = onViUpdate;
function onCreateResources() {
    return function (target, propertyKey, descriptor) {
        if (target.ModLoader === undefined) {
            target['ModLoader'] = {};
        }
        if (target.ModLoader.Lifecycle === undefined) {
            target.ModLoader['Lifecycle'] = {};
        }
        if (target.ModLoader.Lifecycle.onCreateResources === undefined) {
            target.ModLoader.Lifecycle['onCreateResources'] = new Map();
        }
        target.ModLoader.Lifecycle.onCreateResources.set("onCreateResources", new LifecycleContainer(propertyKey));
    };
}
exports.onCreateResources = onCreateResources;
function setupLifecycle(instance) {
    let p = Object.getPrototypeOf(instance);
    if (p.hasOwnProperty('ModLoader')) {
        if (p.ModLoader.hasOwnProperty("hasBeenProcessed")) {
            return;
        }
        if (p.ModLoader.hasOwnProperty('Lifecycle')) {
            if (p.ModLoader.Lifecycle.hasOwnProperty("Preinit")) {
                p.ModLoader.Lifecycle.Preinit.forEach((value, key) => {
                    // How in the fuck is this a string sometimes specifically on the dolphin binding?
                    if (value instanceof LifecycleContainer) {
                        let a = instance[value.key].bind(instance);
                        value.bound = a;
                        exports.lifecyclebus.emit("preinit" /* LifeCycleEvents.PREINIT */, a);
                    }
                    else if (value instanceof String) {
                        let a = instance[value].bind(instance);
                        exports.lifecyclebus.emit("preinit" /* LifeCycleEvents.PREINIT */, a);
                    }
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("Init")) {
                p.ModLoader.Lifecycle.Init.forEach(function (value, key) {
                    if (value instanceof LifecycleContainer) {
                        let a = instance[value.key].bind(instance);
                        value.bound = a;
                        exports.lifecyclebus.emit("init" /* LifeCycleEvents.INIT */, a);
                    }
                    else if (value instanceof String) {
                        let a = instance[value].bind(instance);
                        exports.lifecyclebus.emit("init" /* LifeCycleEvents.INIT */, a);
                    }
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("Postinit")) {
                p.ModLoader.Lifecycle.Postinit.forEach(function (value, key) {
                    if (value instanceof LifecycleContainer) {
                        let a = instance[value.key].bind(instance);
                        value.bound = a;
                        exports.lifecyclebus.emit("postinit" /* LifeCycleEvents.POSTINIT */, a);
                    }
                    else if (value instanceof String) {
                        let a = instance[value].bind(instance);
                        exports.lifecyclebus.emit("postinit" /* LifeCycleEvents.POSTINIT */, a);
                    }
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onTick")) {
                p.ModLoader.Lifecycle.onTick.forEach(function (value, key) {
                    if (value instanceof LifecycleContainer) {
                        let a = instance[value.key].bind(instance);
                        value.bound = a;
                        exports.lifecyclebus.emit("ontick" /* LifeCycleEvents.ONTICK */, a);
                    }
                    else if (value instanceof String) {
                        let a = instance[value].bind(instance);
                        exports.lifecyclebus.emit("ontick" /* LifeCycleEvents.ONTICK */, a);
                    }
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onPostTick")) {
                p.ModLoader.Lifecycle.onPostTick.forEach(function (value, key) {
                    if (value instanceof LifecycleContainer) {
                        let a = instance[value.key].bind(instance);
                        value.bound = a;
                        exports.lifecyclebus.emit("onposttick" /* LifeCycleEvents.ONPOSTTICK */, a);
                    }
                    else if (value instanceof String) {
                        let a = instance[value].bind(instance);
                        exports.lifecyclebus.emit("onposttick" /* LifeCycleEvents.ONPOSTTICK */, a);
                    }
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onViUpdate")) {
                p.ModLoader.Lifecycle.onViUpdate.forEach(function (value, key) {
                    if (value instanceof LifecycleContainer) {
                        let a = instance[value.key].bind(instance);
                        value.bound = a;
                        exports.lifecyclebus.emit("onviupdate" /* LifeCycleEvents.ONVIUPDATE */, a);
                    }
                    else if (value instanceof String) {
                        let a = instance[value].bind(instance);
                        exports.lifecyclebus.emit("onviupdate" /* LifeCycleEvents.ONVIUPDATE */, a);
                    }
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onCreateResources")) {
                p.ModLoader.Lifecycle.onCreateResources.forEach(function (value, key) {
                    if (value instanceof LifecycleContainer) {
                        let a = instance[value.key].bind(instance);
                        value.bound = a;
                        exports.lifecyclebus.emit("oncreateresources" /* LifeCycleEvents.ONCREATERESOURCES */, a);
                    }
                    else if (value instanceof String) {
                        let a = instance[value].bind(instance);
                        exports.lifecyclebus.emit("oncreateresources" /* LifeCycleEvents.ONCREATERESOURCES */, a);
                    }
                });
            }
        }
    }
}
exports.setupLifecycle = setupLifecycle;
function killLifecycle(instance) {
    let p = Object.getPrototypeOf(instance);
    if (p.hasOwnProperty('ModLoader')) {
        if (p.ModLoader.hasOwnProperty("hasBeenProcessed")) {
            delete p.ModLoader["hasBeenProcessed"];
        }
        if (p.ModLoader.hasOwnProperty('Lifecycle')) {
            if (p.ModLoader.Lifecycle.hasOwnProperty("Preinit")) {
                p.ModLoader.Lifecycle.Preinit.forEach(function (value, key) {
                    exports.lifecyclebus.emit("preinit_destroy" /* LifeCycleEvents.PREINIT_DESTROY */, value.bound);
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("Init")) {
                p.ModLoader.Lifecycle.Init.forEach(function (value, key) {
                    exports.lifecyclebus.emit("init_destroy" /* LifeCycleEvents.INIT_DESTROY */, value.bound);
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("Postinit")) {
                p.ModLoader.Lifecycle.Postinit.forEach(function (value, key) {
                    exports.lifecyclebus.emit("postinit_destroy" /* LifeCycleEvents.POSTINIT_DESTROY */, value.bound);
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onTick")) {
                p.ModLoader.Lifecycle.onTick.forEach(function (value, key) {
                    exports.lifecyclebus.emit("ontick_destroy" /* LifeCycleEvents.ONTICK_DESTROY */, value.bound);
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onPostTick")) {
                p.ModLoader.Lifecycle.onPostTick.forEach(function (value, key) {
                    exports.lifecyclebus.emit("onposttick_destroy" /* LifeCycleEvents.ONPOSTTICK_DESTROY */, value.bound);
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onViUpdate")) {
                p.ModLoader.Lifecycle.onViUpdate.forEach(function (value, key) {
                    exports.lifecyclebus.emit("onviupdate_destroy" /* LifeCycleEvents.ONVIUPDATE_DESTROY */, value.bound);
                });
            }
            if (p.ModLoader.Lifecycle.hasOwnProperty("onCreateResources")) {
                p.ModLoader.Lifecycle.onCreateResources.forEach(function (value, key) {
                    exports.lifecyclebus.emit("oncreateresources_destroy" /* LifeCycleEvents.ONCREATERESOURCES_DESTROY */, value.bound);
                });
            }
        }
    }
}
exports.killLifecycle = killLifecycle;
function setupLifecycle_IPlugin(instance) {
    // Wrapper for IPlugin.
    try {
        let old_pre = instance['preinit'].bind(instance);
        exports.lifecyclebus.emit("preinit" /* LifeCycleEvents.PREINIT */, old_pre);
    }
    catch (err) {
    }
    try {
        let old_init = instance['init'].bind(instance);
        exports.lifecyclebus.emit("init" /* LifeCycleEvents.INIT */, old_init);
    }
    catch (err) {
    }
    try {
        let old_post = instance['postinit'].bind(instance);
        exports.lifecyclebus.emit("postinit" /* LifeCycleEvents.POSTINIT */, old_post);
    }
    catch (err) {
    }
    try {
        let old_tick = instance['onTick'].bind(instance);
        exports.lifecyclebus.emit("ontick" /* LifeCycleEvents.ONTICK */, old_tick);
    }
    catch (err) {
    }
}
exports.setupLifecycle_IPlugin = setupLifecycle_IPlugin;
//# sourceMappingURL=PluginLifecycle.js.map