"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.timelineProps = exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _vue = require("vue");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classNames = _interopRequireDefault(require("../_util/classNames"));
var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));
var _propsUtil = require("../_util/props-util");
var _initDefaultProps = _interopRequireDefault(require("../_util/props-util/initDefaultProps"));
var _TimelineItem = _interopRequireDefault(require("./TimelineItem"));
var _LoadingOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/LoadingOutlined"));
var _type = require("../_util/type");
var _useConfigInject = _interopRequireDefault(require("../config-provider/hooks/useConfigInject"));
var _style = _interopRequireDefault(require("./style"));
var _devWarning = _interopRequireDefault(require("../vc-util/devWarning"));
var __rest = void 0 && (void 0).__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;
};

// CSSINJS

const timelineProps = () => ({
  prefixCls: String,
  /** 指定最后一个幽灵节点是否存在或内容 */
  pending: _vueTypes.default.any,
  pendingDot: _vueTypes.default.any,
  reverse: (0, _type.booleanType)(),
  mode: _vueTypes.default.oneOf((0, _type.tuple)('left', 'alternate', 'right', '')),
  items: (0, _type.arrayType)()
});
exports.timelineProps = timelineProps;
function getItems(items, children) {
  if (items && Array.isArray(items)) {
    return [...items];
  }
  return (0, _propsUtil.filterEmpty)(children).map(ele => {
    var _a, _b;
    return (0, _extends2.default)((0, _extends2.default)({}, ele.props), {
      children: (_b = (_a = ele.children) === null || _a === void 0 ? void 0 : _a.default) === null || _b === void 0 ? void 0 : _b.call(_a)
    });
  });
}
var _default = exports.default = (0, _vue.defineComponent)({
  compatConfig: {
    MODE: 3
  },
  name: 'ATimeline',
  inheritAttrs: false,
  props: (0, _initDefaultProps.default)(timelineProps(), {
    reverse: false,
    mode: ''
  }),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    var _a;
    const {
      prefixCls,
      direction
    } = (0, _useConfigInject.default)('timeline', props);
    // =================== Warning =====================
    (0, _devWarning.default)(!((_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] = (0, _style.default)(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 : (0, _vue.createVNode)(_LoadingOutlined.default, 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 (0, _vue.createVNode)(_TimelineItem.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, itemProps), {}, {
          "class": (0, _classNames.default)([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 = (0, _classNames.default)(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((0, _vue.createVNode)("ul", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attrs), {}, {
        "class": classString
      }), [items]));
    };
  }
});