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, { useEffect, useMemo, useState } from 'react';
import { SearchOutlined } from '@ant-design/icons';
import PisellViewGrid from "../pisellViewGrid";
import PisellAnchor from "../pisellAnchor";
import PisellEmpty from "../pisellEmpty";
import Input from "../input";
import "./index.less";
var PisellList01 = function PisellList01(_ref) {
  var showSearch = _ref.showSearch,
    showCategory = _ref.showCategory,
    placeholder = _ref.placeholder,
    dataSource = _ref.dataSource,
    renderItem = _ref.renderItem,
    className = _ref.className,
    style = _ref.style,
    _onChange = _ref.onChange,
    onSearch = _ref.onSearch,
    _ref$isShowEmpty = _ref.isShowEmpty,
    isShowEmpty = _ref$isShowEmpty === void 0 ? true : _ref$isShowEmpty,
    description = _ref.description;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    currentAnchor = _useState2[0],
    setCurrentAnchor = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    products = _useState4[0],
    setProducts = _useState4[1];
  useEffect(function () {
    // 不显示分类时，默认展示所有数据
    if (!showCategory) {
      return setProducts(dataSource !== null && dataSource !== void 0 ? dataSource : []);
    }
    if (currentAnchor) {
      var _currentData$children;
      var currentData = dataSource === null || dataSource === void 0 ? void 0 : dataSource.find(function (item) {
        return item.href === currentAnchor;
      });
      setProducts((_currentData$children = currentData === null || currentData === void 0 ? void 0 : currentData.children) !== null && _currentData$children !== void 0 ? _currentData$children : []);
    }
  }, [currentAnchor, dataSource, showCategory]);
  useEffect(function () {
    var _dataSource$0$href, _dataSource$;
    setCurrentAnchor((_dataSource$0$href = dataSource === null || dataSource === void 0 || (_dataSource$ = dataSource[0]) === null || _dataSource$ === void 0 ? void 0 : _dataSource$.href) !== null && _dataSource$0$href !== void 0 ? _dataSource$0$href : '');
  }, [dataSource]);

  /**
   * @title: 分类标题
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-13 16:21
   */
  var categoryTitle = useMemo(function () {
    var _dataSource$find;
    return dataSource === null || dataSource === void 0 || (_dataSource$find = dataSource.find(function (item) {
      return item.href === currentAnchor;
    })) === null || _dataSource$find === void 0 ? void 0 : _dataSource$find.title;
  }, [currentAnchor, dataSource]);
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-list-01 ".concat(className),
    style: style
  }, showSearch ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-list-01-search"
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: placeholder || 'Search',
    prefix: /*#__PURE__*/React.createElement(SearchOutlined, {
      style: {
        color: '#667085'
      }
    }),
    onChange: onSearch,
    allowClear: true,
    size: "large",
    className: "pisell-list-01-search-input"
  })) : null, showCategory ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-list-01-categories"
  }, /*#__PURE__*/React.createElement(PisellAnchor, {
    items: dataSource,
    getCurrentAnchor: currentAnchor,
    onChange: function onChange(e) {
      _onChange === null || _onChange === void 0 || _onChange(e);
      setCurrentAnchor(e);
    }
  })) : null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-list-01-products ".concat(!products.length && isShowEmpty ? 'pisell-list-01-products-empty' : '')
  }, showCategory ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-list-01-products-title"
  }, categoryTitle) : null, products.length > 0 ? /*#__PURE__*/React.createElement(PisellViewGrid, {
    dataSource: products,
    columns: 1,
    verticalGap: 16,
    renderItem: renderItem
  }) : isShowEmpty ? /*#__PURE__*/React.createElement(PisellEmpty, {
    description: description
  }) : null));
};
export default PisellList01;