"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _expoAv = require("expo-av");
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 formatTime = millis => {
  const totalSec = Math.floor(millis / 1000);
  const min = Math.floor(totalSec / 60);
  const sec = totalSec % 60;
  return `${min}:${sec.toString().padStart(2, '0')}`;
};
const AudioMessage = ({
  src
}) => {
  const soundRef = (0, _react.useRef)(null);
  const [isPlaying, setIsPlaying] = (0, _react.useState)(false);
  const [isLoading, setIsLoading] = (0, _react.useState)(false);
  const [position, setPosition] = (0, _react.useState)(0);
  const [duration, setDuration] = (0, _react.useState)(0);
  const onPlaybackStatusUpdate = status => {
    if (!status.isLoaded) return;
    setPosition(status.positionMillis);
    setDuration(status.durationMillis ?? 0);
    setIsPlaying(status.isPlaying);
    if (status.didJustFinish) {
      setIsPlaying(false);
      soundRef.current?.setPositionAsync(0);
    }
  };
  const togglePlayback = async () => {
    try {
      if (!soundRef.current) {
        setIsLoading(true);
        const {
          sound
        } = await _expoAv.Audio.Sound.createAsync({
          uri: src
        }, {
          shouldPlay: true
        }, onPlaybackStatusUpdate);
        soundRef.current = sound;
        setIsLoading(false);
        setIsPlaying(true);
        return;
      }
      if (isPlaying) {
        await soundRef.current.pauseAsync();
      } else {
        await soundRef.current.playAsync();
      }
    } catch (error) {
      console.error('Audio playback error:', error);
      setIsLoading(false);
    }
  };
  (0, _react.useEffect)(() => {
    return () => {
      soundRef.current?.unloadAsync();
    };
  }, []);
  const progress = duration > 0 ? position / duration : 0;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: styles.container,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
      style: styles.playButton,
      onPress: togglePlayback,
      disabled: isLoading,
      children: isLoading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ActivityIndicator, {
        size: "small",
        color: "#fff"
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: styles.playIcon,
        children: isPlaying ? '⏸' : '▶'
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: styles.progressContainer,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.progressTrack,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: [styles.progressFill, {
            width: `${progress * 100}%`
          }]
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.Text, {
        style: styles.time,
        children: [formatTime(position), " / ", formatTime(duration)]
      })]
    })]
  });
};
var _default = exports.default = AudioMessage;
const styles = _reactNative.StyleSheet.create({
  container: {
    flexDirection: 'row',
    alignItems: 'center',
    paddingHorizontal: 8,
    paddingVertical: 6,
    minWidth: 200
  },
  playButton: {
    width: 36,
    height: 36,
    borderRadius: 18,
    backgroundColor: '#007AFF',
    alignItems: 'center',
    justifyContent: 'center',
    marginRight: 10
  },
  playIcon: {
    color: '#fff',
    fontSize: 14
  },
  progressContainer: {
    flex: 1
  },
  progressTrack: {
    height: 4,
    backgroundColor: '#E0E0E0',
    borderRadius: 2,
    overflow: 'hidden'
  },
  progressFill: {
    height: '100%',
    backgroundColor: '#007AFF',
    borderRadius: 2
  },
  time: {
    fontSize: 11,
    color: '#888',
    marginTop: 4
  }
});
//# sourceMappingURL=AudioMessage.js.map