"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.generateProcessingText = exports.generateDefaultText = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _native = _interopRequireWildcard(require("styled-components/native"));
var _reactNative = require("react-native");
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); }
/* eslint-disable react-hooks/rules-of-hooks -- pre-existing port artifacts; hooks called conditionally / inside helpers. TODO: refactor */
// CustomTypingIndicator.native.tsx

/* -------- Types -------- */

/* -------- Helpers (texts) -------- */
const generateDefaultText = usersTyping => {
  if (usersTyping.length === 0) {
    return '';
  }
  if (usersTyping.length === 1) {
    return `${usersTyping[0]} is typing`;
  }
  if (usersTyping.length === 2) {
    return `${usersTyping[0]} and ${usersTyping[1]} are typing`;
  }
  return `${usersTyping.length} people are typing`;
};
exports.generateDefaultText = generateDefaultText;
const generateProcessingText = usersTyping => {
  if (usersTyping.length === 0) {
    return '';
  }
  const processingStates = ['processing', 'thinking', 'generating answer'];
  const randomState = processingStates[Math.floor(Math.random() * processingStates.length)];
  if (usersTyping.length === 1) {
    return `${usersTyping[0]} is ${randomState}`;
  }
  if (usersTyping.length === 2) {
    return `${usersTyping[0]} and ${usersTyping[1]} are ${randomState}`;
  }
  return `${usersTyping.length} people are ${randomState}`;
};

/* -------- Styled (static layout) -------- */
exports.generateProcessingText = generateProcessingText;
const BaseWrapper = _native.default.View`
  flex-direction: row;
  align-items: center;
  z-index: 1000;
  /* основная типографика */
  /* (цвета текста задаём на текстовых нодах) */

  ${({
  $position
}) => {
  switch ($position) {
    case 'top':
      return (0, _native.css)`
          position: absolute;
          top: 8px;
          left: 16px;
          right: 16px;
          background-color: rgba(255, 255, 255, 0.95);
          padding: 8px 12px;
          border-radius: 8px;
          /* тени (iOS/Android) */
          shadow-color: #000;
          shadow-opacity: 0.1;
          shadow-radius: 8px;
          shadow-offset: 0px 2px;
          elevation: 3;
        `;
    case 'overlay':
      // В RN нет position: fixed, используем absolute + центрирование
      return (0, _native.css)`
          position: absolute;
          top: 0; left: 0; right: 0; bottom: 0;
          align-items: center;
          justify-content: center;
          background-color: rgba(0, 0, 0, 0.4); /* лёгкая вуаль */
          padding: 16px 24px;
        `;
    case 'floating':
      return (0, _native.css)`
          position: absolute;
          right: 20px;
          bottom: 80px;
          background-color: rgba(255, 255, 255, 0.95);
          padding: 12px 16px;
          border-radius: 20px;
          shadow-color: #000;
          shadow-opacity: 0.15;
          shadow-radius: 12px;
          shadow-offset: 0px 4px;
          elevation: 4;
        `;
    case 'bottom':
    default:
      return (0, _native.css)`
          /* встроенное расположение в потоке */
          padding: 8px 0;
        `;
  }
}}
`;
const Label = _native.default.Text`
  margin-right: 8px;
  font-weight: 500;
  font-size: 14px;
  color: #555;
`;
const DotsRow = _native.default.View`
  flex-direction: row;
  align-items: center;
`;

/* -------- Animated helpers -------- */

// pulse для wrapper'ов overlay/floating (scale + opacity)
const usePulse = () => {
  const scale = (0, _react.useRef)(new _reactNative.Animated.Value(1)).current;
  const opacity = (0, _react.useRef)(new _reactNative.Animated.Value(0.8)).current;
  (0, _react.useEffect)(() => {
    const loop = _reactNative.Animated.loop(_reactNative.Animated.parallel([_reactNative.Animated.sequence([_reactNative.Animated.timing(scale, {
      toValue: 1.05,
      duration: 1000,
      easing: _reactNative.Easing.inOut(_reactNative.Easing.ease),
      useNativeDriver: true
    }), _reactNative.Animated.timing(scale, {
      toValue: 1,
      duration: 1000,
      easing: _reactNative.Easing.inOut(_reactNative.Easing.ease),
      useNativeDriver: true
    })]), _reactNative.Animated.sequence([_reactNative.Animated.timing(opacity, {
      toValue: 1,
      duration: 1000,
      easing: _reactNative.Easing.inOut(_reactNative.Easing.ease),
      useNativeDriver: true
    }), _reactNative.Animated.timing(opacity, {
      toValue: 0.8,
      duration: 1000,
      easing: _reactNative.Easing.inOut(_reactNative.Easing.ease),
      useNativeDriver: true
    })])]));
    loop.start();
    return () => loop.stop();
  }, [opacity, scale]);
  return {
    scale,
    opacity
  };
};

// мигание одной точки
const useBlink = (delay = 0) => {
  const o = (0, _react.useRef)(new _reactNative.Animated.Value(0.2)).current;
  (0, _react.useEffect)(() => {
    const loop = _reactNative.Animated.loop(_reactNative.Animated.sequence([_reactNative.Animated.timing(o, {
      toValue: 1,
      duration: 300,
      delay,
      useNativeDriver: true
    }), _reactNative.Animated.timing(o, {
      toValue: 0.2,
      duration: 900,
      useNativeDriver: true
    })]));
    loop.start();
    return () => loop.stop();
  }, [o, delay]);
  return o;
};

/* Точка — как View (а не текст), чтобы выглядело одинаково на iOS/Android */
const DotView = (0, _native.default)(_reactNative.Animated.View)`
  width: 6px;
  height: 6px;
  border-radius: 3px;
  background-color: #666;
  margin-right: 2px;
`;

/* -------- Component -------- */

const CustomTypingIndicator = ({
  usersTyping = [],
  text,
  position = 'bottom',
  styles,
  customComponent: CustomComponent,
  isVisible = true
}) => {
  if (!isVisible || usersTyping.length === 0) {
    return null;
  }
  const displayText = (0, _react.useMemo)(() => {
    if (typeof text === 'function') {
      return text(usersTyping);
    }
    if (typeof text === 'string') {
      return text;
    }
    return generateDefaultText(usersTyping);
  }, [text, usersTyping]);

  // Анимации
  const pulse = usePulse();
  const o1 = useBlink(0);
  const o2 = useBlink(200);
  const o3 = useBlink(400);
  if (CustomComponent) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(CustomComponent, {
      usersTyping: usersTyping,
      text: displayText,
      isVisible: isVisible
    });
  }

  // Для overlay/floating оборачиваем в Animated.View, чтобы дать pulse
  const WrapperComponent = position === 'overlay' || position === 'floating' ? _reactNative.Animated.View : _reactNative.View;
  const animatedStyle = position === 'overlay' || position === 'floating' ? {
    transform: [{
      scale: pulse.scale
    }],
    opacity: pulse.opacity
  } : null;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(WrapperComponent, {
    style: animatedStyle,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(BaseWrapper, {
      $position: position,
      style: styles,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Label, {
        children: displayText
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(DotsRow, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(DotView, {
          style: {
            opacity: o1
          }
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(DotView, {
          style: {
            opacity: o2
          }
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(DotView, {
          style: {
            opacity: o3
          }
        })]
      })]
    })
  });
};
var _default = exports.default = CustomTypingIndicator;
//# sourceMappingURL=CustomTypingIndicator.js.map