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" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) 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; }
import React, { Fragment, forwardRef, useRef, useImperativeHandle } from 'react';
import getSpaceDates from "./getSpaceDates";
import Item from "./../Item";

// 内库使用-start
import DateUtil from "./../../../utils/DateUtil";
// 内库使用-end

/* 测试使用-start
import { DateUtil } from 'seedsui-react'
测试使用-end */

// List
var List = function List(_ref, ref) {
  var _ref$timeSpace = _ref.timeSpace,
    timeSpace = _ref$timeSpace === void 0 ? 60000 : _ref$timeSpace,
    value = _ref.value,
    list = _ref.list,
    checkable = _ref.checkable,
    checkbox = _ref.checkbox,
    checkboxProps = _ref.checkboxProps,
    checkboxPosition = _ref.checkboxPosition,
    _onChange = _ref.onChange;
  // Expose
  var rootRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      rootDOM: rootRef.current,
      getRootDOM: function getRootDOM() {
        return rootRef.current;
      }
    };
  });

  // 获取单项
  function getItemNode(item, index) {
    var _item$id, _value$findIndex;
    return /*#__PURE__*/React.createElement(Item, {
      key: (_item$id = item.id) !== null && _item$id !== void 0 ? _item$id : index,
      itemData: item,
      checkable: checkable,
      checkbox: item.checkbox || checkbox,
      checkboxProps: checkboxProps,
      checkboxPosition: item.checkboxPosition || checkboxPosition,
      position: item.position,
      avatar: item.avatar,
      author: item.name,
      content: item.content,
      checked: (value === null || value === void 0 || (_value$findIndex = value.findIndex) === null || _value$findIndex === void 0 ? void 0 : _value$findIndex.call(value, function (valueItem) {
        return (valueItem === null || valueItem === void 0 ? void 0 : valueItem.id) === item.id;
      })) >= 0,
      onChange: function onChange(checked) {
        var newValue = null;
        if (!checked) {
          newValue = value.filter(function (valueItem) {
            return (valueItem === null || valueItem === void 0 ? void 0 : valueItem.id) !== item.id;
          });
        } else {
          newValue = [].concat(_toConsumableArray(value || []), [item]);
        }
        _onChange && _onChange(newValue);
      }
    });
  }

  // 时间分栏
  var dates = [];
  return /*#__PURE__*/React.createElement("div", {
    className: "chat-list",
    ref: rootRef
  }, list.map(function (item, index) {
    var bar = null;
    if (item.time) {
      var spaceDates = getSpaceDates(item.time, dates, timeSpace);
      dates = spaceDates.dates;
      // 超过时间间隔，则显示时间分栏
      if (spaceDates.isOverTime) {
        bar = /*#__PURE__*/React.createElement("div", {
          className: "chat-divider-time"
        }, DateUtil.format(item.time, 'YYYY-MM-DD hh:mm'));
      }
    }
    return /*#__PURE__*/React.createElement(Fragment, {
      key: index
    }, bar, getItemNode(item, index));
  }));
};
export default /*#__PURE__*/forwardRef(List);