import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { createVNode as _createVNode } from "vue";
import _extends from "@babel/runtime/helpers/esm/extends";
var __rest = this && this.__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
import { defineComponent } from 'vue';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import { filterEmpty } from '../_util/props-util';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import TimelineItem from './TimelineItem';
import LoadingOutlined from "@ant-design/icons-vue/es/icons/LoadingOutlined";
import { tuple, booleanType, arrayType } from '../_util/type';
import useConfigInject from '../config-provider/hooks/useConfigInject';
// CSSINJS
import useStyle from './style';
import devWarning from '../vc-util/devWarning';
export const timelineProps = () => ({
  prefixCls: String,
  /** 指定最后一个幽灵节点是否存在或内容 */
  pending: PropTypes.any,
  pendingDot: PropTypes.any,
  reverse: booleanType(),
  mode: PropTypes.oneOf(tuple('left', 'alternate', 'right', '')),
  items: arrayType()
});
function getItems(items, children) {
  if (items && Array.isArray(items)) {
    return [...items];
  }
  return filterEmpty(children).map(ele => {
    var _a, _b;
    return _extends(_extends({}, ele.props), {
      children: (_b = (_a = ele.children) === null || _a === void 0 ? void 0 : _a.default) === null || _b === void 0 ? void 0 : _b.call(_a)
    });
  });
}
export default defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: 'ATimeline',
  inheritAttrs: false,
  props: initDefaultProps(timelineProps(), {
    reverse: false,
    mode: ''
  }),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    var _a;
    const {
      prefixCls,
      direction
    } = useConfigInject('timeline', props);
    // =================== Warning =====================
    devWarning(!((_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots)), 'Timeline', '`Timeline.Item` is deprecated. Please use `items` prop instead.');
    // style
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const getPositionCls = (position, idx) => {
      if (props.mode === 'alternate') {
        if (position === 'right') return `${prefixCls.value}-item-right`;
        if (position === 'left') return `${prefixCls.value}-item-left`;
        return idx % 2 === 0 ? `${prefixCls.value}-item-left` : `${prefixCls.value}-item-right`;
      }
      if (props.mode === 'left') return `${prefixCls.value}-item-left`;
      if (props.mode === 'right') return `${prefixCls.value}-item-right`;
      if (position === 'right') return `${prefixCls.value}-item-right`;
      return '';
    };
    return () => {
      var _a, _b, _c;
      const {
        pending = (_a = slots.pending) === null || _a === void 0 ? void 0 : _a.call(slots),
        pendingDot = (_b = slots.pendingDot) === null || _b === void 0 ? void 0 : _b.call(slots),
        reverse,
        mode
      } = props;
      const pendingNode = typeof pending === 'boolean' ? null : pending;
      const mergedItems = getItems(props.items, ((_c = slots.default) === null || _c === void 0 ? void 0 : _c.call(slots)) || []);
      if (pending) {
        mergedItems.push({
          pending: !!pending,
          dot: pendingDot !== null && pendingDot !== void 0 ? pendingDot : _createVNode(LoadingOutlined, null, null),
          children: pendingNode
        });
      }
      if (reverse) {
        mergedItems.reverse();
      }
      const itemsCount = mergedItems.length;
      const lastCls = `${prefixCls.value}-item-last`;
      const items = mergedItems.map((item, idx) => {
        var _a;
        const pendingClass = idx === itemsCount - 2 ? lastCls : '';
        const readyClass = idx === itemsCount - 1 ? lastCls : '';
        const {
            class: itemClassName,
            children
          } = item,
          itemProps = __rest(item, ["class", "children"]);
        return _createVNode(TimelineItem, _objectSpread(_objectSpread({}, itemProps), {}, {
          "class": classNames([itemClassName, !reverse && !!pending ? pendingClass : readyClass, getPositionCls((_a = item === null || item === void 0 ? void 0 : item.position) !== null && _a !== void 0 ? _a : '', idx)]),
          "key": (item === null || item === void 0 ? void 0 : item.key) || idx
        }), {
          default: () => [children]
        });
      });
      const hasLabelItem = mergedItems.some(item => !!(item === null || item === void 0 ? void 0 : item.label));
      const classString = classNames(prefixCls.value, {
        [`${prefixCls.value}-pending`]: !!pending,
        [`${prefixCls.value}-reverse`]: !!reverse,
        [`${prefixCls.value}-${mode}`]: !!mode && !hasLabelItem,
        [`${prefixCls.value}-label`]: hasLabelItem,
        [`${prefixCls.value}-rtl`]: direction.value === 'rtl'
      }, attrs.class, hashId.value);
      return wrapSSR(_createVNode("ul", _objectSpread(_objectSpread({}, attrs), {}, {
        "class": classString
      }), [items]));
    };
  }
});