"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SelectContext = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactNative = require("react-native");

var _Actionsheet = require("../../composites/Actionsheet");

var _Box = _interopRequireDefault(require("../Box"));

var _Input = require("../Input");

var _focus = require("@react-native-aria/focus");

var _hooks = require("../../../hooks");

var _useThemeProps = require("../../../hooks/useThemeProps");

var _interactions = require("@react-native-aria/interactions");

var _utils = require("../../../utils");

var _FormControl = require("../../composites/FormControl");

var _utils2 = require("../../../theme/tools/utils");

var _Icons = require("../Icon/Icons");

var _ScrollView = require("../../basic/ScrollView");

var _useHasResponsiveProps = require("../../../hooks/useHasResponsiveProps");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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); }

const unstyledSelecWebtStyles = {
  width: '100%',
  height: '100%',
  appearance: 'none',
  WebkitAppearance: 'none',
  MozAppearance: 'none'
};

const SelectContext = /*#__PURE__*/_react.default.createContext({
  onValueChange: () => {},
  selectedValue: null,
  _selectedItem: {},
  _item: {}
});

exports.SelectContext = SelectContext;

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

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

  const _ref = _react.default.useRef(null);

  const [isOpen, setIsOpen] = _react.default.useState(false);

  const {
    focusProps,
    isFocusVisible
  } = (0, _focus.useFocusRing)();
  const {
    hoverProps,
    isHovered
  } = (0, _interactions.useHover)({
    isDisabled
  }, _ref);
  const {
    onValueChange,
    selectedValue,
    children,
    dropdownIcon,
    dropdownCloseIcon,
    dropdownOpenIcon,
    placeholder,
    accessibilityLabel,
    defaultValue,
    _item,
    _selectedItem,
    size,
    onOpen,
    onClose,
    ...resolvedProps
  } = (0, _useThemeProps.usePropsResolution)('Input', props, {
    isDisabled,
    isHovered,
    isFocusVisible
  });
  const [value, setValue] = (0, _hooks.useControllableState)({
    value: selectedValue,
    defaultValue,
    onChange: newValue => {
      onValueChange && onValueChange(newValue);
      setIsOpen(false);
    }
  });

  const itemsList = _react.default.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
  } = (0, _useThemeProps.usePropsResolution)('Select', props);
  const [borderProps, remainingProps] = (0, _utils2.extractInObject)(newProps, [..._utils2.stylingProps.border]);
  const [layoutProps, nonLayoutProps] = (0, _utils2.extractInObject)(remainingProps, [..._utils2.stylingProps.margin, ..._utils2.stylingProps.layout, ..._utils2.stylingProps.flexbox, ..._utils2.stylingProps.position, ..._utils2.stylingProps.background, 'children']); //TODO: refactor for responsive prop

  if ((0, _useHasResponsiveProps.useHasResponsiveProps)(props)) {
    return null;
  }

  const rightIcon = isOpen && dropdownOpenIcon ? dropdownOpenIcon : !isOpen && dropdownCloseIcon ? dropdownCloseIcon : dropdownIcon ? dropdownIcon : /*#__PURE__*/_react.default.createElement(_Icons.ChevronDownIcon, customDropdownIconProps);

  const commonInput = /*#__PURE__*/_react.default.createElement(_Input.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.default.createElement(_Box.default, _extends({
    borderWidth: 1,
    borderColor: "transparent"
  }, layoutProps, {
    borderRadius: resolvedProps.borderRadius,
    ref: wrapperRef
  }), _reactNative.Platform.OS === 'web' ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Box.default, {
    w: "100%",
    h: "100%",
    position: "absolute",
    opacity: "0",
    zIndex: 1
  }, /*#__PURE__*/_react.default.createElement("select", _extends({
    "aria-readonly": selectProps.readOnly,
    required: selectProps.required,
    disabled: isDisabled
  }, focusProps, hoverProps, {
    ref: (0, _utils.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.default.createElement("option", {
    disabled: true,
    value: tempFix
  }, placeholder), children)), commonInput) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.Pressable, {
    onPress: () => {
      _reactNative.Keyboard.dismiss();

      setIsOpen(true);
      onOpen && onOpen();
    },
    disabled: isDisabled,
    accessibilityLabel: accessibilityLabel,
    accessibilityRole: "button",
    ref: (0, _utils.mergeRefs)([ref, _ref])
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    pointerEvents: "none"
  }, commonInput)), /*#__PURE__*/_react.default.createElement(_Actionsheet.Actionsheet, {
    isOpen: isOpen,
    onClose: handleClose
  }, /*#__PURE__*/_react.default.createElement(_Actionsheet.Actionsheet.Content, _actionSheetContent, /*#__PURE__*/_react.default.createElement(_ScrollView.ScrollView, {
    width: "100%"
  }, /*#__PURE__*/_react.default.createElement(SelectContext.Provider, {
    value: {
      onValueChange: setValue,
      selectedValue: value,
      _selectedItem: _selectedItem !== null && _selectedItem !== void 0 ? _selectedItem : {},
      _item: _item !== null && _item !== void 0 ? _item : {}
    }
  }, children))))));
};

var _default = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(Select));

exports.default = _default;
//# sourceMappingURL=Select.js.map