import XEUtils from 'xe-utils';
import GlobalConfig from '../v-table/src/conf';
import DomZIndex from 'dom-zindex';
import { warnLog, errLog } from '../tools/log';

export function isEnableConf(conf) {
  return conf && conf.enabled !== false;
}

/**
 * 判断值为：'' | null | undefined 时都属于空值
 */
export function eqEmptyValue(cellValue) {
  return cellValue === '' || XEUtils.eqNull(cellValue);
}

export function getFuncText(content) {
  return XEUtils.isFunction(content) ? content() : (GlobalConfig.translate ? GlobalConfig.translate(content) : content);
}

// 获取所有的列，排除分组
export function getColumnList(columns) {
  const result = [];
  columns.forEach(column => {
    result.push(...(column.children && column.children.length ? getColumnList(column.children) : [column]));
  });
  return result;
}

export const UtilTools = {
  nextZIndex() {
    return DomZIndex.getNext();
  },
  getLastZIndex() {
    return DomZIndex.getCurrent();
  },
  getColumnList,
  getClass(property, params) {
    return property ? XEUtils.isFunction(property) ? property(params) : property : '';
  },
  formatText(value, placeholder) {
    return '' + (value === '' || value === null || value === undefined ? (placeholder ? GlobalConfig.emptyCell : '') : value);
  },
  getCellValue(row, column) {
    return XEUtils.get(row, column.prop);
  },
  setCellValue(row, column, value) {
    return XEUtils.set(row, column.prop, value);
  },
  // 组装列配置
  assemColumn(_vm) {
    const { $el, $tablePlus, $xecolumn, columnConfig } = _vm;
    const groupConfig = $xecolumn ? $xecolumn.columnConfig : null;
    columnConfig.slots = _vm.$scopedSlots;
    if (groupConfig) {
      if (process.env.NODE_ENV === 'development') {
        if ($xecolumn.$options._componentTag === 'el-table-plus-column') {
          errLog('tablePlus.error.groupTag', [`<el-table-plus-colgroup title=${$xecolumn.title} ...>`, `<el-table-plus-column title=${$xecolumn.title} ...>`]);
        } else if ($xecolumn.$options._componentTag === 'table-plus-column') {
          warnLog('tablePlus.error.groupTag', [`<table-plus-colgroup title=${$xecolumn.title} ...>`, `<table-plus-column title=${$xecolumn.title} ...>`]);
        }
      }
      if (!groupConfig.children) {
        groupConfig.children = [];
      }
      groupConfig.children.splice([].indexOf.call($xecolumn.$el.children, $el), 0, columnConfig);
    } else {
      $tablePlus.staticColumns.splice([].indexOf.call($tablePlus.$refs.hideColumn.children, $el), 0, columnConfig);
    }
  },
  // 销毁列
  destroyColumn(_vm) {
    const { $tablePlus, columnConfig } = _vm;
    const matchObj = XEUtils.findTree($tablePlus.staticColumns, column => column === columnConfig);
    if (matchObj) {
      matchObj.items.splice(matchObj.index, 1);
    }
  },
  hasChildrenList(item) {
    return item && item.children && item.children.length > 0;
  },
  parseFile(file) {
    const name = file.name;
    const tIndex = XEUtils.lastIndexOf(name, '.');
    const type = name.substring(tIndex + 1, name.length).toLowerCase();
    const filename = name.substring(0, tIndex);
    return { filename, type };
  },
  isNumVal(num) {
    return !isNaN(parseFloat('' + num));
  }
};

export default UtilTools;
