"use strict";

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

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

exports.__esModule = true;
exports.createConcatChildrenFunction = createConcatChildrenFunction;
exports.shouldDisplay = shouldDisplay;
exports.usePickerClassName = usePickerClassName;
exports.onMenuKeyDown = onMenuKeyDown;
exports.useSearch = useSearch;
exports.usePublicMethods = usePublicMethods;
exports.useToggleKeyDownEvent = exports.useFocusItemValue = void 0;

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

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

var _kebabCase = _interopRequireDefault(require("lodash/kebabCase"));

var _trim = _interopRequireDefault(require("lodash/trim"));

var _isFunction = _interopRequireDefault(require("lodash/isFunction"));

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

var _omit = _interopRequireDefault(require("lodash/omit"));

var _find = _interopRequireDefault(require("lodash/find"));

var _treeUtils = require("../utils/treeUtils");

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

var defaultNodeKeys = {
  valueKey: 'value',
  childrenKey: 'children'
};

function createConcatChildrenFunction(node, nodeValue, nodeKeys) {
  if (nodeKeys === void 0) {
    nodeKeys = defaultNodeKeys;
  }

  var _nodeKeys = nodeKeys,
      valueKey = _nodeKeys.valueKey,
      childrenKey = _nodeKeys.childrenKey;
  return function (data, children) {
    if (nodeValue) {
      node = (0, _treeUtils.findNodeOfTree)(data, function (item) {
        return nodeValue === item[valueKey];
      });
    }

    node[childrenKey] = children;
    return data.concat([]);
  };
}

function shouldDisplay(label, searchKeyword) {
  if (!(0, _trim.default)(searchKeyword)) {
    return true;
  }

  var keyword = searchKeyword.toLocaleLowerCase();

  if (typeof label === 'string' || typeof label === 'number') {
    return ("" + label).toLocaleLowerCase().indexOf(keyword) >= 0;
  } else if ( /*#__PURE__*/_react.default.isValidElement(label)) {
    var nodes = (0, _utils.reactToString)(label);
    return nodes.join('').toLocaleLowerCase().indexOf(keyword) >= 0;
  }

  return false;
}

/**
 * The className of the assembled Toggle is on the Picker.
 */
function usePickerClassName(props) {
  var _withClassPrefix;

  var name = props.name,
      classPrefix = props.classPrefix,
      className = props.className,
      placement = props.placement,
      appearance = props.appearance,
      cleanable = props.cleanable,
      block = props.block,
      disabled = props.disabled,
      countable = props.countable,
      readOnly = props.readOnly,
      plaintext = props.plaintext,
      hasValue = props.hasValue,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["name", "classPrefix", "className", "placement", "appearance", "cleanable", "block", "disabled", "countable", "readOnly", "plaintext", "hasValue"]);

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

  var classes = merge(className, withClassPrefix(name, appearance, 'toggle-wrapper', (_withClassPrefix = {}, _withClassPrefix["placement-" + (0, _kebabCase.default)((0, _utils.placementPolyfill)(placement))] = placement, _withClassPrefix['read-only'] = readOnly, _withClassPrefix['has-value'] = hasValue, _withClassPrefix.cleanable = hasValue && cleanable, _withClassPrefix.block = block, _withClassPrefix.disabled = disabled, _withClassPrefix.countable = countable, _withClassPrefix.plaintext = plaintext, _withClassPrefix)));
  var usedClassNamePropKeys = Object.keys((0, _omit.default)(props, [].concat(Object.keys(rest || {}), ['disabled', 'readOnly', 'plaintext'])));
  return [classes, usedClassNamePropKeys];
}

/**
 * Handling keyboard events...
 * @param event Keyboard event object
 * @param events Event callback functions
 */
function onMenuKeyDown(event, events) {
  var down = events.down,
      up = events.up,
      enter = events.enter,
      del = events.del,
      esc = events.esc,
      right = events.right,
      left = events.left;

  switch (event.keyCode) {
    // down
    case _utils.KEY_CODE.DOWN:
      down === null || down === void 0 ? void 0 : down(event);
      event.preventDefault();
      break;
    // up

    case _utils.KEY_CODE.UP:
      up === null || up === void 0 ? void 0 : up(event);
      event.preventDefault();
      break;
    // enter

    case _utils.KEY_CODE.ENTER:
      enter === null || enter === void 0 ? void 0 : enter(event);
      event.preventDefault();
      break;
    // delete

    case _utils.KEY_CODE.BACKSPACE:
      del === null || del === void 0 ? void 0 : del(event);
      break;
    // esc | tab

    case _utils.KEY_CODE.ESC:
    case _utils.KEY_CODE.TAB:
      esc === null || esc === void 0 ? void 0 : esc(event);
      break;
    // left arrow

    case _utils.KEY_CODE.LEFT:
      left === null || left === void 0 ? void 0 : left(event);
      break;
    // right arrow

    case _utils.KEY_CODE.RIGHT:
      right === null || right === void 0 ? void 0 : right(event);
      break;

    default:
  }
}

/**
 * A hook that manages the focus state of the option.
 * @param defaultFocusItemValue
 * @param props
 */
var useFocusItemValue = function useFocusItemValue(defaultFocusItemValue, props) {
  var _props$valueKey = props.valueKey,
      valueKey = _props$valueKey === void 0 ? 'value' : _props$valueKey,
      _props$focusableQuery = props.focusableQueryKey,
      focusableQueryKey = _props$focusableQuery === void 0 ? '[data-key][aria-disabled="false"]' : _props$focusableQuery,
      _props$defaultLayer = props.defaultLayer,
      defaultLayer = _props$defaultLayer === void 0 ? 0 : _props$defaultLayer,
      data = props.data,
      target = props.target,
      rtl = props.rtl,
      callback = props.callback;

  var _useState = (0, _react.useState)(defaultFocusItemValue),
      focusItemValue = _useState[0],
      setFocusItemValue = _useState[1];

  var _useState2 = (0, _react.useState)(defaultLayer),
      layer = _useState2[0],
      setLayer = _useState2[1];

  var _useState3 = (0, _react.useState)([]),
      keys = _useState3[0],
      setKeys = _useState3[1];
  /**
   * Get the elements visible in all options.
   */


  var getFocusableMenuItems = (0, _react.useCallback)(function () {
    if (!target) {
      return [];
    }

    var menu = (0, _isFunction.default)(target) ? target() : target;
    var currentKeys = keys;

    if (currentKeys.length === 0) {
      var _Array$from;

      currentKeys = (_Array$from = Array.from(menu === null || menu === void 0 ? void 0 : menu.querySelectorAll(focusableQueryKey))) === null || _Array$from === void 0 ? void 0 : _Array$from.map(function (item) {
        var _item$dataset;

        return item === null || item === void 0 ? void 0 : (_item$dataset = item.dataset) === null || _item$dataset === void 0 ? void 0 : _item$dataset.key;
      });
      setKeys(currentKeys);
    }

    if (currentKeys.length === 0) {
      return [];
    } // 1. It is necessary to traverse the `keys` instead of `data` here to preserve the order of the array.
    // 2. The values ​​in `keys` are all string, so the corresponding value of `data` should also be converted to string


    return currentKeys.map(function (key) {
      return (0, _find.default)(data, function (i) {
        return "" + i[valueKey] === key;
      });
    });
  }, [data, focusableQueryKey, keys, target, valueKey]);
  /**
   * Get the index of the focus item.
   */

  var findFocusItemIndex = (0, _react.useCallback)(function (callback) {
    var items = getFocusableMenuItems();

    for (var i = 0; i < items.length; i += 1) {
      var _items$i;

      if ((0, _utils.shallowEqual)(focusItemValue, (_items$i = items[i]) === null || _items$i === void 0 ? void 0 : _items$i[valueKey])) {
        callback(items, i);
        return;
      }
    }

    callback(items, -1);
  }, [focusItemValue, getFocusableMenuItems, valueKey]);
  var focusNextMenuItem = (0, _react.useCallback)(function (event) {
    findFocusItemIndex(function (items, index) {
      var nextIndex = index + 2 > items.length ? 0 : index + 1;
      var focusItem = items[nextIndex];

      if (!(0, _isUndefined.default)(focusItem)) {
        setFocusItemValue(focusItem[valueKey]);
        callback === null || callback === void 0 ? void 0 : callback(focusItem[valueKey], event);
      }
    });
  }, [callback, findFocusItemIndex, valueKey]);
  var focusPrevMenuItem = (0, _react.useCallback)(function (event) {
    findFocusItemIndex(function (items, index) {
      var nextIndex = index === 0 ? items.length - 1 : index - 1;
      var focusItem = items[nextIndex];

      if (!(0, _isUndefined.default)(focusItem)) {
        setFocusItemValue(focusItem[valueKey]);
        callback === null || callback === void 0 ? void 0 : callback(focusItem[valueKey], event);
      }
    });
  }, [callback, findFocusItemIndex, valueKey]);
  var getSubMenuKeys = (0, _react.useCallback)(function (nextLayer) {
    var menu = (0, _isFunction.default)(target) ? target() : target;
    var subMenu = menu === null || menu === void 0 ? void 0 : menu.querySelector("[data-layer=\"" + nextLayer + "\"]");

    if (subMenu) {
      var _Array$from2;

      return (_Array$from2 = Array.from(subMenu.querySelectorAll(focusableQueryKey))) === null || _Array$from2 === void 0 ? void 0 : _Array$from2.map(function (item) {
        var _item$dataset2;

        return item === null || item === void 0 ? void 0 : (_item$dataset2 = item.dataset) === null || _item$dataset2 === void 0 ? void 0 : _item$dataset2.key;
      });
    }

    return null;
  }, [focusableQueryKey, target]);
  var focusNextLevelMenu = (0, _react.useCallback)(function (event) {
    var nextLayer = layer + 1;
    var nextKeys = getSubMenuKeys(nextLayer);

    if (nextKeys) {
      setKeys(nextKeys);
      setLayer(nextLayer);
      setFocusItemValue(nextKeys[0]);
      callback === null || callback === void 0 ? void 0 : callback(nextKeys[0], event);
    }
  }, [callback, getSubMenuKeys, layer]);
  var focusPrevLevelMenu = (0, _react.useCallback)(function (event) {
    var nextLayer = layer - 1;
    var nextKeys = getSubMenuKeys(nextLayer);

    if (nextKeys) {
      var _focusItem$parent;

      setKeys(nextKeys);
      setLayer(nextLayer);
      var focusItem = (0, _treeUtils.findNodeOfTree)(data, function (item) {
        return item[valueKey] === focusItemValue;
      });
      var parentItemValue = focusItem === null || focusItem === void 0 ? void 0 : (_focusItem$parent = focusItem.parent) === null || _focusItem$parent === void 0 ? void 0 : _focusItem$parent[valueKey];

      if (parentItemValue) {
        setFocusItemValue(parentItemValue);
        callback === null || callback === void 0 ? void 0 : callback(parentItemValue, event);
      }
    }
  }, [callback, data, focusItemValue, getSubMenuKeys, layer, valueKey]);
  var handleKeyDown = (0, _react.useCallback)(function (event) {
    var _onMenuKeyDown;

    onMenuKeyDown(event, (_onMenuKeyDown = {
      down: focusNextMenuItem,
      up: focusPrevMenuItem
    }, _onMenuKeyDown[rtl ? 'left' : 'right'] = focusNextLevelMenu, _onMenuKeyDown[rtl ? 'right' : 'left'] = focusPrevLevelMenu, _onMenuKeyDown));
  }, [focusNextLevelMenu, focusNextMenuItem, focusPrevLevelMenu, focusPrevMenuItem, rtl]);
  return {
    focusItemValue: focusItemValue,
    setFocusItemValue: setFocusItemValue,
    layer: layer,
    setLayer: setLayer,
    keys: keys,
    setKeys: setKeys,
    onKeyDown: handleKeyDown
  };
};

exports.useFocusItemValue = useFocusItemValue;

/**
 * A hook to control the toggle keyboard operation
 * @param props
 */
var useToggleKeyDownEvent = function useToggleKeyDownEvent(props) {
  var _props$toggle = props.toggle,
      toggle = _props$toggle === void 0 ? true : _props$toggle,
      triggerRef = props.triggerRef,
      targetRef = props.targetRef,
      overlayRef = props.overlayRef,
      active = props.active,
      onExit = props.onExit,
      onOpen = props.onOpen,
      onClose = props.onClose,
      onKeyDown = props.onKeyDown,
      onMenuKeyDown = props.onMenuKeyDown,
      onMenuPressEnter = props.onMenuPressEnter,
      onMenuPressBackspace = props.onMenuPressBackspace;
  var handleClose = (0, _react.useCallback)(function () {
    var _triggerRef$current, _triggerRef$current$c;

    (_triggerRef$current = triggerRef.current) === null || _triggerRef$current === void 0 ? void 0 : (_triggerRef$current$c = _triggerRef$current.close) === null || _triggerRef$current$c === void 0 ? void 0 : _triggerRef$current$c.call(_triggerRef$current);
    onClose === null || onClose === void 0 ? void 0 : onClose();
  }, [onClose, triggerRef]);
  var handleOpen = (0, _react.useCallback)(function () {
    var _triggerRef$current2, _triggerRef$current2$;

    (_triggerRef$current2 = triggerRef.current) === null || _triggerRef$current2 === void 0 ? void 0 : (_triggerRef$current2$ = _triggerRef$current2.open) === null || _triggerRef$current2$ === void 0 ? void 0 : _triggerRef$current2$.call(_triggerRef$current2);
    onOpen === null || onOpen === void 0 ? void 0 : onOpen();
  }, [onOpen, triggerRef]);
  var handleToggleDropdown = (0, _react.useCallback)(function () {
    if (active) {
      handleClose();
      return;
    }

    handleOpen();
  }, [active, handleOpen, handleClose]);
  var onToggle = (0, _react.useCallback)(function (event) {
    if (event.target === (targetRef === null || targetRef === void 0 ? void 0 : targetRef.current)) {
      // enter
      if (toggle && event.keyCode === _utils.KEY_CODE.ENTER) {
        handleToggleDropdown();
      } // delete


      if (event.keyCode === _utils.KEY_CODE.BACKSPACE) {
        onExit === null || onExit === void 0 ? void 0 : onExit(event);
      }
    }

    if (event.keyCode === _utils.KEY_CODE.ESC || event.keyCode === _utils.KEY_CODE.TAB) {
      handleClose();
    } // Native event callback


    onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(event);

    if (overlayRef === null || overlayRef === void 0 ? void 0 : overlayRef.current) {
      // The keyboard operation callback on the menu.
      onMenuKeyDown === null || onMenuKeyDown === void 0 ? void 0 : onMenuKeyDown(event);

      if (event.keyCode === _utils.KEY_CODE.ENTER) {
        onMenuPressEnter === null || onMenuPressEnter === void 0 ? void 0 : onMenuPressEnter(event);
      }

      if (event.keyCode === _utils.KEY_CODE.BACKSPACE) {
        onMenuPressBackspace === null || onMenuPressBackspace === void 0 ? void 0 : onMenuPressBackspace(event);
      }
    }
  }, [handleClose, handleToggleDropdown, overlayRef, onExit, onKeyDown, onMenuKeyDown, onMenuPressBackspace, onMenuPressEnter, toggle, targetRef]);
  return onToggle;
};

exports.useToggleKeyDownEvent = useToggleKeyDownEvent;

/**
 * A hook that handles search filter options
 * @param props
 */
function useSearch(props) {
  var labelKey = props.labelKey,
      data = props.data,
      searchBy = props.searchBy,
      callback = props.callback; // Use search keywords to filter options.

  var _useState4 = (0, _react.useState)(''),
      searchKeyword = _useState4[0],
      setSearchKeyword = _useState4[1];
  /**
   * Index of keyword  in `label`
   * @param {node} label
   */


  var checkShouldDisplay = (0, _react.useCallback)(function (item, keyword) {
    var label = item === null || item === void 0 ? void 0 : item[labelKey];

    var _keyword = (0, _isUndefined.default)(keyword) ? searchKeyword : keyword;

    if (typeof searchBy === 'function') {
      return searchBy(_keyword, label, item);
    }

    return shouldDisplay(label, _keyword);
  }, [labelKey, searchBy, searchKeyword]);
  var updateFilteredData = (0, _react.useCallback)(function (nextData) {
    setFilteredData((0, _treeUtils.filterNodesOfTree)(nextData, function (item) {
      return checkShouldDisplay(item);
    }));
  }, [checkShouldDisplay]);

  var _useState5 = (0, _react.useState)((0, _treeUtils.filterNodesOfTree)(data, function (item) {
    return checkShouldDisplay(item);
  })),
      filteredData = _useState5[0],
      setFilteredData = _useState5[1];

  var handleSearch = function handleSearch(searchKeyword, event) {
    var filteredData = (0, _treeUtils.filterNodesOfTree)(data, function (item) {
      return checkShouldDisplay(item, searchKeyword);
    });
    setFilteredData(filteredData);
    setSearchKeyword(searchKeyword);
    callback === null || callback === void 0 ? void 0 : callback(searchKeyword, filteredData, event);
  };

  return {
    searchKeyword: searchKeyword,
    filteredData: filteredData,
    updateFilteredData: updateFilteredData,
    setSearchKeyword: setSearchKeyword,
    checkShouldDisplay: checkShouldDisplay,
    handleSearch: handleSearch
  };
}

/**
 * A hook of the exposed method of Picker
 *
 * {
 *   root: Element;
 *   overlay: Element;
 *   target?: Element;
 *   updatePosition:() => void;
 *   open:() => void;
 *   close:() => void;
 * }
 * @param ref
 * @param params
 */
function usePublicMethods(ref, _ref) {
  var triggerRef = _ref.triggerRef,
      overlayRef = _ref.overlayRef,
      targetRef = _ref.targetRef,
      rootRef = _ref.rootRef;
  var handleOpen = (0, _react.useCallback)(function () {
    var _triggerRef$current3;

    (_triggerRef$current3 = triggerRef.current) === null || _triggerRef$current3 === void 0 ? void 0 : _triggerRef$current3.open();
  }, [triggerRef]);
  var handleClose = (0, _react.useCallback)(function () {
    var _triggerRef$current4;

    (_triggerRef$current4 = triggerRef.current) === null || _triggerRef$current4 === void 0 ? void 0 : _triggerRef$current4.close();
  }, [triggerRef]);
  var handleUpdatePosition = (0, _react.useCallback)(function () {
    var _triggerRef$current5;

    (_triggerRef$current5 = triggerRef.current) === null || _triggerRef$current5 === void 0 ? void 0 : _triggerRef$current5.updatePosition();
  }, [triggerRef]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      get root() {
        var _triggerRef$current6;

        return (rootRef === null || rootRef === void 0 ? void 0 : rootRef.current) ? rootRef === null || rootRef === void 0 ? void 0 : rootRef.current : (_triggerRef$current6 = triggerRef.current) === null || _triggerRef$current6 === void 0 ? void 0 : _triggerRef$current6.root;
      },

      get overlay() {
        return overlayRef.current;
      },

      get target() {
        return targetRef === null || targetRef === void 0 ? void 0 : targetRef.current;
      },

      updatePosition: handleUpdatePosition,
      open: handleOpen,
      close: handleClose
    };
  });
}