"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.INTERVAL_DELAY = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
require("core-js/modules/es.number.constructor.js");
require("core-js/modules/web.timers.js");
var _react = _interopRequireDefault(require("react"));
var _uiHelpers = require("@megafon/ui-helpers");
var _AudioRange = _interopRequireDefault(require("./AudioRange"));
var _useTrackDuration = _interopRequireDefault(require("./hooks/useTrackDuration"));
var _timerFormatUtil = _interopRequireDefault(require("./timerFormatUtil"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var INTERVAL_DELAY = exports.INTERVAL_DELAY = 1000;
var cn = (0, _uiHelpers.cnCreate)('mfui-9-audio-progress');
var AudioProgress = function AudioProgress(_ref) {
  var audioRef = _ref.audioRef,
    audioTitle = _ref.audioTitle,
    isPlaying = _ref.isPlaying,
    isPause = _ref.isPause,
    onChangeAudioCurrentTime = _ref.onChangeAudioCurrentTime,
    onPlay = _ref.onPlay,
    onSetIsPlaying = _ref.onSetIsPlaying;
  var intervalId = _react["default"].useRef(null);
  var _React$useState = _react["default"].useState(0),
    _React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2),
    trackProgress = _React$useState2[0],
    setTrackProgress = _React$useState2[1];
  var trackDuration = (0, _useTrackDuration["default"])(audioRef);
  var progressColorPercent = _react["default"].useMemo(function () {
    return trackProgress / trackDuration * 100;
  }, [trackDuration, trackProgress]);
  var progressValue = _react["default"].useMemo(function () {
    return isPlaying || isPause ? trackProgress : trackDuration;
  }, [isPause, isPlaying, trackDuration, trackProgress]);
  var handleStartTimer = _react["default"].useCallback(function () {
    intervalId.current && clearInterval(intervalId.current);
    intervalId.current = setInterval(function () {
      var _a, _b, _c;
      if ((_a = audioRef.current) === null || _a === void 0 ? void 0 : _a.ended) {
        onSetIsPlaying === null || onSetIsPlaying === void 0 ? void 0 : onSetIsPlaying(false);
        setTrackProgress(Math.floor(((_b = audioRef.current) === null || _b === void 0 ? void 0 : _b.duration) || 0));
        return;
      }
      setTrackProgress(Math.floor(((_c = audioRef.current) === null || _c === void 0 ? void 0 : _c.currentTime) || 0));
    }, INTERVAL_DELAY);
  }, [audioRef, onSetIsPlaying]);
  var handleScrubProgress = function handleScrubProgress(e) {
    var currentTime = Number(e.target.value);
    setTrackProgress(currentTime);
    onChangeAudioCurrentTime === null || onChangeAudioCurrentTime === void 0 ? void 0 : onChangeAudioCurrentTime(currentTime);
  };
  var handleScrubEndProgress = function handleScrubEndProgress() {
    if (!isPlaying) {
      onPlay === null || onPlay === void 0 ? void 0 : onPlay();
    }
    handleStartTimer();
  };
  _react["default"].useEffect(function () {
    var _a;
    setTrackProgress(Math.floor(((_a = audioRef === null || audioRef === void 0 ? void 0 : audioRef.current) === null || _a === void 0 ? void 0 : _a.currentTime) || 0));
  }, [audioRef]);
  _react["default"].useEffect(function () {
    if (isPlaying) {
      handleStartTimer();
    }
    if (isPause) {
      intervalId.current && clearInterval(intervalId.current);
    }
  }, [handleStartTimer, isPause, isPlaying]);
  _react["default"].useEffect(function () {
    return function () {
      intervalId.current && clearInterval(intervalId.current);
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: cn()
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: cn('info')
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: cn('title')
  }, audioTitle), /*#__PURE__*/_react["default"].createElement("span", {
    className: cn('value')
  }, (0, _timerFormatUtil["default"])(progressValue))), /*#__PURE__*/_react["default"].createElement("div", {
    className: cn('range-wrapper')
  }, /*#__PURE__*/_react["default"].createElement(_AudioRange["default"], {
    className: cn('range'),
    maxValue: trackDuration,
    value: trackProgress,
    step: "1",
    colorPercent: progressColorPercent || 0,
    onChange: handleScrubProgress,
    onMouseUp: handleScrubEndProgress,
    onTouchEnd: handleScrubEndProgress,
    dataAttrs: {
      'data-testid': 'AudioTimeRange'
    }
  })));
};
var _default = exports["default"] = AudioProgress;