function _extends() { _extends = Object.assign || 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); }

import React, { forwardRef, memo } from 'react';
import { Platform, View, Pressable, Keyboard } from 'react-native';
import { Actionsheet } from '../../composites/Actionsheet';
import Box from '../Box';
import { Input } from '../Input';
import { useFocusRing } from '@react-native-aria/focus';
import { useControllableState } from '../../../hooks';
import { usePropsResolution } from '../../../hooks/useThemeProps';
import { useHover } from '@react-native-aria/interactions';
import { mergeRefs } from '../../../utils';
import { useFormControl } from '../../composites/FormControl';
import { extractInObject, stylingProps } from '../../../theme/tools/utils';
import { ChevronDownIcon } from '../Icon/Icons';
import { ScrollView } from '../../basic/ScrollView';
import { useHasResponsiveProps } from '../../../hooks/useHasResponsiveProps';
const unstyledSelecWebtStyles = {
  width: '100%',
  height: '100%',
  appearance: 'none',
  WebkitAppearance: 'none',
  MozAppearance: 'none'
};
export const SelectContext = /*#__PURE__*/React.createContext({
  onValueChange: () => {},
  selectedValue: null,
  _selectedItem: {},
  _item: {}
});

const Select = ({
  wrapperRef,
  ...props
}, ref) => {
  var _layoutProps$height;

  const selectProps = useFormControl({
    isDisabled: props.isDisabled,
    nativeID: props.nativeID
  });
  const isDisabled = selectProps.disabled;
  const tempFix = '__NativebasePlaceholder__';

  const _ref = React.useRef(null);

  const [isOpen, setIsOpen] = React.useState(false);
  const {
    focusProps,
    isFocusVisible
  } = useFocusRing();
  const {
    hoverProps,
    isHovered
  } = useHover({
    isDisabled
  }, _ref);
  const {
    onValueChange,
    selectedValue,
    children,
    dropdownIcon,
    dropdownCloseIcon,
    dropdownOpenIcon,
    placeholder,
    accessibilityLabel,
    defaultValue,
    _item,
    _selectedItem,
    size,
    onOpen,
    onClose,
    ...resolvedProps
  } = usePropsResolution('Input', props, {
    isDisabled,
    isHovered,
    isFocusVisible
  });
  const [value, setValue] = useControllableState({
    value: selectedValue,
    defaultValue,
    onChange: newValue => {
      onValueChange && onValueChange(newValue);
      setIsOpen(false);
    }
  });
  const itemsList = React.Children.map(children, child => {
    return {
      label: child.props.label,
      value: child.props.value
    };
  });
  const selectedItemArray = itemsList.filter(item => item.value === value);
  const selectedItem = selectedItemArray && selectedItemArray.length ? selectedItemArray[0] : null;
  const {
    variant,
    customDropdownIconProps,
    _actionSheetContent,
    ...newProps
  } = usePropsResolution('Select', props);
  const [borderProps, remainingProps] = extractInObject(newProps, [...stylingProps.border]);
  const [layoutProps, nonLayoutProps] = extractInObject(remainingProps, [...stylingProps.margin, ...stylingProps.layout, ...stylingProps.flexbox, ...stylingProps.position, ...stylingProps.background, 'children']); //TODO: refactor for responsive prop

  if (useHasResponsiveProps(props)) {
    return null;
  }

  const rightIcon = isOpen && dropdownOpenIcon ? dropdownOpenIcon : !isOpen && dropdownCloseIcon ? dropdownCloseIcon : dropdownIcon ? dropdownIcon : /*#__PURE__*/React.createElement(ChevronDownIcon, customDropdownIconProps);
  const commonInput = /*#__PURE__*/React.createElement(Input, _extends({
    "aria-hidden": true,
    importantForAccessibility: "no",
    value: selectedItem === null || selectedItem === void 0 ? void 0 : selectedItem.label,
    placeholder: placeholder,
    editable: false,
    focusable: false,
    size: size,
    variant: variant,
    InputRightElement: rightIcon,
    height: (_layoutProps$height = layoutProps.height) !== null && _layoutProps$height !== void 0 ? _layoutProps$height : layoutProps.h
  }, nonLayoutProps, borderProps, {
    isDisabled: isDisabled
  }));

  const handleClose = () => {
    setIsOpen(false);
    onClose && onClose();
  };

  return /*#__PURE__*/React.createElement(Box, _extends({
    borderWidth: 1,
    borderColor: "transparent"
  }, layoutProps, {
    borderRadius: resolvedProps.borderRadius,
    ref: wrapperRef
  }), Platform.OS === 'web' ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Box, {
    w: "100%",
    h: "100%",
    position: "absolute",
    opacity: "0",
    zIndex: 1
  }, /*#__PURE__*/React.createElement("select", _extends({
    "aria-readonly": selectProps.readOnly,
    required: selectProps.required,
    disabled: isDisabled
  }, focusProps, hoverProps, {
    ref: mergeRefs([ref, _ref]) //@ts-ignore
    ,
    style: unstyledSelecWebtStyles,
    onChange: e => {
      setValue(e.target.value);
    },
    value: selectedItem === null ? tempFix : value,
    "aria-label": placeholder,
    onFocus: () => {
      onOpen && onOpen();
    },
    onBlur: () => {
      onClose && onClose();
    }
  }), /*#__PURE__*/React.createElement("option", {
    disabled: true,
    value: tempFix
  }, placeholder), children)), commonInput) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Pressable, {
    onPress: () => {
      Keyboard.dismiss();
      setIsOpen(true);
      onOpen && onOpen();
    },
    disabled: isDisabled,
    accessibilityLabel: accessibilityLabel,
    accessibilityRole: "button",
    ref: mergeRefs([ref, _ref])
  }, /*#__PURE__*/React.createElement(View, {
    pointerEvents: "none"
  }, commonInput)), /*#__PURE__*/React.createElement(Actionsheet, {
    isOpen: isOpen,
    onClose: handleClose
  }, /*#__PURE__*/React.createElement(Actionsheet.Content, _actionSheetContent, /*#__PURE__*/React.createElement(ScrollView, {
    width: "100%"
  }, /*#__PURE__*/React.createElement(SelectContext.Provider, {
    value: {
      onValueChange: setValue,
      selectedValue: value,
      _selectedItem: _selectedItem !== null && _selectedItem !== void 0 ? _selectedItem : {},
      _item: _item !== null && _item !== void 0 ? _item : {}
    }
  }, children))))));
};

export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(Select));
//# sourceMappingURL=Select.js.map