"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ClaimsSelector = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeGestureHandler = require("react-native-gesture-handler");
var _reactNativeLinearGradient = _interopRequireDefault(require("react-native-linear-gradient"));
var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated"));
var _context = require("../../context");
var _core = require("../../core");
var _useAccordionAnimation = require("../../hooks/useAccordionAnimation");
var _icons = require("../icons");
var _layout = require("../layout");
var _listitems = require("../listitems");
var _typography = require("../typography");
var _jsxRuntime = require("react/jsx-runtime");
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); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const accordionBodySpacing = 16;

// Threshold to determine when the accordion is considered fully collapsed
const COLLAPSED_RADIUS_THRESHOLD = 0.001;

// Border width offset to ensure gradient fits within the border curves
const COLLAPSIBLE_BORDER = 1;
const ClaimsSelector = ({
  title,
  items,
  defaultExpanded,
  onItemSelected,
  onToggle,
  accessibilityLabel,
  selectedItemIds,
  selectionEnabled = true,
  headerGradientColors
}) => {
  const theme = (0, _context.useIOTheme)();
  const {
    expanded,
    toggleAccordion,
    onBodyLayout,
    iconAnimatedStyle,
    bodyAnimatedStyle,
    bodyInnerStyle,
    progress
  } = (0, _useAccordionAnimation.useAccordionAnimation)({
    defaultExpanded
  });
  const accordionBackground = theme["appBackground-primary"];
  const accordionBorder = theme["cardBorder-default"];
  const onItemPress = () => {
    toggleAccordion();
    onToggle?.(!expanded);
  };
  const hasHeaderGradient = headerGradientColors && headerGradientColors.length >= 2;
  const headerForegroundColor = hasHeaderGradient ? "black" : theme["textBody-default"];
  const headerRadiusAnimatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
    /**
     * Dynamically adjust bottom corner radius based on the expansion progress.
     * Bottom corners are rounded only when the accordion is fully collapsed to
     * ensure visual consistency with the outer container.
     */
    const bottomRadius = progress.value < COLLAPSED_RADIUS_THRESHOLD ? _core.IOAccordionRadius : 0;
    return {
      borderTopLeftRadius: _core.IOAccordionRadius - COLLAPSIBLE_BORDER,
      borderTopRightRadius: _core.IOAccordionRadius - COLLAPSIBLE_BORDER,
      borderBottomLeftRadius: bottomRadius - COLLAPSIBLE_BORDER,
      borderBottomRightRadius: bottomRadius - COLLAPSIBLE_BORDER,
      overflow: "hidden"
    };
  });
  const renderClaimItem = (item, index) => {
    const {
      id,
      value,
      description,
      type = "text",
      endElement
    } = item;
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, {
      children: [index !== 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.Divider, {}),
      // We do not support checkbox selection for images, as it is not needed now
      selectionEnabled && type === "text" ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_listitems.ListItemCheckbox, {
        value: value,
        description: description,
        selected: selectedItemIds?.includes(id),
        onValueChange: onItemSelected ? selected => onItemSelected(item, selected) : undefined
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_listitems.ListItemInfo, {
        value: type === "image" ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Image, {
          source: {
            uri: value
          },
          style: styles.imageClaim,
          resizeMode: "contain",
          accessibilityIgnoresInvertColors: true
        }) : value,
        label: description,
        accessibilityRole: type,
        reversed: true,
        endElement: endElement
      })]
    }, id);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: [styles.accordionWrapper, {
      backgroundColor: _core.IOColors[accordionBackground],
      borderColor: _core.IOColors[accordionBorder]
    }],
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeGestureHandler.TouchableWithoutFeedback, {
      accessible: true,
      accessibilityRole: "button",
      accessibilityState: {
        expanded
      },
      accessibilityLabel: accessibilityLabel ?? title,
      onPress: onItemPress,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNativeReanimated.default.View, {
        style: [styles.textContainer, headerRadiusAnimatedStyle],
        children: [hasHeaderGradient && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeLinearGradient.default, {
          colors: headerGradientColors,
          style: _reactNative.StyleSheet.absoluteFill
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.H6, {
          color: headerForegroundColor,
          children: title
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
          style: iconAnimatedStyle,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
            name: "chevronBottom",
            color: headerForegroundColor
          })
        })]
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
      style: bodyAnimatedStyle,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: [bodyInnerStyle, styles.bodyInnerContainer],
        onLayout: onBodyLayout,
        children: items.map(renderClaimItem)
      })
    })]
  });
};
exports.ClaimsSelector = ClaimsSelector;
const styles = _reactNative.StyleSheet.create({
  accordionWrapper: {
    borderWidth: 1,
    borderRadius: _core.IOAccordionRadius,
    borderCurve: "continuous"
  },
  textContainer: {
    padding: accordionBodySpacing,
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between"
  },
  bodyInnerContainer: {
    width: "100%"
  },
  imageClaim: {
    width: 160,
    aspectRatio: 3 / 4
  }
});
//# sourceMappingURL=ClaimsSelector.js.map