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 _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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useMemo } from "react";
import { locales } from "@pisell/utils";
import classNames from "classnames";
import "./index.less";
var MAX_LIMIT = 3;
var CollapsibleList = function CollapsibleList(props) {
  var _props$list = props.list,
    list = _props$list === void 0 ? [] : _props$list,
    renderItem = props.renderItem,
    className = props.className,
    style = props.style,
    border = props.border;
  var _useState = useState(MAX_LIMIT),
    _useState2 = _slicedToArray(_useState, 2),
    maxLimit = _useState2[0],
    setMaxLimit = _useState2[1];

  /**
   * @title: 展开和收起
   * @description:
   * @param {any} d
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-01-30 17:08
   */
  var handlePreviewAll = function handlePreviewAll() {
    var _maxLimit = maxLimit;
    if (_maxLimit === MAX_LIMIT) {
      _maxLimit = 99;
    } else {
      _maxLimit = MAX_LIMIT;
    }
    setMaxLimit(_maxLimit);
  };

  /**
   * @title: 是否展开全部
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-01-30 17:08
   */
  var showAll = useMemo(function () {
    return list.length > MAX_LIMIT;
  }, [list]);

  /**
   * @title: 展开文案
   * @description:
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-01-30 17:08
   */
  var viewAllText = useMemo(function () {
    var _maxLimit = maxLimit;
    if (_maxLimit === MAX_LIMIT) {
      return locales.getText("pisell2.text.view-all");
    }
    return locales.getText("pisell2.text.view-less");
  }, [maxLimit]);
  var _list = useMemo(function () {
    var arr = _toConsumableArray(list);
    // 默认展示3条, 点击更多时展示全部
    var _maxLimit = maxLimit;
    if (list.length > _maxLimit) {
      arr = arr.slice(0, _maxLimit);
    }
    return arr;
  }, [list, maxLimit]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames("pisell2-collapsible-list-wrap", className),
    style: style
  }, border ? /*#__PURE__*/React.createElement("div", {
    className: "pisell2-collapsible-list-top-border"
  }) : null, /*#__PURE__*/React.createElement("div", {
    className: "pisell2-collapsible-list"
  }, _list.map(function (d, i) {
    return renderItem(d, i);
  })), showAll ? /*#__PURE__*/React.createElement("div", {
    className: "pisell2-collapsible-list-view-all",
    onClick: function onClick(e) {
      e.preventDefault();
      e.stopPropagation();
      handlePreviewAll();
    }
  }, viewAllText) : null);
};
export default CollapsibleList;