/**
 * Parse CMX3600 EDL format
 * Standard edit line format:
 * EDIT# REEL TRACK_TYPE TRANS_TYPE SOURCE_IN SOURCE_OUT RECORD_IN RECORD_OUT
 *
 * Example:
 * 001  REEL001  V     C        01:00:00:00 01:00:05:00 01:00:00:00 01:00:05:00
 */
export const parseEDL = (edlContent) => {
  const lines = edlContent
    .split('\n')
    .map(line => line.trim())
    .filter(line => line && !line.startsWith('TITLE:'));

  const edits = [];

  for (const line of lines) {
    // Skip empty lines and non-edit lines
    if (!line || line.startsWith('*') || line.startsWith('TITLE')) {
      continue;
    }

    const parts = line.split(/\s+/);

    if (parts.length < 8) {
      continue;
    }

    const edit = {
      editNumber: parts[0],
      reelName: parts[1],
      trackType: parts[2],
      transitionType: parts[3],
      sourceIn: parts[4],
      sourceOut: parts[5],
      recordIn: parts[6],
      recordOut: parts[7],
    };

    edits.push(edit);
  }

  return edits;
};

/**
 * Convert timecode string (HH:MM:SS:FF) to seconds
 */
export const timecodeToSeconds = (timecode) => {
  const [hours, minutes, seconds, frames] = timecode
    .split(':')
    .map(Number);

  // Assuming 30fps (adjust if needed for 24fps or 25fps)
  const fps = 30;
  const frameSeconds = frames / fps;

  return hours * 3600 + minutes * 60 + seconds + frameSeconds;
};

/**
 * Convert seconds to timecode string (HH:MM:SS:FF)
 */
export const secondsToTimecode = (totalSeconds, fps = 30) => {
  const hours = Math.floor(totalSeconds / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  const seconds = Math.floor(totalSeconds % 60);
  const frames = Math.round((totalSeconds % 1) * fps);

  return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}:${String(frames).padStart(2, '0')}`;
};
