"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ListItemSwitch = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _context = require("../../context");
var _core = require("../../core");
var _accessibility = require("../../utils/accessibility");
var _badge = require("../badge");
var _icons = require("../icons");
var _layout = require("../layout");
var _loadingSpinner = require("../loadingSpinner");
var _logos = require("../logos");
var _NativeSwitch = require("../switch/NativeSwitch");
var _typography = require("../typography");
var _jsxRuntime = require("react/jsx-runtime");
const DISABLED_OPACITY = 0.5;

/* Estimated height of the native switch component,
both on iOS & Android */
const ESTIMATED_SWITCH_HEIGHT = 32;
const ListItemSwitch = exports.ListItemSwitch = /*#__PURE__*/(0, _react.memo)(({
  label,
  description,
  icon,
  paymentLogo,
  value = false,
  disabled,
  action,
  isLoading,
  badge,
  onSwitchValueChange,
  switchTestID,
  testID
}) => {
  const theme = (0, _context.useIOTheme)();
  const {
    dynamicFontScale,
    spacingScaleMultiplier,
    hugeFontEnabled
  } = (0, _accessibility.useIOFontDynamicScale)();

  // If we have a badge or we are loading, we can't render the switch
  // this affects the accessibility tree and the rendering of the component
  const canRenderSwitch = (0, _react.useMemo)(() => !isLoading && !badge, [isLoading, badge]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    testID: testID ?? "ListItemSwitch",
    style: [_core.IOSelectionListItemStyles.listItem, {
      opacity: disabled ? DISABLED_OPACITY : 1
    }],
    pointerEvents: disabled ? "none" : "auto",
    needsOffscreenAlphaCompositing: true,
    accessible: false,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: [_core.IOSelectionListItemStyles.listItemInner, {
        alignItems: "center"
      }],
      accessible: false,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: {
          flex: 1,
          flexDirection: "row",
          alignItems: icon ? "flex-start" : "center",
          columnGap: _core.IOSelectionListItemVisualParams.iconMargin * dynamicFontScale * spacingScaleMultiplier
        },
        accessible: !canRenderSwitch,
        ..._reactNative.Platform.select({
          android: {
            importantForAccessibility: !canRenderSwitch ? "yes" : "no-hide-descendants"
          }
        }),
        accessibilityState: {
          disabled
        },
        children: [icon && !hugeFontEnabled && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
          allowFontScaling: true,
          name: icon,
          color: theme["icon-decorative"],
          size: _core.IOSelectionListItemVisualParams.iconSize
        }), paymentLogo && /*#__PURE__*/(0, _jsxRuntime.jsx)(_logos.LogoPayment, {
          name: paymentLogo,
          size: _core.IOSelectionListItemVisualParams.iconSize
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.H6, {
          color: theme["textBody-default"],
          style: {
            flex: 1
          },
          accessible: !canRenderSwitch,
          importantForAccessibility: !canRenderSwitch ? "yes" : "no-hide-descendants",
          children: label
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.HSpacer, {
        size: 8
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: {
          justifyContent: "center",
          alignSelf: "flex-start",
          minHeight: ESTIMATED_SWITCH_HEIGHT
        },
        children: [badge && /*#__PURE__*/(0, _jsxRuntime.jsx)(_badge.Badge, {
          text: badge.text,
          variant: badge.variant,
          testID: badge.testID
        }), isLoading && /*#__PURE__*/(0, _jsxRuntime.jsx)(_loadingSpinner.LoadingSpinner, {
          size: 24
        }), canRenderSwitch && /*#__PURE__*/(0, _jsxRuntime.jsx)(_NativeSwitch.NativeSwitch, {
          value: value,
          accessibilityLabel: label,
          disabled: disabled,
          onValueChange: onSwitchValueChange,
          testID: switchTestID
        })]
      })]
    }), description && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
        size: _core.IOSelectionListItemVisualParams.descriptionMargin
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.BodySmall, {
        weight: "Regular",
        color: theme["textBody-tertiary"],
        children: description
      })]
    }), action && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
        size: _core.IOSelectionListItemVisualParams.actionMargin
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.LabelMini, {
        asLink: true,
        onPress: action.onPress,
        children: action.label
      })]
    })]
  });
});
//# sourceMappingURL=ListItemSwitch.js.map