"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _theme = require("../../contexts/theme");
var _testable = require("../../utils/testable");
var _RecordField = _interopRequireDefault(require("./RecordField"));
var _RecordFooter = _interopRequireDefault(require("./RecordFooter"));
var _RecordHeader = _interopRequireDefault(require("./RecordHeader"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
const Record = _ref => {
  let {
    header,
    footer,
    fields,
    hideFieldValues = false,
    field = null
  } = _ref;
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [shown, setShown] = (0, _react.useState)([]);
  const [showAll, setShowAll] = (0, _react.useState)(false);
  const {
    ListItems,
    TextTheme
  } = (0, _theme.useTheme)();
  const styles = _reactNative.StyleSheet.create({
    linkContainer: {
      ...ListItems.recordContainer,
      flexDirection: 'row',
      justifyContent: 'flex-end',
      paddingHorizontal: 25,
      paddingVertical: 16
    },
    link: {
      minHeight: TextTheme.normal.fontSize,
      paddingVertical: 2
    }
  });
  const resetShown = () => {
    setShown(fields.map(() => showAll));
    setShowAll(!showAll);
  };
  const toggleShownState = newShowStates => {
    if (newShowStates.filter(shownState => shownState === showAll).length > Math.floor(fields.length / 2)) {
      setShowAll(!showAll);
    }
  };
  (0, _react.useEffect)(() => {
    resetShown();
  }, []);
  return /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    data: fields,
    keyExtractor: (_ref2, index) => {
      let {
        name
      } = _ref2;
      return name || index.toString();
    },
    renderItem: _ref3 => {
      let {
        item: attr,
        index
      } = _ref3;
      return field ? field(attr, index, fields) : /*#__PURE__*/_react.default.createElement(_RecordField.default, {
        field: attr,
        hideFieldValue: hideFieldValues,
        onToggleViewPressed: () => {
          const newShowState = [...shown];
          newShowState[index] = !shown[index];
          setShown(newShowState);
          toggleShownState(newShowState);
        },
        shown: hideFieldValues ? !!shown[index] : true,
        hideBottomBorder: index === fields.length - 1
      });
    },
    ListHeaderComponent: header ? /*#__PURE__*/_react.default.createElement(_RecordHeader.default, null, header(), hideFieldValues ? /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: styles.linkContainer
    }, /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
      style: styles.link,
      activeOpacity: 1,
      onPress: () => resetShown(),
      testID: (0, _testable.testIdWithKey)('HideAll'),
      accessible: true,
      accessibilityLabel: showAll ? t('Record.ShowAll') : t('Record.HideAll')
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: ListItems.recordLink
    }, showAll ? t('Record.ShowAll') : t('Record.HideAll')))) : null) : null,
    ListFooterComponent: footer ? /*#__PURE__*/_react.default.createElement(_RecordFooter.default, null, footer()) : null
  });
};
var _default = Record;
exports.default = _default;
//# sourceMappingURL=Record.js.map