import { isVNode } from 'vue';
import { isNil } from 'lodash-es';
import { isValidElementNode } from '../_util/vnode';
import { prefixCls, COMPONENT_NAME } from './const';

const cls = className => `${prefixCls}-${className}`;
const dotCls = className => `.${cls(className)}`;
const warn = function () {
  for (var _len = arguments.length, log = new Array(_len), _key = 0; _key < _len; _key++) {
    log[_key] = arguments[_key];
  }
  return console.warn(`[${COMPONENT_NAME}]:`, ...log);
};
const isPresetIconTypes = icon => {
  const preset = ['info', 'success', 'warning', 'error'];
  return preset.includes(icon);
};
const getTitleOppositePosition = position => {
  return position === 'start' ? 'end' : 'start';
};
const isValidRenderResult = result => {
  if (Array.isArray(result)) {
    return result.some(node => isValidRenderResult(node));
  }
  if (isVNode(result)) {
    return isValidElementNode(result);
  }
  return !isNil(result);
};
const calcTitlePosition = (index, propPosition, nodePosition) => {
  // 组件设置的布局方式优先级最高
  if (propPosition !== 'alternate') {
    return propPosition;
  }
  // 只有设置了交叉布局，node 自定义的位置才会生效
  if (nodePosition) {
    return nodePosition;
  }
  // 交叉布局且 node 未自定义时，交替排列
  return index % 2 === 0 ? 'start' : 'end';
};
const calcDescPosition = (direction, descPosition) => {
  if ((direction === 'row' || direction === 'row-reverse') && descPosition === 'inline') {
    warn('横向时间轴中不支持标题和辅助描述同行');
    return 'under';
  }
  return descPosition;
};
const calcInlineStartProp = axisDirection => {
  if (axisDirection === 'row') {
    return 'left';
  }
  if (axisDirection === 'row-reverse') {
    return 'right';
  }
  return 'top';
};
const calcLengthProp = axisDirection => {
  return axisDirection === 'column' ? 'height' : 'width';
};
const calcDimensionProp = axisDirection => {
  return axisDirection === 'column' ? 'height' : 'width';
};

export { calcDescPosition, calcDimensionProp, calcInlineStartProp, calcLengthProp, calcTitlePosition, cls, dotCls, getTitleOppositePosition, isPresetIconTypes, isValidRenderResult, warn };
