'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var pathReducer = require('../reducers/pathReducer.js');
var treePathReducer = require('../reducers/treePathReducer.js');
var focusedPathReducer = require('../reducers/focusedPathReducer.js');

var useListState = function useListState(_ref) {
  var disabled = _ref.disabled,
    readOnly = _ref.readOnly,
    onToggle = _ref.onToggle,
    multiselect = _ref.multiselect,
    rootRef = _ref.rootRef;
  var _useReducer = React.useReducer(pathReducer.pathReducer, []),
    _useReducer2 = _rollupPluginBabelHelpers.slicedToArray(_useReducer, 2),
    path = _useReducer2[0],
    dispatchPath = _useReducer2[1];
  var _useReducer3 = React.useReducer(treePathReducer.treePathReducer, {}),
    _useReducer4 = _rollupPluginBabelHelpers.slicedToArray(_useReducer3, 2),
    treePath = _useReducer4[0],
    dispatchTreePath = _useReducer4[1];
  var _useReducer5 = React.useReducer(focusedPathReducer.focusedPathReducer, []),
    _useReducer6 = _rollupPluginBabelHelpers.slicedToArray(_useReducer5, 2),
    focusedPath = _useReducer6[0],
    dispatchFocusedPath = _useReducer6[1];
  var resetListState = function resetListState() {
    dispatchFocusedPath({
      type: 'reset'
    });
    dispatchPath({
      type: 'reset'
    });
    dispatchTreePath({
      type: 'reset'
    });
  };
  var handleListToggle = function handleListToggle(opened) {
    if (disabled || readOnly) {
      return;
    }
    if (opened) {
      dispatchPath({
        type: 'opened_first_level'
      });
    } else {
      var _rootRef$current;
      // При закрытии ресетим все состояния.
      resetListState();
      var chipsWrapperNode = rootRef === null || rootRef === void 0 || (_rootRef$current = rootRef.current) === null || _rootRef$current === void 0 ? void 0 : _rootRef$current.querySelector('.input-scrollable-wrapper');

      // И скроллим чипы влево (при multiselect).
      if (multiselect && chipsWrapperNode) {
        chipsWrapperNode.scrollLeft = 0;
      }
    }
    if (onToggle) {
      onToggle(opened);
    }
  };
  return {
    path: path,
    treePath: treePath,
    focusedPath: focusedPath,
    dispatchPath: dispatchPath,
    dispatchTreePath: dispatchTreePath,
    dispatchFocusedPath: dispatchFocusedPath,
    handleListToggle: handleListToggle,
    resetListState: resetListState,
    isListOpened: Boolean(path[0])
  };
};

exports.useListState = useListState;
