import XEUtils from 'xe-utils';
import { warnLog } from '../../tools/log';

class TablePlusCommandsStore {
  constructor() {
    this.store = {};
  }

  mixin(options) {
    XEUtils.each(options, (item, key) => {
      this.add(key, item);
    });
    return TablePlusCommandsStore;
  }

  get(name) {
    return this.store[name];
  }

  add(name, render) {
    const conf = this.store[name];
    // 兼容
    if (XEUtils.isFunction(render)) {
      // if (process.env.NODE_ENV === 'development') {
      //   warnLog('tablePlus.error.delProp', ['commands -> callback', 'commandMethod'])
      // }
      render = {
        commandMethod: render
      };
    }

    // 检测是否覆盖
    if (process.env.NODE_ENV === 'development') {
      const confKeys = XEUtils.keys(conf);
      XEUtils.each(render, (item, key) => {
        if (confKeys.includes(key)) {
          warnLog('tablePlus.error.coverProp', [name, key]);
        }
      });
    }
    this.store[name] = conf ? XEUtils.merge(conf, render) : render;
    return TablePlusCommandsStore;
  }

  delete(name) {
    delete this.store[name];
    return TablePlusCommandsStore;
  }
}

export const commands = new TablePlusCommandsStore();

if (process.env.NODE_ENV === 'development') {
  Object.assign(commands, { _name: 'Commands' });
}
