"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TabItem = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeHapticFeedback = _interopRequireDefault(require("react-native-haptic-feedback"));
var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated"));
var _context = require("../../context");
var _core = require("../../core");
var _hooks = require("../../hooks");
var _icons = require("../icons");
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 DISABLED_OPACITY = 0.5;
const TabItem = exports.TabItem = /*#__PURE__*/(0, _react.forwardRef)(({
  label,
  color = "light",
  selected = false,
  accessibilityLabel,
  accessibilityHint,
  testID,
  onPress,
  disabled = false,
  icon,
  iconSelected
}, ref) => {
  const {
    onPressIn,
    onPressOut,
    scaleAnimatedStyle
  } = (0, _hooks.useScaleAnimation)("medium");
  const theme = (0, _context.useIOTheme)();
  const reducedMotion = (0, _reactNativeReanimated.useReducedMotion)();
  const mapColorStates = (0, _react.useMemo)(() => ({
    light: {
      border: {
        default: _core.IOColors[theme["tab-item-border-default"]],
        selected: (0, _core.hexToRgba)(_core.IOColors[theme["tab-item-foreground-selected"]], 0.5)
      },
      background: {
        default: (0, _core.hexToRgba)(_core.IOColors[theme["tab-item-background-selected"]], 0),
        selected: (0, _core.hexToRgba)(_core.IOColors[theme["tab-item-background-selected"]], 0.25),
        pressed: _core.IOColors[theme["appBackground-primary"]]
      },
      foreground: {
        default: theme["tab-item-foreground-default"],
        selected: theme["tab-item-foreground-selected"],
        disabled: "grey-700"
      }
    },
    dark: {
      border: {
        default: (0, _core.hexToRgba)(_core.IOColors.white, 0),
        selected: _core.IOColors.white
      },
      background: {
        default: (0, _core.hexToRgba)(_core.IOColors.white, 0.1),
        selected: _core.IOColors.white,
        pressed: _core.IOColors.white
      },
      foreground: {
        default: "white",
        selected: "black",
        disabled: "white"
      }
    }
  }), [theme]);
  const itemState = selected ? "selected" : disabled ? "disabled" : "default";
  const foregroundColor = mapColorStates[color].foreground[itemState];
  const selectedStateTransition = (0, _reactNativeReanimated.useDerivedValue)(() => (0, _reactNativeReanimated.withSpring)(selected ? 1 : 0, _core.IOSpringValues.selection));

  // Interpolate animation values from `pressed` values
  const animatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => ({
    backgroundColor: (0, _reactNativeReanimated.interpolateColor)(selectedStateTransition.value, [0, 1], [mapColorStates[color].background.default, mapColorStates[color].background.selected]),
    borderColor: (0, _reactNativeReanimated.interpolateColor)(selectedStateTransition.value, [0, 1], [mapColorStates[color].border.default, mapColorStates[color].border.selected])
  }));
  const activeIcon = selected ? iconSelected ?? icon : icon;
  const handleOnPress = (0, _react.useCallback)(event => {
    if (onPress) {
      _reactNativeHapticFeedback.default.trigger("impactLight");
      onPress(event);
    }
  }, [onPress]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
    ref: ref,
    accessibilityLabel: accessibilityLabel,
    accessibilityHint: accessibilityHint,
    accessibilityRole: "tab",
    accessibilityState: {
      checked: !!selected
    },
    testID: testID,
    onPress: handleOnPress,
    onPressIn: onPressIn,
    onPressOut: onPressOut,
    accessible: true,
    disabled: disabled,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNativeReanimated.default.View, {
      style: [styles.container, {
        columnGap: 4
      }, !disabled && !reducedMotion && scaleAnimatedStyle, animatedStyle, disabled && {
        opacity: DISABLED_OPACITY
      }],
      children: [activeIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
        name: activeIcon,
        color: foregroundColor,
        size: 16
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.IOText, {
        size: 14,
        weight: "Semibold",
        color: foregroundColor,
        children: label
      })]
    })
  });
});
const styles = _reactNative.StyleSheet.create({
  container: {
    flexDirection: "row",
    alignItems: "center",
    paddingHorizontal: 16,
    paddingVertical: 8,
    borderWidth: 1,
    borderRadius: 64,
    borderCurve: "continuous",
    justifyContent: "center",
    alignSelf: "flex-start"
  }
});
//# sourceMappingURL=TabItem.js.map