"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.ListItemInfo = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactNativeReanimated = _interopRequireDefault(require("react-native-reanimated"));
var _core = require("../../core");
var _context = require("../../context");
var _hooks = require("../../hooks");
var _accessibility = require("../../utils/accessibility");
var _badge = require("../badge");
var _buttons = require("../buttons");
var _LogoPaymentWithFallback = require("../common/LogoPaymentWithFallback");
var _icons = require("../icons");
var _typography = require("../typography");
var _layout = require("../layout");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const PAYMENT_LOGO_SIZE = 24;
const ListItemInfo = ({
  value,
  label,
  numberOfLines = 2,
  reversed = false,
  icon,
  paymentLogoIcon,
  endElement,
  topElement,
  accessibilityLabel,
  accessibilityRole,
  accessibilityActions,
  onAccessibilityAction,
  onLongPress,
  testID
}) => {
  const theme = (0, _context.useIOTheme)();
  const {
    dynamicFontScale,
    spacingScaleMultiplier,
    hugeFontEnabled
  } = (0, _accessibility.useIOFontDynamicScale)();
  const {
    onPressIn,
    onPressOut,
    scaleAnimatedStyle,
    backgroundAnimatedStyle
  } = (0, _hooks.useListItemAnimation)();
  const componentValueToAccessibility = (0, _react.useMemo)(() => typeof value === "string" ? value : "", [value]);
  const listItemAccessibilityLabel = (0, _react.useMemo)(() => accessibilityLabel ?? (label ? `${label}; ${componentValueToAccessibility}` : componentValueToAccessibility), [label, componentValueToAccessibility, accessibilityLabel]);
  const itemInfoTextComponent = (0, _react.useMemo)(() => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    accessible: _reactNative.Platform.OS === "ios",
    style: {
      flexDirection: reversed ? "column-reverse" : "column"
    },
    children: [topElement?.type === "badge" && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: {
        alignSelf: "flex-start"
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_badge.Badge, {
        ...topElement.componentProps
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
        size: 4
      })]
    }), label && /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.BodySmall, {
      weight: "Regular",
      color: theme["textBody-tertiary"],
      children: label
    }), typeof value === "string" ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.H6, {
      color: theme["textBody-default"],
      numberOfLines: numberOfLines,
      children: value
    }) : value]
  }), [label, value, numberOfLines, theme, reversed, topElement]);
  const listItemInfoAction = (0, _react.useCallback)(() => {
    if (endElement) {
      const {
        type,
        componentProps
      } = endElement;
      switch (type) {
        case "buttonLink":
          const buttonLinkAccessibilityLabel = `${componentProps.accessibilityLabel ?? componentProps.label}`;
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_buttons.IOButton, {
            variant: "link",
            ...componentProps,
            accessibilityLabel: buttonLinkAccessibilityLabel
          });
        case "iconButton":
          const iconButtonAccessibilityLabel = `${componentProps.accessibilityLabel}`;
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_buttons.IconButton, {
            ...componentProps,
            accessibilityLabel: iconButtonAccessibilityLabel
          });
        case "badge":
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_badge.Badge, {
            ...componentProps
          });
        default:
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {});
      }
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {});
  }, [endElement]);
  const ListItemInfoContent = () => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [icon && !hugeFontEnabled && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
      allowFontScaling: true,
      name: icon,
      color: theme["icon-decorative"],
      size: _core.IOListItemVisualParams.iconSize
    }), paymentLogoIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_LogoPaymentWithFallback.LogoPaymentWithFallback, {
      brand: paymentLogoIcon,
      size: PAYMENT_LOGO_SIZE
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: {
        flex: 1
      },
      children: itemInfoTextComponent
    }), endElement && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      children: listItemInfoAction()
    })]
  });
  if (onLongPress) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
      onLongPress: onLongPress,
      testID: testID,
      accessible: !endElement,
      onPressIn: onPressIn,
      onPressOut: onPressOut,
      onTouchEnd: onPressOut,
      accessibilityRole: "button",
      accessibilityLabel: listItemAccessibilityLabel,
      accessibilityActions: accessibilityActions,
      onAccessibilityAction: onAccessibilityAction,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
        style: [_core.IOListItemStyles.listItem, backgroundAnimatedStyle],
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
          style: [_core.IOListItemStyles.listItemInner, {
            columnGap: _core.IOListItemVisualParams.iconMargin * dynamicFontScale * spacingScaleMultiplier
          }, scaleAnimatedStyle],
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ListItemInfoContent, {})
        })
      })
    });
  } else {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: _core.IOListItemStyles.listItem,
      testID: testID,
      accessible: !endElement,
      accessibilityLabel: listItemAccessibilityLabel,
      accessibilityRole: accessibilityRole,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: [_core.IOListItemStyles.listItemInner, {
          columnGap: _core.IOListItemVisualParams.iconMargin * dynamicFontScale * spacingScaleMultiplier
        }],
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ListItemInfoContent, {})
      })
    });
  }
};
exports.ListItemInfo = ListItemInfo;
var _default = exports.default = ListItemInfo;
//# sourceMappingURL=ListItemInfo.js.map