import * as _vue from "vue";

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { inject, cloneVNode } from 'vue';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import { getOptionProps, getPropsData, initDefaultProps, filterEmpty, getComponent } from '../_util/props-util';
import TimelineItem from './TimelineItem';
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import { ConfigConsumerProps } from '../config-provider';
export var TimelineProps = {
  prefixCls: PropTypes.string,

  /** 指定最后一个幽灵节点是否存在或内容 */
  pending: PropTypes.any,
  pendingDot: PropTypes.string,
  reverse: PropTypes.bool,
  mode: PropTypes.oneOf(['left', 'alternate', 'right', ''])
};
export default {
  name: 'ATimeline',
  props: initDefaultProps(TimelineProps, {
    reverse: false,
    mode: ''
  }),
  setup: function setup() {
    var configProvider = inject('configProvider', ConfigConsumerProps);
    return {
      configProvider: configProvider
    };
  },
  render: function render() {
    var _classNames;

    var _getOptionProps = getOptionProps(this),
        customizePrefixCls = _getOptionProps.prefixCls,
        reverse = _getOptionProps.reverse,
        mode = _getOptionProps.mode;

    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('timeline', customizePrefixCls);
    var pendingDot = getComponent(this, 'pendingDot');
    var pending = getComponent(this, 'pending');
    var pendingNode = typeof pending === 'boolean' ? null : pending;
    var classString = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-pending"), !!pending), _defineProperty(_classNames, "".concat(prefixCls, "-reverse"), !!reverse), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(mode), !!mode), _classNames));
    var children = filterEmpty(this.$slots.default && this.$slots.default()); // // Remove falsy items
    // const falsylessItems = filterEmpty(this.$slots.default)
    // const items = falsylessItems.map((item, idx) => {
    //   return cloneElement(item, {
    //     props: {
    //       last: falsylessItems.length - 1 === idx,
    //     },
    //   })
    // })

    var pendingItem = pending ? _vue.createVNode(TimelineItem, {
      "pending": !!pending,
      "dot": pendingDot || _vue.createVNode(LoadingOutlined, null, null)
    }, {
      default: function _default() {
        return [pendingNode];
      }
    }) : null;
    var timeLineItems = reverse ? [pendingItem].concat(_toConsumableArray(children.reverse())) : [].concat(_toConsumableArray(children), [pendingItem]);

    var getPositionCls = function getPositionCls(ele, idx) {
      var eleProps = getPropsData(ele);

      if (mode === 'alternate') {
        if (eleProps.position === 'right') return "".concat(prefixCls, "-item-right");
        if (eleProps.position === 'left') return "".concat(prefixCls, "-item-left");
        return idx % 2 === 0 ? "".concat(prefixCls, "-item-left") : "".concat(prefixCls, "-item-right");
      }

      if (mode === 'left') return "".concat(prefixCls, "-item-left");
      if (mode === 'right') return "".concat(prefixCls, "-item-right");
      if (eleProps.position === 'right') return "".concat(prefixCls, "-item-right");
      return '';
    }; // Remove falsy items


    var truthyItems = timeLineItems.filter(function (item) {
      return !!item;
    });
    var itemsCount = truthyItems.length;
    var lastCls = "".concat(prefixCls, "-item-last");
    var items = truthyItems.map(function (ele, idx) {
      var pendingClass = idx === itemsCount - 2 ? lastCls : '';
      var readyClass = idx === itemsCount - 1 ? lastCls : '';
      return cloneVNode(ele, {
        class: classNames([!reverse && !!pending ? pendingClass : readyClass, getPositionCls(ele, idx)])
      });
    });
    var timelineProps = {
      class: classString
    };
    return _vue.createVNode("ul", timelineProps, [items]);
  }
};