"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useTreeViewId = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _useTreeViewId = require("./useTreeViewId.utils");
const useTreeViewId = ({
  params,
  state,
  setState
}) => {
  React.useEffect(() => {
    setState(prevState => {
      if (prevState.id.treeId === params.id && prevState.id.treeId !== undefined) {
        return prevState;
      }
      return (0, _extends2.default)({}, prevState, {
        id: (0, _extends2.default)({}, prevState.id, {
          treeId: params.id ?? (0, _useTreeViewId.createTreeViewDefaultId)()
        })
      });
    });
  }, [setState, params.id]);
  const treeId = params.id ?? state.id.treeId;
  return {
    getRootProps: () => ({
      id: treeId
    }),
    contextValue: {
      treeId
    }
  };
};
exports.useTreeViewId = useTreeViewId;
useTreeViewId.params = {
  id: true
};
useTreeViewId.getInitialState = ({
  id
}) => ({
  id: {
    treeId: id ?? undefined
  }
});