using System.Globalization;
using Microsoft.AspNetCore.Components;
namespace Radzen.Blazor.Spreadsheet;
#nullable enable
///
/// Base class for spreadsheet dialogs that provides shared injection,
/// a localizer, and a default cancel handler.
///
public abstract class SpreadsheetDialogBase : ComponentBase
{
///
/// Gets or sets the dialog service used to close the dialog.
///
[Inject]
protected DialogService DialogService { get; set; } = default!;
///
/// Gets or sets the localizer used to resolve resource strings.
///
[Inject]
internal Localizer Localizer { get; set; } = default!;
///
/// The UI culture cascaded from a parent component. Dialogs render outside the component that
/// opened them, so this resolves only when a DefaultUICulture cascade is set above the
/// dialog host; otherwise falls back to .
///
[CascadingParameter(Name = "DefaultUICulture")]
protected CultureInfo? DefaultUICulture { get; set; }
///
/// Returns the localized string for the specified resource key in the current UI culture.
///
protected string L(string key) => Localizer.Get(key, DefaultUICulture ?? CultureInfo.CurrentUICulture);
///
/// The culture used to parse and format values entered in the dialog. The spreadsheet passes
/// its workbook culture when opening the dialog.
///
[Parameter]
public CultureInfo Culture { get; set; } = CultureInfo.CurrentCulture;
///
/// Converts a numeric string entered in to the canonical invariant form
/// stored by the engine. Non-numeric text is returned verbatim.
///
protected string ToInvariantNumber(string value) =>
double.TryParse(value, NumberStyles.Any, Culture, out var number)
? number.ToString(CultureInfo.InvariantCulture)
: value;
///
/// Converts a canonical invariant numeric string to for display.
/// Non-numeric text is returned verbatim.
///
protected string ToLocalNumber(string value) =>
double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var number)
? number.ToString(Culture)
: value;
///
/// Closes the dialog without returning a result.
///
protected virtual void OnCancel() => DialogService.Close();
}