"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = exports.dropdownMenuPropTypes = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Spinner = _interopRequireDefault(require("@rsuite/icons/legacy/Spinner"));

var _DOMHelper = _interopRequireDefault(require("../DOMHelper"));

var _isUndefined = _interopRequireDefault(require("lodash/isUndefined"));

var _isNil = _interopRequireDefault(require("lodash/isNil"));

var _utils = require("../utils");

var _Picker = require("../Picker");

var _AngleLeft = _interopRequireDefault(require("@rsuite/icons/legacy/AngleLeft"));

var _AngleRight = _interopRequireDefault(require("@rsuite/icons/legacy/AngleRight"));

var defaultProps = {
  as: 'div',
  disabledItemValues: [],
  cascadeData: [],
  cascadePaths: [],
  menuWidth: 120,
  menuHeight: 200,
  childrenKey: 'children',
  valueKey: 'value',
  labelKey: 'label'
};

var DropdownMenu = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var Component = props.as,
      activeItemValue = props.activeItemValue,
      classPrefix = props.classPrefix,
      className = props.className,
      childrenKey = props.childrenKey,
      disabledItemValues = props.disabledItemValues,
      menuWidth = props.menuWidth,
      menuHeight = props.menuHeight,
      valueKey = props.valueKey,
      cascadeData = props.cascadeData,
      cascadePaths = props.cascadePaths,
      labelKey = props.labelKey,
      loadingText = props.loadingText,
      renderMenu = props.renderMenu,
      renderMenuItem = props.renderMenuItem,
      onSelect = props.onSelect,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "activeItemValue", "classPrefix", "className", "childrenKey", "disabledItemValues", "menuWidth", "menuHeight", "valueKey", "cascadeData", "cascadePaths", "labelKey", "loadingText", "renderMenu", "renderMenuItem", "onSelect"]);

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      merge = _useClassNames.merge,
      prefix = _useClassNames.prefix;

  var classes = merge(className, prefix('items'));
  var rootRef = (0, _react.useRef)();
  var rtl = (0, _utils.useCustom)('DropdownMenu');
  (0, _react.useEffect)(function () {
    var _rootRef$current;

    var columns = ((_rootRef$current = rootRef.current) === null || _rootRef$current === void 0 ? void 0 : _rootRef$current.querySelectorAll('[data-type="column"]')) || [];
    columns.forEach(function (column) {
      if (!column) {
        return;
      }

      var activeItem = column.querySelector("." + prefix('item-focus'));

      if (!activeItem) {
        activeItem = column.querySelector("." + prefix('item-active'));
      }

      if (activeItem) {
        var position = _DOMHelper.default.getPosition(activeItem, column); // Let the active option scroll into view.


        _DOMHelper.default.scrollTop(column, position.top);
      }
    });
  }, [prefix]);
  var getCascadeItems = (0, _react.useCallback)(function (items, layer, node, isLeafNode) {
    var data = [];
    var paths = [];

    for (var i = 0; i < cascadeData.length && i < layer; i += 1) {
      data.push(cascadeData[i]);

      if (i < layer - 1 && cascadePaths) {
        paths.push(cascadePaths[i]);
      }
    }

    paths.push(node);

    if (!isLeafNode) {
      data.push(items);
    }

    return {
      cascadeData: data,
      cascadePaths: paths
    };
  }, [cascadeData, cascadePaths]);

  var handleSelect = function handleSelect(layer, node, event) {
    var children = node[childrenKey];
    var isLeafNode = (0, _isNil.default)(children);
    var items = (children || []).map(function (item) {
      return (0, _extends2.default)({}, item, {
        parent: node
      });
    });

    var _getCascadeItems = getCascadeItems(items, layer + 1, node, isLeafNode),
        cascadeData = _getCascadeItems.cascadeData,
        cascadePaths = _getCascadeItems.cascadePaths;

    onSelect === null || onSelect === void 0 ? void 0 : onSelect(node, cascadeData, cascadePaths, isLeafNode, event);
  };

  var renderCascadeNode = function renderCascadeNode(node, index, layer, focus) {
    var children = node[childrenKey];
    var value = node[valueKey];
    var label = node[labelKey];
    var disabled = disabledItemValues.some(function (disabledValue) {
      return (0, _utils.shallowEqual)(disabledValue, value);
    }); // Use `value` in keys when If `value` is string or number

    var onlyKey = typeof value === 'number' || typeof value === 'string' ? value : index;
    var Icon = rtl ? _AngleRight.default : _AngleLeft.default;
    return /*#__PURE__*/_react.default.createElement(_Picker.DropdownMenuItem, {
      classPrefix: "picker-cascader-menu-item",
      as: 'li',
      key: layer + "-" + onlyKey,
      disabled: disabled,
      active: !(0, _isUndefined.default)(activeItemValue) && (0, _utils.shallowEqual)(activeItemValue, value),
      focus: focus,
      value: value,
      className: children ? prefix('has-children') : undefined,
      onSelect: function onSelect(_value, event) {
        return handleSelect(layer, node, event);
      }
    }, renderMenuItem ? renderMenuItem(label, node) : label, children ? /*#__PURE__*/_react.default.createElement(Icon, {
      className: prefix('caret')
    }) : null);
  };

  var styles = {
    width: cascadeData.length * menuWidth
  };
  var cascadeNodes = cascadeData.map(function (children, layer) {
    var onlyKey = layer + "_" + children.length;

    var menu = /*#__PURE__*/_react.default.createElement("ul", {
      role: "listbox"
    }, children.map(function (item, index) {
      return renderCascadeNode(item, index, layer, cascadePaths[layer] && (0, _utils.shallowEqual)(cascadePaths[layer][valueKey], item[valueKey]));
    }));

    var parentNode = cascadePaths[layer - 1];

    var renderMenuElement = function renderMenuElement() {
      if (renderMenu) {
        return renderMenu(children, menu, parentNode);
      }

      return (parentNode === null || parentNode === void 0 ? void 0 : parentNode.loading) ? /*#__PURE__*/_react.default.createElement("div", {
        className: prefix('column-loading')
      }, /*#__PURE__*/_react.default.createElement(_Spinner.default, {
        spin: true
      }), " ", /*#__PURE__*/_react.default.createElement("span", null, loadingText)) : menu;
    };

    var node = /*#__PURE__*/_react.default.createElement("div", {
      key: onlyKey,
      className: prefix('column'),
      "data-layer": layer,
      "data-type": 'column',
      style: {
        height: menuHeight,
        width: menuWidth
      }
    }, renderMenuElement());

    return node;
  });
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, rest, {
    ref: (0, _utils.mergeRefs)(rootRef, ref),
    className: classes
  }), /*#__PURE__*/_react.default.createElement("div", {
    style: styles
  }, cascadeNodes));
});

var dropdownMenuPropTypes = {
  classPrefix: _propTypes.default.string,
  disabledItemValues: _propTypes.default.array,
  activeItemValue: _propTypes.default.any,
  childrenKey: _propTypes.default.string,
  valueKey: _propTypes.default.string,
  labelKey: _propTypes.default.string,
  menuWidth: _propTypes.default.number,
  menuHeight: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
  className: _propTypes.default.string,
  renderMenuItem: _propTypes.default.func,
  renderMenu: _propTypes.default.func,
  onSelect: _propTypes.default.func,
  cascadeData: _propTypes.default.array,
  cascadePaths: _propTypes.default.array
};
exports.dropdownMenuPropTypes = dropdownMenuPropTypes;
DropdownMenu.displayName = 'DropdownMenu';
DropdownMenu.defaultProps = defaultProps;
DropdownMenu.propTypes = dropdownMenuPropTypes;
var _default = DropdownMenu;
exports.default = _default;