namespace.js

'use strict';

/**
 * @namespace Monster
 * @author schukai GmbH
 */


/**
 * namespace class objects form the basic framework of the namespace administration.
 *
 * all functions, classes and objects of the library hang within the namespace tree.
 *
 * via `obj instanceof Monster.Namespace` it is also easy to check whether it is an object or a namespace.
 *
 * @memberOf Monster
 
 * @copyright schukai GmbH
 * @since 1.0.0
 */
class Namespace {

    /**
     *
     * @param namespace
     * @param obj
     */
    constructor(namespace) {
        if (namespace === undefined || typeof namespace !== 'string') {
            throw new Error("namespace is not a string")
        }
        this.namespace = namespace;
    }

    /**
     *
     * @returns {string}
     */
    getNamespace() {
        return this.namespace;
    }

    /**
     *
     * @returns {string}
     */
    toString() {
        return this.getNamespace();
    }
}

/**
 *
 * @type {Namespace}
 * @global
 */
export const Monster = new Namespace("Monster");


/**
 *
 */
assignToNamespace('Monster', assignToNamespace);

/**
 * To expand monster, the `Monster.assignToNamespace()` method can be used. 
 *
 * you must call the method in the monster namespace. this allows you to mount your own classes, objects and functions into the namespace.
 * 
 * To avoid confusion and so that you do not accidentally overwrite existing functions, you should use the custom namespace `X`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.5.0/dist/modules/namespace.js';
 * function hello() {
 *            console.log('Hello World!');
 *        }
 * Monster.assignToNamespace("Monster.X",hello)
 * Monster.X.hello(); // ↦ Hello World!
 * </script>
 * 
 * ```
 *
 * @param ns
 * @param obj
 * @memberOf Monster
 
 */
function assignToNamespace(ns, ...obj) {
    let current = namespaceFor(ns.split("."));

    for (let i = 0, l = obj.length; i < l; i++) {
        current[objectName(obj[i])] = obj[i];
    }
}

/**
 *
 * @param fn
 * @returns {string|*}
 * @private
 */
function objectName(fn) {
    try {

        if (typeof fn !== 'function') {
            throw  new Error("the first argument is not a function or class.");
        }

        if (fn.hasOwnProperty('name')) {
            return fn.name;
        }

        if ("function" === typeof fn.toString) {
            let s = fn.toString();
            let f = s.match(/^\s*function\s+([^\s(]+)/);
            if (Array.isArray(f) && typeof f[1] === 'string') {
                return f[1];
            }
            let c = s.match(/^\s*class\s+([^\s(]+)/);
            if (Array.isArray(c) && typeof c[1] === 'string') {
                return c[1];
            }
        }

    } catch (e) {
        throw new Error("exception " + e);
    }

    throw  new Error("the name of the class or function cannot be resolved.");
}

/**
 *
 * @param parts
 * @returns {Namespace}
 * @private
 */
function namespaceFor(parts) {
    var space = Monster, ns = 'Monster';

    for (let i = 0; i < parts.length; i++) {

        if ("Monster" === parts[i]) {
            continue;
        }

        ns += '.' + parts[i];

        if (!space.hasOwnProperty(parts[i])) {
            space[parts[i]] = new Namespace(ns);
        }

        space = space[parts[i]];
    }

    return space;
}


export {assignToNamespace}