var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useRef, useState } from 'react';
import { _cx } from 'src/utility/class';
import css from './Video.module.scss';
import { track } from 'src/lib/common/helpers/tracking';
var cx = _cx(css);
var Video = function (props) {
    var videoRef = useRef(null);
    var firstTimePlay = useRef(false);
    var _a = __read(useState({
        playing: false,
        min: 0,
        max: 2,
        step: 0.1,
        progress: 0,
    }), 2), settings = _a[0], setSettings = _a[1];
    var _b = __read(useState(false), 2), buffer = _b[0], isBuffering = _b[1];
    var src = props.src, _c = props.autoPlay, autoPlay = _c === void 0 ? false : _c, _d = props.controls, controls = _d === void 0 ? false : _d, _e = props.loop, loop = _e === void 0 ? true : _e;
    if (!src)
        return null;
    var extra = {};
    if (autoPlay) {
        extra.autoPlay = true;
        extra.muted = false;
        extra.loop = loop;
        extra.src = src;
    }
    if (controls)
        extra.controls = true;
    var checkUpdatePlaying = function (event) {
        if (!videoRef.current) {
            return;
        }
        var video = videoRef.current;
        var playing = !!(!video.paused && !video.ended);
        var obj = { playing: playing };
        obj.progress = video.currentTime || 0;
        if (video.duration && !isNaN(video.duration)) {
            obj.max = video.duration;
        }
        obj.step = 0.1;
        obj.max = videoRef.current.duration.toFixed(1);
        obj.min = 0;
        setSettings(obj);
        isBuffering(videoRef.current.readyState < videoRef.current.HAVE_FUTURE_DATA);
    };
    var onProgress = function () {
        checkUpdatePlaying();
    };
    var onEnded = function () {
        checkUpdatePlaying();
    };
    var onPause = function () {
        checkUpdatePlaying();
    };
    var onPlay = function () {
        firstTimePlay.current = true;
        var urlParts = src.split('/');
        var fileName = urlParts[urlParts.length - 1];
        track('video_start', {
            video_title: fileName,
            video_url: 'https:' + src,
        });
        checkUpdatePlaying('onPlay');
    };
    var togglePlay = function () {
        if (!videoRef.current) {
            return;
        }
        if (settings.playing) {
            videoRef.current.pause();
        }
        else {
            videoRef.current.play();
        }
    };
    return (_jsxs("div", { className: cx('video-container'), onClick: togglePlay, children: [_jsx("video", __assign({ ref: videoRef, className: "video" }, extra, { onPlay: onPlay, onEnded: onEnded, onPause: onPause, onTimeUpdate: onProgress, playsInline: true, preload: "true", autoPlay: autoPlay, onClick: togglePlay, src: src })), !controls && (_jsx("div", { className: cx('custom-controls', settings.playing ? 'pause' : 'play'), children: _jsx("button", { className: cx('controls', settings.playing ? (buffer ? 'buffering' : 'pause-btn') : 'play-btn'), onClick: togglePlay, title: settings.playing ? 'Pause' : 'Play', "aria-label": settings.playing ? 'Pause' : 'Play' }) }))] }));
};
export default Video;
