"use strict";

var __assign = void 0 && (void 0).__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);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.virtualizedListWrapperClassName = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var checkbox_1 = __importDefault(require("../../checkbox"));

var react_use_1 = require("react-use");

require("../style/list.css");

var react_1 = require("react");

var input_1 = require("../../input");

var virtualized_list_1 = __importDefault(require("../../virtualized-list"));

exports.virtualizedListWrapperClassName = 'virtualized-list-wrapper';
var prefixCls = 'cascade-modified-list';
var List = React.memo(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 = react_1.useState(false),
      allChecked = _g[0],
      setAllChecked = _g[1];

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

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

  var listRef = react_1.useRef(null);
  var fallbackOptions = react_1.useRef(options);
  var style = react_1.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 = react_1.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 = react_1.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);
    }
  };

  react_1.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]);
  react_1.useEffect(function () {
    setOptions(options);
    fallbackOptions.current = options;
  }, [options]);
  react_use_1.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 = react_1.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 = react_1.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 jsx_runtime_1.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_runtime_1.jsx(checkbox_1["default"], {
        checked: checked,
        indeterminate: indeterminate,
        onClick: function onClick(checked) {
          if (triggerType === 'click') {
            handleCheckedChange(id, _id, val, !checked);
          }
        }
      }, void 0), renderer ? jsx_runtime_1.jsx(jsx_runtime_1.Fragment, {
        children: renderer(id, _id, val, checked, item, index)
      }, void 0) : jsx_runtime_1.jsx("span", {
        children: val
      }, void 0)]
    }), id + val + index);
  }, [checkedStatus.checkedObj, checkedStatus.indeterminateObj, handleCheckedChange, otherProps, renderer, showCheckbox, triggerType]);
  return jsx_runtime_1.jsxs("div", __assign({
    className: prefixCls,
    style: style,
    ref: listRef
  }, {
    children: [levelTitle && jsx_runtime_1.jsx("span", __assign({
      className: "title-wrapper"
    }, {
      children: jsx_runtime_1.jsx("span", __assign({
        className: "title"
      }, {
        children: levelTitle
      }), void 0)
    }), void 0), showSearch && jsx_runtime_1.jsx(input_1.Input, {
      placeholder: searchInputPlaceholder,
      prefixIcon: "search",
      onChange: handleSearchChange
    }, void 0), selectAll && jsx_runtime_1.jsx("div", __assign({
      className: "select-all"
    }, {
      children: jsx_runtime_1.jsx(checkbox_1["default"], {
        className: "selectAll",
        checked: allChecked,
        indeterminate: allCheckedIndeterminate,
        onChange: _handleCheckedAllChange,
        title: selectAllText
      }, void 0)
    }), void 0), jsx_runtime_1.jsx("div", __assign({
      className: exports.virtualizedListWrapperClassName,
      ref: ref
    }, {
      children: jsx_runtime_1.jsx(virtualized_list_1["default"], {
        minRowHeight: 36,
        dynamicRowHeight: true,
        data: _options,
        ItemView: rowRenderer,
        useScrollbar: false,
        rowWidth: typeof rowWidth === 'number' ? rowWidth : undefined
      }, void 0)
    }), void 0)]
  }), void 0);
}));
exports["default"] = List;

