import * as _vue from "vue";

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; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import PropTypes from '../_util/vue-types';
import classNames from '../_util/classNames';
import { getComponent, isStringElement, isEmptyElement, getSlot } from '../_util/props-util';
import { Col } from '../grid';
import { ConfigConsumerProps } from '../config-provider';
import { ListGridType } from './index';
import { cloneElement } from '../_util/vnode';
import { inject } from 'vue';
export var ListItemProps = {
  prefixCls: PropTypes.string,
  extra: PropTypes.any,
  actions: PropTypes.arrayOf(PropTypes.any),
  grid: ListGridType
};
export var ListItemMetaProps = {
  avatar: PropTypes.any,
  description: PropTypes.any,
  prefixCls: PropTypes.string,
  title: PropTypes.any
};
export var ListItemMeta = function ListItemMeta(props, _ref) {
  var _slots$avatar, _slots$title, _slots$description;

  var slots = _ref.slots;
  var configProvider = inject('configProvider', ConfigConsumerProps);
  var getPrefixCls = configProvider.getPrefixCls;
  var customizePrefixCls = props.prefixCls;
  var prefixCls = getPrefixCls('list', customizePrefixCls);
  var avatar = props.avatar || ((_slots$avatar = slots.avatar) === null || _slots$avatar === void 0 ? void 0 : _slots$avatar.call(slots));
  var title = props.title || ((_slots$title = slots.title) === null || _slots$title === void 0 ? void 0 : _slots$title.call(slots));
  var description = props.description || ((_slots$description = slots.description) === null || _slots$description === void 0 ? void 0 : _slots$description.call(slots));

  var content = _vue.createVNode("div", {
    "class": "".concat(prefixCls, "-item-meta-content")
  }, [title && _vue.createVNode("h4", {
    "class": "".concat(prefixCls, "-item-meta-title")
  }, [title]), description && _vue.createVNode("div", {
    "class": "".concat(prefixCls, "-item-meta-description")
  }, [description])]);

  return _vue.createVNode("div", {
    "class": "".concat(prefixCls, "-item-meta")
  }, [avatar && _vue.createVNode("div", {
    "class": "".concat(prefixCls, "-item-meta-avatar")
  }, [avatar]), (title || description) && content]);
};

_extends(ListItemMeta, {
  props: ListItemMetaProps,
  __ANT_LIST_ITEM_META: true,
  displayName: 'AListItemMeta'
});

function getGrid(grid, t) {
  return grid[t] && Math.floor(24 / grid[t]);
}

export default {
  name: 'AListItem',
  inheritAttrs: false,
  Meta: ListItemMeta,
  props: ListItemProps,
  setup: function setup() {
    var listContext = inject('listContext', {});
    var configProvider = inject('configProvider', ConfigConsumerProps);
    return {
      listContext: listContext,
      configProvider: configProvider
    };
  },
  methods: {
    isItemContainsTextNodeAndNotSingular: function isItemContainsTextNodeAndNotSingular() {
      var children = getSlot(this) || [];
      var result;
      children.forEach(function (element) {
        if (isStringElement(element) && !isEmptyElement(element)) {
          result = true;
        }
      });
      return result && children.length > 1;
    },
    isFlexMode: function isFlexMode() {
      var extra = getComponent(this, 'extra');
      var itemLayout = this.listContext.itemLayout;

      if (itemLayout === 'vertical') {
        return !!extra;
      }

      return !this.isItemContainsTextNodeAndNotSingular();
    }
  },
  render: function render() {
    var _this$listContext = this.listContext,
        grid = _this$listContext.grid,
        itemLayout = _this$listContext.itemLayout;
    var customizePrefixCls = this.prefixCls,
        $attrs = this.$attrs;

    var _className = $attrs.class,
        restAttrs = _objectWithoutProperties($attrs, ["class"]);

    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('list', customizePrefixCls);
    var extra = getComponent(this, 'extra');
    var actions = getComponent(this, 'actions');
    actions = actions && !Array.isArray(actions) ? [actions] : actions;

    var actionsContent = actions && actions.length > 0 && _vue.createVNode("ul", {
      "class": "".concat(prefixCls, "-item-action"),
      "key": "actions"
    }, [actions.map(function (action, i) {
      return _vue.createVNode("li", {
        "key": "".concat(prefixCls, "-item-action-").concat(i)
      }, [action, i !== actions.length - 1 && _vue.createVNode("em", {
        "class": "".concat(prefixCls, "-item-action-split")
      }, null)]);
    })]);

    var children = getSlot(this);
    var Tag = grid ? 'div' : 'li';

    var itemChildren = _vue.createVNode(Tag, _vue.mergeProps(restAttrs, {
      "class": classNames("".concat(prefixCls, "-item "), _className, _defineProperty({}, "".concat(prefixCls, "-item-no-flex"), !this.isFlexMode()))
    }), {
      default: function _default() {
        return [itemLayout === 'vertical' && extra ? [_vue.createVNode("div", {
          "class": "".concat(prefixCls, "-item-main"),
          "key": "content"
        }, [children, actionsContent]), _vue.createVNode("div", {
          "class": "".concat(prefixCls, "-item-extra"),
          "key": "extra"
        }, [extra])] : [children, actionsContent, cloneElement(extra, {
          key: 'extra'
        })]];
      }
    });

    var mainContent = grid ? _vue.createVNode(Col, {
      "span": getGrid(grid, 'column'),
      "xs": getGrid(grid, 'xs'),
      "sm": getGrid(grid, 'sm'),
      "md": getGrid(grid, 'md'),
      "lg": getGrid(grid, 'lg'),
      "xl": getGrid(grid, 'xl'),
      "xxl": getGrid(grid, 'xxl')
    }, {
      default: function _default() {
        return [itemChildren];
      }
    }) : itemChildren;
    return mainContent;
  }
};