"use strict";

import { memo, useMemo } from "react";
import { Platform, View } from "react-native";
import { useIOTheme } from "../../context";
import { IOSelectionListItemStyles, IOSelectionListItemVisualParams } from "../../core";
import { useIOFontDynamicScale } from "../../utils/accessibility";
import { Badge } from "../badge";
import { Icon } from "../icons";
import { HSpacer, VSpacer } from "../layout";
import { LoadingSpinner } from "../loadingSpinner";
import { LogoPayment } from "../logos";
import { NativeSwitch } from "../switch/NativeSwitch";
import { BodySmall, H6, LabelMini } from "../typography";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const DISABLED_OPACITY = 0.5;

/* Estimated height of the native switch component,
both on iOS & Android */
const ESTIMATED_SWITCH_HEIGHT = 32;
export const ListItemSwitch = /*#__PURE__*/memo(({
  label,
  description,
  icon,
  paymentLogo,
  value = false,
  disabled,
  action,
  isLoading,
  badge,
  onSwitchValueChange,
  switchTestID,
  testID
}) => {
  const theme = useIOTheme();
  const {
    dynamicFontScale,
    spacingScaleMultiplier,
    hugeFontEnabled
  } = 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 = useMemo(() => !isLoading && !badge, [isLoading, badge]);
  return /*#__PURE__*/_jsxs(View, {
    testID: testID ?? "ListItemSwitch",
    style: [IOSelectionListItemStyles.listItem, {
      opacity: disabled ? DISABLED_OPACITY : 1
    }],
    pointerEvents: disabled ? "none" : "auto",
    needsOffscreenAlphaCompositing: true,
    accessible: false,
    children: [/*#__PURE__*/_jsxs(View, {
      style: [IOSelectionListItemStyles.listItemInner, {
        alignItems: "center"
      }],
      accessible: false,
      children: [/*#__PURE__*/_jsxs(View, {
        style: {
          flex: 1,
          flexDirection: "row",
          alignItems: icon ? "flex-start" : "center",
          columnGap: IOSelectionListItemVisualParams.iconMargin * dynamicFontScale * spacingScaleMultiplier
        },
        accessible: !canRenderSwitch,
        ...Platform.select({
          android: {
            importantForAccessibility: !canRenderSwitch ? "yes" : "no-hide-descendants"
          }
        }),
        accessibilityState: {
          disabled
        },
        children: [icon && !hugeFontEnabled && /*#__PURE__*/_jsx(Icon, {
          allowFontScaling: true,
          name: icon,
          color: theme["icon-decorative"],
          size: IOSelectionListItemVisualParams.iconSize
        }), paymentLogo && /*#__PURE__*/_jsx(LogoPayment, {
          name: paymentLogo,
          size: IOSelectionListItemVisualParams.iconSize
        }), /*#__PURE__*/_jsx(H6, {
          color: theme["textBody-default"],
          style: {
            flex: 1
          },
          accessible: !canRenderSwitch,
          importantForAccessibility: !canRenderSwitch ? "yes" : "no-hide-descendants",
          children: label
        })]
      }), /*#__PURE__*/_jsx(HSpacer, {
        size: 8
      }), /*#__PURE__*/_jsxs(View, {
        style: {
          justifyContent: "center",
          alignSelf: "flex-start",
          minHeight: ESTIMATED_SWITCH_HEIGHT
        },
        children: [badge && /*#__PURE__*/_jsx(Badge, {
          text: badge.text,
          variant: badge.variant,
          testID: badge.testID
        }), isLoading && /*#__PURE__*/_jsx(LoadingSpinner, {
          size: 24
        }), canRenderSwitch && /*#__PURE__*/_jsx(NativeSwitch, {
          value: value,
          accessibilityLabel: label,
          disabled: disabled,
          onValueChange: onSwitchValueChange,
          testID: switchTestID
        })]
      })]
    }), description && /*#__PURE__*/_jsxs(_Fragment, {
      children: [/*#__PURE__*/_jsx(VSpacer, {
        size: IOSelectionListItemVisualParams.descriptionMargin
      }), /*#__PURE__*/_jsx(BodySmall, {
        weight: "Regular",
        color: theme["textBody-tertiary"],
        children: description
      })]
    }), action && /*#__PURE__*/_jsxs(_Fragment, {
      children: [/*#__PURE__*/_jsx(VSpacer, {
        size: IOSelectionListItemVisualParams.actionMargin
      }), /*#__PURE__*/_jsx(LabelMini, {
        asLink: true,
        onPress: action.onPress,
        children: action.label
      })]
    })]
  });
});
//# sourceMappingURL=ListItemSwitch.js.map