"use strict";

exports.__esModule = true;
exports.useCreatePickerModalState = useCreatePickerModalState;
exports.usePickerModalState = usePickerModalState;
exports.useSyncPickerModalProps = useSyncPickerModalProps;
var _react = require("react");
var _PickerModalState = require("./PickerModalState");
var _reactDom = require("@wix/bex-utils/react-dom");
var _version = require("../../version");
(0, _reactDom.configure)();
function useCreatePickerModalState() {
  return (0, _react.useCallback)(params => new _PickerModalState.PickerModalState({
    ...params,
    environment: {
      ...params.environment,
      uiLibVersion: _version.version
    }
  }), []);
}
function useSyncPickerModalProps(state, {
  onSelect,
  disableAutoSelectOnSingleItem
}) {
  (0, _react.useEffect)(() => {
    if (!state) {
      return;
    }
    state.onSelectRef.current = onSelect;
  }, [onSelect]);
  (0, _react.useState)(() => {
    if (!state) {
      return;
    }
    if (state.isModalOpen) {
      state.prefetch();
    }
    state.disableAutoSelectionRef.current = disableAutoSelectOnSingleItem;
  });
  (0, _react.useEffect)(() => {
    if (!state) {
      return;
    }
    state.disableAutoSelectionRef.current = disableAutoSelectOnSingleItem;
  }, [disableAutoSelectOnSingleItem]);
}
function usePickerModalState(params) {
  const createPickerModal = useCreatePickerModalState();
  const [state] = (0, _react.useState)(() => createPickerModal(params));
  useSyncPickerModalProps(state, params);
  return state;
}
//# sourceMappingURL=usePickerModalState.js.map