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

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

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

  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', ['menus -> callback', 'menuMethod'])
      // }
      render = {
        menuMethod: 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 TablePlusMenusStore;
  }

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

export const menus = new TablePlusMenusStore();

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