"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _index = _interopRequireDefault(require("../../../../atom/choice/index.native"));
var _index2 = _interopRequireDefault(require("../draggable/index.native"));
var _index3 = _interopRequireDefault(require("../template/index.native"));
var _index4 = _interopRequireDefault(require("../slider/index.native"));
var _index5 = _interopRequireDefault(require("../../free-text/index.native"));
var _templateContext = require("../../../../template/app-review/template-context");
var _analytics = require("../../../../variables/analytics");
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 styleSheet = _reactNative.StyleSheet.create({
  cards: {
    flexDirection: 'row',
    alignItems: 'stretch'
  },
  card: {
    flex: 1
  },
  choice: {
    paddingVertical: 5
  }
});
const Switch = props => {
  const templateContext = (0, _templateContext.useTemplateContext)();
  const {
    analytics,
    store
  } = templateContext;
  const {
    focusedSelectId,
    handleBlur,
    handleFocus
  } = store;
  const {
    type,
    template,
    isDisabled,
    choices = [],
    min,
    max,
    unit,
    value,
    step,
    onSliderChange,
    onItemPress,
    onItemInputChange,
    onInputValueChange
  } = props;
  const handleChoicePress = (0, _react.useCallback)(choice => () => {
    if (choice.onPress) {
      // e.g. app-review
      choice.onPress();
    } else if (onItemPress) {
      // e.g. learner
      onItemPress(choice);
    }
  }, [onItemPress]);
  const handleItemInputChange = (0, _react.useCallback)((choice, _value) => {
    if (choice.onPress) {
      // e.g. app-review
      choice.onPress(_value);
    } else if (onItemInputChange) {
      // e.g. learner
      onItemInputChange(choice, _value);
    }
  }, [onItemInputChange]);
  const handleSlidingComplete = (0, _react.useCallback)(_value => {
    analytics && analytics.logEvent(_analytics.ANALYTICS_EVENT_TYPE.SLIDE, {
      id: 'slider',
      questionType: 'slider'
    });
    if (onSliderChange) {
      onSliderChange(_value);
    }
  }, [analytics, onSliderChange]);
  switch (type) {
    case 'qcm':
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
        testID: "question-choices"
      }, choices.map(choice => /*#__PURE__*/_react.default.createElement(_index.default, {
        key: `question-choice-${choice._id}`,
        onPress: handleChoicePress(choice),
        isDisabled: isDisabled,
        isSelected: choice.selected,
        testID: `question-choice-${choice._id}`,
        style: styleSheet.choice,
        questionType: type
      }, choice.label)));
    case 'qcmGraphic':
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
        testID: "question-choices"
      }, choices.map(choice => /*#__PURE__*/_react.default.createElement(_index.default, {
        key: `question-choice-${choice._id}`,
        onPress: handleChoicePress(choice),
        media: choice.media,
        isDisabled: isDisabled,
        isSelected: choice.selected,
        testID: `question-choice-${choice._id}`,
        style: styleSheet.choice,
        questionType: type
      }, choice.label)));
    case 'slider':
      {
        if (min === undefined || max === undefined) {
          return /*#__PURE__*/_react.default.createElement(_reactNative.View, null);
        }
        return /*#__PURE__*/_react.default.createElement(_index4.default, {
          min: min,
          max: max,
          unit: unit,
          value: value,
          step: step,
          onSlidingComplete: handleSlidingComplete,
          testID: "question-slider"
        });
      }
    case 'template':
      if (handleBlur === undefined || handleFocus === undefined || focusedSelectId === undefined) {
        // eslint-disable-next-line no-console
        console.warn('type template must implement handleFocus etc within Context.store');
        return /*#__PURE__*/_react.default.createElement(_reactNative.View, null);
      }
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
        testID: "question-choices"
      }, /*#__PURE__*/_react.default.createElement(_index3.default, {
        isDisabled: isDisabled,
        template: template || '',
        choices: choices,
        onInputChange: handleItemInputChange,
        handleBlur: handleBlur,
        handleFocus: handleFocus,
        focusedSelectId: focusedSelectId
      }));
    case 'qcmDrag':
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
        testID: "question-draggable"
      }, /*#__PURE__*/_react.default.createElement(_index2.default, {
        choices: choices,
        onPress: handleChoicePress
      }));
    case 'basic':
      if (!onInputValueChange) {
        return /*#__PURE__*/_react.default.createElement(_reactNative.View, null);
      }
      return /*#__PURE__*/_react.default.createElement(_index5.default, {
        fullWidth: true,
        onChange: onInputValueChange,
        testID: "question-input-text",
        questionType: "basic",
        isDisabled: isDisabled
      });
    default:
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, null, "Unhandled type: ", type));
  }
};
var _default = exports.default = Switch;
//# sourceMappingURL=index.native.js.map