import { log } from "./common";

/* eslint-disable no-underscore-dangle */



/**
 * Naive implementation of Singleton
 * @author Aamir khan
 * @namespace Singleton
 */
const Singleton = {
    /**
     * @private
     */
    _instances: Object.create(null),


    /**
     * 
     * @param {String} id - unique id of the instance
     * @param {Function} instanceCreator - a function that creates the instance
     * @returns {Object} the instance
     */
    create: function makeInstance(id, instanceCreator) {
        if (this._instances[id]) return this.get(id);
        log('making new instance', id);
        this._instances[id] = instanceCreator();
        return this.get(id);
    },


    /**
     * get the instance by id if exists
     * @param {String} id - unique id of the instance
     * @returns {Object} the instance
     */
    get: function getInstance(id) {
        return this._instances[id];
    },


    /**
     * remove the instance by id
     * @param {String} id - unique id of the instance
     */
    remove: function removeInstance(id) {
        delete this._instances[id];
        log('removing instance', id);
    }
}


export default Singleton;