import {mergeRecursive} from "@/utils/muyu";
import DictOptions from './DictOptions'

/**
 * @classdesc 字典元数据
 * @property {String} type 类型
 * @property {Function} request 请求
 * @property {String} label 标签字段
 * @property {String} value 值字段
 */
export default class DictMeta {
  constructor(options) {
    this.type = options.type   // @classdesc 字典元数据
    this.request = options.request   // @classdesc 字典元数据
    this.responseConverter = options.responseConverter   // @classdesc 字典元数据
    this.labelField = options.labelField   // @classdesc 字典元数据
    this.valueField = options.valueField   // @classdesc 字典元数据
    this.lazy = options.lazy === true   // @classdesc 字典元数据
  }
}


/**
 * 解析字典元数据
 * @param {Object} options
 * @returns {DictMeta}
 */
DictMeta.parse = function (options) {   // 解析字典元数据
  let opts = null
  if (typeof options === 'string') {
    opts = DictOptions.metas[options] || {}
    opts.type = options
  } else if (typeof options === 'object') {
    opts = options
  }
  opts = mergeRecursive(DictOptions.metas['*'], opts)
  return new DictMeta(opts)   // @classdesc 字典元数据
}

