'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _events = require('events');

var _lodash = require('lodash');

//
// ipc: Inter-Plugins Call
//
function _ref() {
  return true;
}

function _ref2(scope) {
  return (0, _lodash.mapValues)(scope, _ref);
}

class IPC extends _events.EventEmitter {
  constructor() {
    super();

    this.register = (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;
    };

    this.unregister = (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;
    };

    this.unregisterAll = scope => {
      delete this.data[scope];
      this.emit('update', { type: '@@unregisterAllIPC', value: { scope } });
    };

    this.access = scope => {
      return this.data[scope];
    };

    this.list = () => (0, _lodash.mapValues)(this.data, _ref2);

    this.foreachCall = (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


  // scope:  string
  // keys:   string / Array of string / key-func Object


  // key:    string
  // args:   arguments passing to api
}

exports.default = new IPC();
module.exports = exports['default'];