using Febe.Core.Storage;
namespace Febe.Core.Collection.Requests;
/// What a copy of the browser's session does when some value the requests need is missing (issue #36, S4b).
public enum IncompleteCopyChoice
{
///
/// «Sostituisci comunque» (the default): the new copy replaces the saved one all the same, because keeping an older
/// copy could keep the cookies of another login (§6.0).
///
Replace,
///
/// «Tieni la copia completa»: when the saved copy has every value the configured operations need and the new one
/// does not, the saved one stays and the message says so.
///
KeepComplete,
}
///
/// The settings of the copy of the session (issue #36, S4b; decision of Alberto of 18 November 2026, answer 9), in the
/// setting table like the other settings. Read at every copy: a change holds from the next copy, no restart.
///
/// What an incomplete copy does.
public sealed record SessionCopySettings(IncompleteCopyChoice IncompleteCopy)
{
/// The key of : sostituisci or tieni-completa.
public const string IncompleteCopyKey = "sessione.copia-incompleta";
/// The text of in the table and in the API.
public const string ReplaceText = "sostituisci";
/// The text of in the table and in the API.
public const string KeepCompleteText = "tieni-completa";
/// The settings of a database that never stored them: an incomplete copy replaces the saved one.
public static SessionCopySettings Default { get; } = new(IncompleteCopyChoice.Replace);
/// The text of .
public static string ToText(IncompleteCopyChoice choice) => choice == IncompleteCopyChoice.KeepComplete ? KeepCompleteText : ReplaceText;
/// The choice of ; null for any other text.
public static IncompleteCopyChoice? Parse(string? text) => text switch
{
ReplaceText => IncompleteCopyChoice.Replace,
KeepCompleteText => IncompleteCopyChoice.KeepComplete,
_ => null,
};
/// The stored settings; an unknown or missing value is the default.
public static async Task LoadAsync(ISettingsStore settings, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(settings);
var text = await settings.GetAsync(IncompleteCopyKey, ct).ConfigureAwait(false);
return Parse(text) is { } choice ? new SessionCopySettings(choice) : Default;
}
/// Stores these settings.
public Task SaveAsync(ISettingsStore settings, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(settings);
return settings.SetAsync(IncompleteCopyKey, ToText(IncompleteCopy), ct);
}
}