import React, { useCallback } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import QuestionChoice from '../../../../atom/choice/index.native';
import QuestionDraggable from '../draggable/index.native';
import QuestionTemplate from '../template/index.native';
import QuestionSlider from '../slider/index.native';
import FreeText from '../../free-text/index.native';
import { useTemplateContext } from '../../../../template/app-review/template-context';
import { ANALYTICS_EVENT_TYPE } from '../../../../variables/analytics';
const styleSheet = StyleSheet.create({
  cards: {
    flexDirection: 'row',
    alignItems: 'stretch'
  },
  card: {
    flex: 1
  },
  choice: {
    paddingVertical: 5
  }
});
const Switch = props => {
  const 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 = useCallback(choice => () => {
    if (choice.onPress) {
      // e.g. app-review
      choice.onPress();
    } else if (onItemPress) {
      // e.g. learner
      onItemPress(choice);
    }
  }, [onItemPress]);
  const handleItemInputChange = 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 = useCallback(_value => {
    analytics && analytics.logEvent(ANALYTICS_EVENT_TYPE.SLIDE, {
      id: 'slider',
      questionType: 'slider'
    });
    if (onSliderChange) {
      onSliderChange(_value);
    }
  }, [analytics, onSliderChange]);
  switch (type) {
    case 'qcm':
      return /*#__PURE__*/React.createElement(View, {
        testID: "question-choices"
      }, choices.map(choice => /*#__PURE__*/React.createElement(QuestionChoice, {
        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.createElement(View, {
        testID: "question-choices"
      }, choices.map(choice => /*#__PURE__*/React.createElement(QuestionChoice, {
        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.createElement(View, null);
        }
        return /*#__PURE__*/React.createElement(QuestionSlider, {
          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.createElement(View, null);
      }
      return /*#__PURE__*/React.createElement(View, {
        testID: "question-choices"
      }, /*#__PURE__*/React.createElement(QuestionTemplate, {
        isDisabled: isDisabled,
        template: template || '',
        choices: choices,
        onInputChange: handleItemInputChange,
        handleBlur: handleBlur,
        handleFocus: handleFocus,
        focusedSelectId: focusedSelectId
      }));
    case 'qcmDrag':
      return /*#__PURE__*/React.createElement(View, {
        testID: "question-draggable"
      }, /*#__PURE__*/React.createElement(QuestionDraggable, {
        choices: choices,
        onPress: handleChoicePress
      }));
    case 'basic':
      if (!onInputValueChange) {
        return /*#__PURE__*/React.createElement(View, null);
      }
      return /*#__PURE__*/React.createElement(FreeText, {
        fullWidth: true,
        onChange: onInputValueChange,
        testID: "question-input-text",
        questionType: "basic",
        isDisabled: isDisabled
      });
    default:
      return /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(Text, null, "Unhandled type: ", type));
  }
};
export default Switch;
//# sourceMappingURL=index.native.js.map