var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import Checkbox from "../../checkbox";
import { useClickAway } from 'react-use';
import "../style/list.css";
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Input } from "../../input";
import VirtualizedList from "../../virtualized-list";
export var virtualizedListWrapperClassName = 'virtualized-list-wrapper';
var prefixCls = 'cascade-modified-list';
var List = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _a = props.showCheckbox,
      showCheckbox = _a === void 0 ? false : _a,
      _b = props.selectAll,
      selectAll = _b === void 0 ? true : _b,
      _c = props.selectAllText,
      selectAllText = _c === void 0 ? '全选' : _c,
      options = props.options,
      handleMouseEnterLine = props.handleMouseEnterLine,
      handleMouseLeaveLine = props.handleMouseLeaveLine,
      renderer = props.renderer,
      handleCheckboxChange = props.handleCheckboxChange,
      handleCheckedAllChange = props.handleCheckedAllChange,
      checkedStatus = props.checkedStatus,
      levelTitles = props.levelTitles,
      level = props.level,
      showSearch = props.showSearch,
      _d = props.ignoreCase,
      ignoreCase = _d === void 0 ? false : _d,
      _e = props.searchInputPlaceholder,
      searchInputPlaceholder = _e === void 0 ? '' : _e,
      _f = props.triggerType,
      triggerType = _f === void 0 ? 'hover' : _f,
      rowWidth = props.rowWidth,
      rowMinWidth = props.rowMinWidth,
      rowMaxWidth = props.rowMaxWidth;
  var levelTitle = level != null ? levelTitles === null || levelTitles === void 0 ? void 0 : levelTitles[level] : '' || '';

  var _g = useState(false),
      allChecked = _g[0],
      setAllChecked = _g[1];

  var _h = useState(false),
      allCheckedIndeterminate = _h[0],
      setAllCheckedIndeterminate = _h[1];

  var _j = useState(options),
      _options = _j[0],
      setOptions = _j[1];

  var listRef = useRef(null);
  var fallbackOptions = useRef(options);
  var style = useMemo(function () {
    var _style = {};

    if (rowWidth) {
      _style.width = typeof rowWidth === 'number' ? rowWidth + "px" : rowWidth;
    }

    if (rowMinWidth) {
      _style.minWidth = typeof rowMinWidth === 'number' ? rowMinWidth + "px" : rowMinWidth;
    }

    if (rowMaxWidth) {
      _style.maxWidth = typeof rowMaxWidth === 'number' ? rowMaxWidth + "px" : rowMaxWidth;
    }

    return _style;
  }, [rowWidth, rowMinWidth, rowMaxWidth]);
  var handleTriggerMenu = useCallback(function (e) {
    var currentTarget = e.currentTarget;
    var _a = currentTarget.dataset,
        _b = _a.val,
        val = _b === void 0 ? '' : _b,
        index = _a.index,
        _c = _a.id,
        id = _c === void 0 ? '' : _c;
    handleMouseEnterLine === null || handleMouseEnterLine === void 0 ? void 0 : handleMouseEnterLine(currentTarget.getBoundingClientRect(), id, val, index ? Number.parseInt(index, 10) : -1, currentTarget);
  }, [handleMouseEnterLine]);
  var handleCheckedChange = useCallback(function (id, _id, val, checked) {
    handleCheckboxChange === null || handleCheckboxChange === void 0 ? void 0 : handleCheckboxChange(id, _id, val, checked);
  }, [handleCheckboxChange]);

  var _handleCheckedAllChange = function _handleCheckedAllChange(checked) {
    setAllChecked(checked);
    setAllCheckedIndeterminate(false);
    handleCheckedAllChange === null || handleCheckedAllChange === void 0 ? void 0 : handleCheckedAllChange(checked);
  };

  var handleSearchChange = function handleSearchChange(e) {
    var value = e.target.value;

    if (value.length) {
      setOptions(options.filter(function (i) {
        return ignoreCase ? i.val.toLocaleLowerCase().includes(value.toLocaleLowerCase()) : i.val.includes(value);
      }));
    } else {
      setOptions(fallbackOptions.current);
    }
  };

  useEffect(function () {
    var checkedCnt = checkedStatus.checkedCnt;

    if (checkedCnt === 0) {
      setAllCheckedIndeterminate(false);
      setAllChecked(false);
    } else {
      if (checkedCnt > 0) {
        setAllCheckedIndeterminate(true);
        setAllChecked(checkedCnt === options.length);
      }
    }
  }, [checkedStatus, level, options.length]);
  useEffect(function () {
    setOptions(options);
    fallbackOptions.current = options;
  }, [options]);
  useClickAway(listRef, function (e) {
    var isClickAway = true;

    if (e.target instanceof Node) {
      var el = e.target; // 是否是某一列表的子元素

      while (el) {
        if (el instanceof HTMLElement && el.classList.contains(prefixCls)) {
          isClickAway = false;
          break;
        }

        el = el.parentElement;
      }
    }

    if (isClickAway) {
      handleMouseLeaveLine === null || handleMouseLeaveLine === void 0 ? void 0 : handleMouseLeaveLine();
    }
  });
  var otherProps = useMemo(function () {
    var otherProps = {};

    if (triggerType === 'hover') {
      otherProps.onMouseEnter = handleTriggerMenu;
      otherProps.onMouseLeave = handleMouseLeaveLine;
    } else {
      otherProps.onClick = function (e) {
        handleTriggerMenu(e);
      };
    }

    return otherProps;
  }, [handleMouseLeaveLine, handleTriggerMenu, triggerType]);
  var rowRenderer = useCallback(function (_a) {
    var _b, _c;

    var index = _a.index,
        item = _a.item;
    var id = item.id,
        val = item.val,
        _id = item._id;
    var checked = (_b = checkedStatus.checkedObj[id]) !== null && _b !== void 0 ? _b : false;
    var indeterminate = (_c = checkedStatus.indeterminateObj[id]) !== null && _c !== void 0 ? _c : false;
    return _jsxs("div", __assign({
      "data-id": id,
      "data-val": val,
      "data-index": index,
      className: prefixCls + "-item",
      onClick: function onClick(e) {
        // 阻止因 React Portal 事件冒泡导致的级联点击
        if (e.currentTarget.contains(e.target)) {
          handleCheckedChange(id, _id, val, !checked);
        }
      }
    }, otherProps, {
      children: [showCheckbox && _jsx(Checkbox, {
        checked: checked,
        indeterminate: indeterminate,
        onClick: function onClick(checked) {
          if (triggerType === 'click') {
            handleCheckedChange(id, _id, val, !checked);
          }
        }
      }, void 0), renderer ? _jsx(_Fragment, {
        children: renderer(id, _id, val, checked, item, index)
      }, void 0) : _jsx("span", {
        children: val
      }, void 0)]
    }), id + val + index);
  }, [checkedStatus.checkedObj, checkedStatus.indeterminateObj, handleCheckedChange, otherProps, renderer, showCheckbox, triggerType]);
  return _jsxs("div", __assign({
    className: prefixCls,
    style: style,
    ref: listRef
  }, {
    children: [levelTitle && _jsx("span", __assign({
      className: "title-wrapper"
    }, {
      children: _jsx("span", __assign({
        className: "title"
      }, {
        children: levelTitle
      }), void 0)
    }), void 0), showSearch && _jsx(Input, {
      placeholder: searchInputPlaceholder,
      prefixIcon: "search",
      onChange: handleSearchChange
    }, void 0), selectAll && _jsx("div", __assign({
      className: "select-all"
    }, {
      children: _jsx(Checkbox, {
        className: "selectAll",
        checked: allChecked,
        indeterminate: allCheckedIndeterminate,
        onChange: _handleCheckedAllChange,
        title: selectAllText
      }, void 0)
    }), void 0), _jsx("div", __assign({
      className: virtualizedListWrapperClassName,
      ref: ref
    }, {
      children: _jsx(VirtualizedList, {
        minRowHeight: 36,
        dynamicRowHeight: true,
        data: _options,
        ItemView: rowRenderer,
        useScrollbar: false,
        rowWidth: typeof rowWidth === 'number' ? rowWidth : undefined
      }, void 0)
    }), void 0)]
  }), void 0);
}));
export default List;

