"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Switch = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useTheme = require("../theme/use-theme.js");
var _tokens = require("../styles/tokens.js");
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); }
const TRACK = {
  default: {
    w: 44,
    h: 26
  },
  sm: {
    w: 36,
    h: 22
  }
};
const THUMB = {
  default: 22,
  sm: 18
};
const PADDING = 2;
const SQUEEZE_RATIO = 0.75; // thumb height shrinks to 75% when pressed

const SwitchComponent = /*#__PURE__*/_react.default.forwardRef(({
  value,
  onValueChange,
  disabled,
  style,
  size = 'default',
  testID
}, ref) => {
  const theme = (0, _useTheme.useTheme)();
  const anim = (0, _react.useRef)(new _reactNative.Animated.Value(value ? 1 : 0)).current;
  const pressAnim = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current;

  // Track last value to detect external changes and animate accordingly.
  // This avoids useEffect while still handling controlled value changes.
  const prevValueRef = (0, _react.useRef)(value);
  if (prevValueRef.current !== value) {
    prevValueRef.current = value;
    _reactNative.Animated.spring(anim, {
      toValue: value ? 1 : 0,
      useNativeDriver: false,
      ..._tokens.animation.spring.gentle
    }).start();
  }
  const handlePress = (0, _react.useCallback)(() => {
    if (disabled) return;
    onValueChange(!value);
  }, [disabled, value, onValueChange]);
  const onPressIn = (0, _react.useCallback)(() => {
    if (disabled) return;
    _reactNative.Animated.spring(pressAnim, {
      toValue: 1,
      useNativeDriver: false,
      ..._tokens.animation.spring.snappy
    }).start();
  }, [disabled, pressAnim]);
  const onPressOut = (0, _react.useCallback)(() => {
    _reactNative.Animated.spring(pressAnim, {
      toValue: 0,
      useNativeDriver: false,
      ..._tokens.animation.spring.gentle
    }).start();
  }, [pressAnim]);
  const track = TRACK[size];
  const thumb = THUMB[size];
  const travel = track.w - thumb - PADDING * 2;
  const trackBg = anim.interpolate({
    inputRange: [0, 1],
    outputRange: [theme.colors.border, theme.colors.primary]
  });
  const thumbX = anim.interpolate({
    inputRange: [0, 1],
    outputRange: [PADDING, PADDING + travel]
  });
  const squeezedHeight = thumb * SQUEEZE_RATIO;
  const thumbHeight = pressAnim.interpolate({
    inputRange: [0, 1],
    outputRange: [thumb, squeezedHeight]
  });
  const thumbRadius = pressAnim.interpolate({
    inputRange: [0, 1],
    outputRange: [thumb / 2, squeezedHeight / 2]
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
    ref: ref,
    role: "switch",
    "aria-checked": value,
    accessibilityState: {
      checked: value,
      disabled
    },
    onPress: handlePress,
    onPressIn: onPressIn,
    onPressOut: onPressOut,
    style: [disabled && styles.disabled, style],
    hitSlop: HIT_SLOP,
    testID: testID,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.View, {
      style: {
        width: track.w,
        height: track.h,
        borderRadius: track.h / 2,
        backgroundColor: trackBg,
        justifyContent: 'center',
        alignItems: 'flex-start'
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.View, {
        style: [styles.thumb, {
          width: thumb,
          height: thumbHeight,
          borderRadius: thumbRadius,
          transform: [{
            translateX: thumbX
          }]
        }]
      })
    })
  });
});
SwitchComponent.displayName = 'Switch';
const Switch = exports.Switch = /*#__PURE__*/(0, _react.memo)(SwitchComponent);
const HIT_SLOP = {
  top: 8,
  bottom: 8,
  left: 8,
  right: 8
};
const styles = _reactNative.StyleSheet.create({
  disabled: {
    opacity: 0.4
  },
  thumb: {
    backgroundColor: '#fff',
    shadowColor: '#000',
    shadowOffset: {
      width: 0,
      height: 2
    },
    shadowOpacity: 0.15,
    shadowRadius: 3,
    elevation: 3
  }
});
//# sourceMappingURL=Switch.js.map