using Chronos.Core.Model;
using Chronos.Core.Project;
namespace Chronos.Data.Processing;
///
/// .chronos project files (BRIEF ยง18) against this index: the file for a sequence of one of its archives, and
/// the sequence a file names.
///
public static class ProjectBundles
{
///
/// The project file of with (the editor's current
/// settings), whose frame positions refer to frames.
///
/// The sequence is not in the index.
public static async Task CreateAsync(ChronosDatabase db, Guid sequenceId, ProjectSettings project, int frames,
CancellationToken cancellationToken = default)
{
var sequence = await db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException($"Sequence {sequenceId} is not in the index.");
var archive = await db.Archives.GetAsync(sequence.ArchiveId, cancellationToken).ConfigureAwait(false);
return new ProjectBundle(
new BundleArchive(sequence.ArchiveId, archive?.Name ?? string.Empty, archive?.RootPath),
new BundleSequence(sequence.Id, sequence.Folder, sequence.DisplayTitle, frames),
project,
DateTimeOffset.UtcNow);
}
///
/// The sequence names: by its id, else the sequence in the same folder of the same
/// archive (an index rebuilt elsewhere, or a folder adopted again, gives it a new id). Null when this index has
/// neither: the archive is not added here, or the sequence was deleted.
///
public static async Task ResolveAsync(ChronosDatabase db, ProjectBundle bundle, CancellationToken cancellationToken = default)
{
if (await db.Sequences.GetAsync(bundle.Sequence.Id, cancellationToken).ConfigureAwait(false) is { } byId) return byId;
var locations = await db.Sequences.ListLocationsAsync(bundle.Archive.Id, cancellationToken).ConfigureAwait(false);
var match = locations.FirstOrDefault(l => string.Equals(l.Folder, bundle.Sequence.Folder, StringComparison.OrdinalIgnoreCase));
return match is null ? null : await db.Sequences.GetAsync(match.Id, cancellationToken).ConfigureAwait(false);
}
}