"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.get_terminate = exports.set_terminate = exports.terminate = void 0;
//================================================================
/**
 * @packageDocumentation
 * @module std
 */
//================================================================
var node_1 = require("../utility/node");
var Global_1 = require("../internal/Global");
/**
 * Terminate program.
 */
function terminate() {
    if ((0, node_1.is_node)() === true)
        global.process.exit();
    else {
        if (typeof window !== "undefined" && self.open instanceof Function)
            self.open("", "_self", "");
        self.close();
    }
}
exports.terminate = terminate;
/**
 * Set terminate handler.
 *
 * @param func The terminate handler.
 */
function set_terminate(func) {
    //----
    // PREPARE EVENT LISTENER
    //----
    var type;
    var register;
    var eraser;
    if ((0, node_1.is_node)() === true) {
        type = "exit";
        register = function (type, listener) {
            return global.process.addListener(type, listener);
        };
        eraser = function (type, listener) {
            return global.process.removeListener(type, listener);
        };
    }
    else {
        // IF WORKER, THEN CANNOT ASSURE ACTIVATION.
        type = typeof window !== "undefined" ? "unload" : "close";
        register = function (type, listener) { return self.addEventListener(type, listener); };
        eraser = function (type, listener) { return self.removeEventListener(type, listener); };
    }
    //----
    // ENROLL THE LISTENER
    //----
    // ERASE ORDINARY
    if ((0, Global_1._Get_root)().__s_pTerminate_handler !== undefined)
        eraser(type, (0, Global_1._Get_root)().__s_pTerminate_handler);
    // DO REGISTER
    register("exit", func);
    // ARCHIVE THE LISTENER
    (0, Global_1._Get_root)().__s_pTerminate_handler = func;
}
exports.set_terminate = set_terminate;
/**
 * Get terminate handler.
 *
 * @return The terminate handler.
 */
function get_terminate() {
    return (0, Global_1._Get_root)().__s_pTerminate_handler;
}
exports.get_terminate = get_terminate;
//# sourceMappingURL=global.js.map