<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Utility;

/**
 * Utility helpers for stable queue item keys.
 */
final class QueueItemKey {

  /**
   * Builds a stable queue item key from media and field context.
   */
  #[\NoDiscard]
  public static function fromParts(int $mediaId, string $fieldName = ''): string {
    return $fieldName === '' ? (string) $mediaId : $mediaId . ':' . $fieldName;
  }

  /**
   * Parses a queue item key into media and field components.
   *
   * @return array{media_id: int, field_name: string}
   *   Parsed queue item data.
   */
  #[\NoDiscard]
  public static function parse(string|int $itemId): array {
    $parts = explode(':', (string) $itemId, 2);

    return [
      'media_id' => (int) ($parts[0] ?? 0),
      'field_name' => (string) ($parts[1] ?? ''),
    ];
  }

  /**
   * Checks whether a queue item key is syntactically valid.
   */
  #[\NoDiscard]
  public static function isValid(string|int $itemId): bool {
    $parsed = self::parse($itemId);

    return $parsed['media_id'] > 0;
  }

}
