"use strict";

exports.__esModule = true;
exports.default = void 0;

var _events = require("events");

var _lodash = require("lodash");

//
// ipc: Inter-Plugins Call
//
class IPC extends _events.EventEmitter {
  constructor() {
    super();
    Object.defineProperty(this, "register", {
      configurable: true,
      enumerable: true,
      writable: true,
      value: (scope, opts) => {
        if (!(scope && opts)) {
          console.error("Invalid scope or opts:", scope, opts);
          return;
        }

        if (!this.data[scope]) {
          this.data[scope] = new Object();
        }

        this.unregister(scope, Object.keys(opts));

        for (const key in opts) {
          this.data[scope][key] = opts[key];
        }

        this.emit('update', {
          type: '@@registerIPC',
          value: {
            scope,
            opts
          }
        });
        return;
      }
    });
    Object.defineProperty(this, "unregister", {
      configurable: true,
      enumerable: true,
      writable: true,
      value: (scope, keys) => {
        if (!(scope && keys)) {
          console.error("Invalid scope or keys:", scope, keys);
          return;
        }

        if (!this.data[scope]) {
          return;
        }

        if (typeof keys === "string") {
          keys = new Array(keys);
        }

        if (keys instanceof Object && !(keys instanceof Array)) {
          keys = Object.keys(keys);
        }

        for (const key of keys) {
          delete this.data[scope][key];
        }

        this.emit('update', {
          type: '@@unregisterIPC',
          value: {
            scope,
            keys
          }
        });
        return;
      }
    });
    Object.defineProperty(this, "unregisterAll", {
      configurable: true,
      enumerable: true,
      writable: true,
      value: scope => {
        delete this.data[scope];
        this.emit('update', {
          type: '@@unregisterAllIPC',
          value: {
            scope
          }
        });
      }
    });
    Object.defineProperty(this, "access", {
      configurable: true,
      enumerable: true,
      writable: true,
      value: scope => {
        return this.data[scope];
      }
    });
    Object.defineProperty(this, "list", {
      configurable: true,
      enumerable: true,
      writable: true,
      value: () => (0, _lodash.mapValues)(this.data, scope => (0, _lodash.mapValues)(scope, () => true))
    });
    Object.defineProperty(this, "foreachCall", {
      configurable: true,
      enumerable: true,
      writable: true,
      value: (key, ...args) => {
        for (const scope in this.data) {
          if (this.data[scope].hasOwnProperty(key)) {
            this.data[key].apply(null, args);
          }
        }
      }
    });
    this.data = new Object();
  } // scope:  string
  // opts:   key-func Object


}

var _default = new IPC();

exports.default = _default;
module.exports = exports["default"];