/**
 * 全局配置pro配置
 * @author xiufu.wang
 */
import { objectProperty, paramsSerializer } from 'mars-pro/src/pro/util';

export const PRO_CONFIG_PROVIDE_CONTEXT = 'PRO_CONFIG_PROVIDE_CONTEXT'
export const NOT_FOUND = '_$_NOT_FOUND_$_'

export default {
  name: 'ProConfigProvide',
  componentName: 'ProConfigProvide',
  provide() {
    return {
      [PRO_CONFIG_PROVIDE_CONTEXT]: this
    }
  },
  props: {
    config: {
      type: Object,
      default: function () {
        return {
          //菜单数
          menuData: [],
          //字典数据
          dictData: {},
          //字典数据结构默认字段转换规则,用于dataSource
          dictFieldMap:null,
          //自定义字典类型 到 数据的映射逻辑
          getDataByDictType: (dictPath, dictDatas) => dictPath,
          // 操作权限 Key/value结构
          authorityData: {},
          // 用户信息
          useInfoData: {},
          //getComponentByUrl
          getComponentByUrl: () => { },

          //detailModel--label命名规范
          getLabelPropFromDetailModel: (prop) => prop + 'AsText',
          //多选展示文本分割字符,默认为英文逗号
          multipleSelectLabelTxtSeparator: null,
          // 接口规范配置
          api: {
            totalField: 'total',
            //每页多少条字段
            pageSizeField: 'pageSize',
            // 当前字段
            currentPageField: 'currentPage',
            // 默认查询接口响应结果字段
            dataField: 'list',
            //默认http请求方式: 用于简化数据源配置
            //childrenField
            childrenField: 'children',
            method: 'post',
            // tree按需加载key field
            // 简单查询通用字段
            singleQueryField: 'query'
          }
        }
      }
    }
  },
  methods: {
    getMenuDatas() {
      return this.config.menuData
    },
    getApiParam(key, defaultV) {
      if (!key) {
        return this.config.api
      }
      return objectProperty(this.config.api, key, defaultV)
    },
    getDict(key, defaultV) {
      if (!key) {
        return this.config.dictData
      }
      return objectProperty(this.config.dictData, key, defaultV)
    },
    getAuthority(key, defaultV) {
      if (!key) {
        return this.config.authorityData
      }
      return objectProperty(this.config.authorityData, key, defaultV)
    },
    getUseInfo(key, defaultV) {
      if (!key) {
        return this.config.useInfoData
      }
      return objectProperty(this.config.useInfoData, key, defaultV)
    },
    //跳转路由
    goToByRoute(route, params) {
      if (this.$router) {
        //解析路由
        this.$router.push({
          path: route,
          params: params,
          query: params
        })
      }
    },
    // 跳转新窗口
    goToByHref(toHref, params) {
      toHref = toHref + (toHref.indexOf('?') > -1 ? '&' : '?') + paramsSerializer(params || {})
      window.open(toHref, '_blank')
    }
  },
  render(h) {
    return this.$slots.default ? this.$slots.default[0] : null;
  }
}