﻿"use client";
import { BadgeCheck } from "lucide-react";

export function VideoDescription({ description }: { description: string }) {
  // Regex pour trouver des timestamps comme 01:23, 1:23, 01:23:45
  const timestampRegex = /\b(\d{1,2}:\d{2}(?::\d{2})?)\b/g;

  const parts = description.split(timestampRegex);

  const handleSeek = (timeStr: string) => {
    let seconds = 0;
    const p = timeStr.split(':').reverse();
    seconds += parseInt(p[0]); // secondes
    seconds += parseInt(p[1]) * 60; // minutes
    if (p[2]) seconds += parseInt(p[2]) * 3600; // heures

    // Dispatch a custom event that VideoPlayer will listen to
    window.dispatchEvent(new CustomEvent('seekTo', { detail: seconds }));
  };

  return (
    <p className="text-sm leading-relaxed whitespace-pre-wrap" style={{ color: "var(--text-secondary)" }}>
      {parts.map((part, index) => {
        if (timestampRegex.test(part)) {
          return (
            <button 
              key={index} 
              onClick={() => handleSeek(part)}
              className="text-[#e05a68] hover:underline font-medium"
            >
              {part}
            </button>
          );
        }
        return part;
      })}
    </p>
  );
}

