const MamiEventTarget = function(prefix) {
    prefix = typeof prefix === 'string' ? `${prefix}:` : '';

    const eventTarget = new EventTarget;
    const createEvent = (name, detail) => new CustomEvent(prefix + name, (typeof detail === 'object' && detail !== null && 'detail' in detail ? detail : { detail: detail }));

    return {
        create: createEvent,

        addEventListener: eventTarget.addEventListener.bind(eventTarget),
        removeEventListener: eventTarget.removeEventListener.bind(eventTarget),
        dispatchEvent: eventTarget.dispatchEvent.bind(eventTarget),

        watch: (name, ...args) => {
            eventTarget.addEventListener(prefix + name, ...args);
        },
        unwatch: (name, ...args) => {
            eventTarget.removeEventListener(prefix + name, ...args);
        },
        dispatch: (...args) => eventTarget.dispatchEvent(createEvent(...args)),
    };
};
