function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { FloatingFocusManager, FloatingPortal } from '@floating-ui/react';
import { FocusScope } from '@react-aria/focus';
import React from 'react';
import { DragDropContext, Droppable } from 'react-beautiful-dnd';
import { Virtuoso } from 'react-virtuoso';
import { presets } from './presets';
import { DraggableOptionContent, OptionContent } from './SuperSelect.components';
import { SuperSelectContext, useSuperSelect } from './useSuperSelect';
export var InternalDragDropContext = /*#__PURE__*/React.createContext({
  isDraggingOver: false
});
var InternalDragDropProvider = function InternalDragDropProvider(_ref) {
  var isDraggingOver = _ref.isDraggingOver,
    children = _ref.children;
  var value = React.useMemo(function () {
    return {
      isDraggingOver: isDraggingOver
    };
  }, [isDraggingOver]);
  return /*#__PURE__*/React.createElement(InternalDragDropContext.Provider, {
    value: value
  }, children);
};
function SelectComponent(props, ref) {
  var search = props.search === undefined || props.search === true;
  var preset = props.preset || '';
  var config = _objectSpread(_objectSpread(_objectSpread({}, presets[preset]), props), {}, {
    search: search
  });
  var ctx = useSuperSelect(config);
  React.useImperativeHandle(ref, function () {
    return ctx;
  });
  React.useEffect(function () {
    if (props.value && props.onChange === undefined) {
      console.warn('Must provide both a `value` and `onChange` prop for a controlled Select');
    }
  }, []);
  return /*#__PURE__*/React.createElement(SuperSelectContext.Provider, {
    value: ctx
  }, /*#__PURE__*/React.createElement(ctx.components.Trigger, _extends({
    ref: ctx.refs.floating.refs.setReference
  }, ctx.props.trigger())), /*#__PURE__*/React.createElement(FloatingPortal, null, ctx.state.open &&
  /*#__PURE__*/
  // FocusScope is needed to allow move focus from Modal and Tearsheet
  React.createElement(FocusScope, null, /*#__PURE__*/React.createElement(FloatingFocusManager, {
    context: ctx.refs.floating.context,
    order: config.search ? ['content', 'reference'] : ['reference', 'content'],
    initialFocus: config.multiple ? undefined : 0
  }, /*#__PURE__*/React.createElement(ctx.components.Overlay, _extends({
    ref: ctx.refs.floating.refs.setFloating
  }, ctx.props.overlay()), (config.search === true || config.search === undefined && !config.multiple) && /*#__PURE__*/React.createElement(ctx.components.SearchContainer, ctx.props.searchContainer(), /*#__PURE__*/React.createElement(ctx.components.Search, ctx.props.search())), config.header && /*#__PURE__*/React.createElement(ctx.components.Header, ctx.props.header(), config.header), ctx.state.options.length > 0 ? /*#__PURE__*/React.createElement(ctx.components.Options, null, config.draggable ? /*#__PURE__*/React.createElement(DragDropContext, {
    onDragEnd: ctx.state.onDragEnd
  }, /*#__PURE__*/React.createElement(Droppable, {
    droppableId: "droppable",
    mode: "virtual",
    renderClone: function renderClone(provided, snapshot, dragged) {
      var option = ctx.state.options[dragged.source.index];
      return /*#__PURE__*/React.createElement(ctx.components.Item, _extends({
        isDragging: true,
        "data-index": dragged.source.index
      }, provided.draggableProps), /*#__PURE__*/React.createElement(ctx.components.DragHandle, null), /*#__PURE__*/React.createElement(ctx.components.OptionLabel, {
        option: option
      }));
    }
  }, function (provided, snapshot) {
    return /*#__PURE__*/React.createElement(InternalDragDropProvider, {
      isDraggingOver: snapshot.isDraggingOver
    }, /*#__PURE__*/React.createElement(Virtuoso, _extends({}, ctx.props.virtuoso(), {
      scrollerRef: function scrollerRef(el) {
        return provided.innerRef(el);
      },
      itemContent: function itemContent(i, data) {
        return /*#__PURE__*/React.createElement(DraggableOptionContent, {
          index: i,
          option: data
        });
      }
    })));
  })) : /*#__PURE__*/React.createElement(Virtuoso, _extends({}, ctx.props.virtuoso(), {
    ref: ctx.refs.virtuoso,
    itemContent: function itemContent(i, option) {
      return /*#__PURE__*/React.createElement(OptionContent, {
        option: option
      });
    }
  }))) : /*#__PURE__*/React.createElement(ctx.components.EmptyMessage, ctx.props.emptyMessage()), config.footer && /*#__PURE__*/React.createElement(ctx.components.Footer, ctx.props.footer()))))));
}
export var SuperSelect = /*#__PURE__*/React.forwardRef(SelectComponent);
//# sourceMappingURL=SuperSelect.js.map