import React, { useEffect, useState } from 'react';
import { View, StyleSheet } from 'react-native';
import Html from '../html/index.native';
import ImageBackground from '../image-background/index.native';
import getCleanUri from '../../util/get-clean-uri';
import Touchable from '../../hoc/touchable/index.native';
import { useTemplateContext } from '../../template/app-review/template-context';
const createStyleSheet = (theme, squeezed) => StyleSheet.create({
  boxShadow: {
    shadowColor: '#000',
    shadowOffset: {
      width: 0,
      height: 4
    },
    shadowOpacity: 0.12,
    shadowRadius: 8,
    elevation: 8,
    backgroundColor: '#0000'
  },
  container: {
    backgroundColor: theme.colors.white,
    borderRadius: theme.radius.regular,
    flexDirection: 'row',
    alignItems: 'stretch',
    minHeight: 60
  },
  textContainer: {
    paddingHorizontal: 24,
    paddingVertical: 12,
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1
  },
  squeezedTextContainer: {
    padding: theme.spacing.small,
    paddingLeft: undefined,
    paddingVertical: undefined,
    paddingRight: undefined,
    flex: 0
  },
  text: {
    fontSize: squeezed ? theme.fontSize.medium : theme.fontSize.regular,
    fontWeight: theme.fontWeight.bold,
    color: theme.colors.black,
    textAlign: 'center'
  },
  textSelected: {
    color: theme.colors.white
  },
  unselectedImageContainer: {
    borderRightColor: theme.colors.border
  },
  imageContainer: {
    height: '100%',
    width: '25%',
    overflow: 'hidden',
    borderTopLeftRadius: theme.radius.regular,
    borderBottomLeftRadius: theme.radius.regular
  },
  image: {
    flex: 1
  }
});
const Choice = ({
  children,
  isSelected = false,
  squeezed = false,
  isDisabled,
  onPress,
  media,
  testID: prefixTestID,
  style,
  questionType
}) => {
  const templateContext = useTemplateContext();
  const {
    theme,
    brandTheme
  } = templateContext;
  const [styleSheet, setStylesheet] = useState(null);
  useEffect(() => {
    const _stylesheet = createStyleSheet(theme, squeezed);
    setStylesheet(_stylesheet);
  }, [theme, squeezed]);
  if (!styleSheet) {
    return null;
  }
  const selectedSuffix = prefixTestID && isSelected ? '-selected' : '';
  const mediaType = media && media.type && media.type === 'img' && media.type.toLowerCase();
  const url = media && media.type === 'img' && media.src && media.src.length > 0 && getCleanUri(media.src[0].url);
  const source = {
    uri: url ? getCleanUri(url) : undefined
  };
  const mediaSuffix = prefixTestID && mediaType ? `-${mediaType}` : '';
  const textStyle = [styleSheet.text];
  const textWrapperStyle = [styleSheet.textContainer];
  if (squeezed) {
    textWrapperStyle.push(styleSheet.squeezedTextContainer);
  }
  if (isSelected) {
    textStyle.push(styleSheet.textSelected);

    // eslint-disable-next-line @coorpacademy/coorpacademy/no-overwriting-spread
    const selectionStyle = brandTheme && {
      backgroundColor: brandTheme.colors?.primary,
      borderColor: brandTheme.colors?.primary,
      ...(url ? {
        borderTopRightRadius: theme.radius.regular,
        borderBottomRightRadius: theme.radius.regular
      } : {
        borderRadius: theme.radius.regular
      })
    };
    if (selectionStyle) {
      textWrapperStyle.push(selectionStyle);
    }
  }
  return /*#__PURE__*/React.createElement(Touchable, {
    onPress: !isDisabled ? onPress : undefined,
    style: style,
    analyticsID: "question-choice",
    analyticsParams: {
      questionType
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: [styleSheet.boxShadow, styleSheet.container],
    testID: prefixTestID && `${prefixTestID}${selectedSuffix}`
  }, url ? /*#__PURE__*/React.createElement(View, {
    style: styleSheet.imageContainer
  }, /*#__PURE__*/React.createElement(ImageBackground, {
    testID: prefixTestID && `${prefixTestID}${mediaSuffix}`,
    source: source,
    style: styleSheet.image
  })) : null, children ? /*#__PURE__*/React.createElement(View, {
    style: textWrapperStyle
  }, /*#__PURE__*/React.createElement(Html, {
    style: textStyle,
    isTextCentered: true
  }, children)) : null));
};
export default Choice;
//# sourceMappingURL=index.native.js.map