"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _novaIcons = require("@coorpacademy/nova-icons");
var _reactNativeModal = _interopRequireDefault(require("react-native-modal"));
var _index = _interopRequireDefault(require("../../hoc/touchable/index.native"));
var _analytics = require("../../variables/analytics");
var _index2 = _interopRequireDefault(require("../space/index.native"));
var _index3 = _interopRequireDefault(require("../text/index.native"));
var _index4 = _interopRequireDefault(require("../../hoc/modal/select/index.native"));
var _templateContext = require("../../template/app-review/template-context");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const ICON_WIDTH = 15;
const createStyleSheet = theme => _reactNative.StyleSheet.create({
  container: {
    alignItems: 'center',
    flexDirection: 'row'
  },
  text: {
    flex: 1,
    color: theme.colors.gray.medium,
    textAlign: 'center'
  }
});
const logEvent = (eventName, analyticsID, analytics, questionType) => {
  analytics && analytics.logEvent(eventName, {
    id: analyticsID,
    questionType
  });
};
const Select = props => {
  const templateContext = (0, _templateContext.useTemplateContext)();
  const {
    brandTheme,
    theme,
    analytics
  } = templateContext;
  const [styleSheet, setStylesheet] = (0, _react.useState)(null);
  (0, _react.useEffect)(() => {
    const _stylesheet = createStyleSheet(theme);
    setStylesheet(_stylesheet);
  }, [theme]);
  const {
    analyticsID,
    onBlur,
    onFocus,
    onChange,
    values,
    placeholder,
    value,
    style,
    textStyle,
    questionType,
    isFocused = false,
    isDisabled = false,
    testID = 'select'
  } = props;
  const handleFocus = (0, _react.useCallback)(() => {
    if (!onFocus) return;
    analytics && logEvent(_analytics.ANALYTICS_EVENT_TYPE.OPEN_SELECT, analyticsID, analytics, questionType);
    onFocus();
  }, [analytics, analyticsID, onFocus, questionType]);
  const handleBlur = (0, _react.useCallback)(() => {
    if (!onBlur) return;
    analytics && logEvent(_analytics.ANALYTICS_EVENT_TYPE.CLOSE_SELECT, analyticsID, analytics, questionType);
    onBlur();
  }, [analytics, analyticsID, onBlur, questionType]);
  const handleChange = (0, _react.useCallback)(_value => {
    onChange(_value);
    handleBlur();
  }, [onChange, handleBlur]);
  if (!styleSheet) {
    return null;
  }
  const selectedItem = values.find(item => item.selected);
  const text = selectedItem && selectedItem.text || placeholder || null;
  let textStyles = {
    ...styleSheet.text,
    ...textStyle
  };
  let color;
  if ((selectedItem?.value?.length || 0) > 0) {
    color = brandTheme?.colors?.primary;
    textStyles = {
      ...textStyles,
      color
    };
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_index.default, {
    disabled: isDisabled,
    onPress: handleFocus,
    analyticsID: analyticsID,
    testID: `${testID}-input`
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: [styleSheet.container, style]
  }, /*#__PURE__*/_react.default.createElement(_index3.default, {
    style: textStyles
  }, text), /*#__PURE__*/_react.default.createElement(_index2.default, {
    type: "tiny"
  }), /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowDown, {
    color: color || theme.colors.gray.dark,
    height: ICON_WIDTH,
    width: ICON_WIDTH
  }))), /*#__PURE__*/_react.default.createElement(_reactNativeModal.default, {
    isVisible: isFocused,
    onSwipeComplete: handleBlur,
    onBackdropPress: handleBlur,
    testID: testID
  }, /*#__PURE__*/_react.default.createElement(_index4.default, {
    value: value,
    values: values,
    onChange: handleChange,
    onClose: handleBlur,
    testID: `${testID}-modal`
  })));
};
var _default = exports.default = Select;
//# sourceMappingURL=index.native.js.map