namespace MarkdownEditor.Models;

/// <summary>
/// Represents a text selection range within a document.
/// Used for formatting operations on selected text.
/// </summary>
public class TextSelection
{
    /// <summary>
    /// Gets or sets the starting position of the selection.
    /// </summary>
    public int Start { get; set; }

    /// <summary>
    /// Gets or sets the ending position of the selection.
    /// </summary>
    public int End { get; set; }

    /// <summary>
    /// Gets the length of the selection.
    /// </summary>
    public int Length => System.Math.Max(0, End - Start);

    /// <summary>
    /// Initializes a new instance of the <see cref="TextSelection"/> class.
    /// </summary>
    public TextSelection()
    {
        Start = 0;
        End = 0;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="TextSelection"/> class with specified range.
    /// </summary>
    /// <param name="start">The start position.</param>
    /// <param name="end">The end position.</param>
    public TextSelection(int start, int end)
    {
        Start = start;
        End = end;
    }

    /// <summary>
    /// Validates that the selection is within the given text bounds.
    /// </summary>
    /// <param name="textLength">The maximum text length.</param>
    /// <returns>True if the selection is valid, false otherwise.</returns>
    public bool IsValid(int textLength)
    {
        return Start >= 0 && End <= textLength && Start <= End;
    }

    /// <summary>
    /// Normalizes the selection to ensure it's within valid bounds.
    /// </summary>
    /// <param name="textLength">The maximum text length.</param>
    public void Normalize(int textLength)
    {
        Start = System.Math.Max(0, System.Math.Min(Start, textLength));
        End = System.Math.Max(Start, System.Math.Min(End, textLength));
    }

    /// <summary>
    /// Copies values from another text selection.
    /// </summary>
    /// <param name="other">The selection to copy from.</param>
    public void CopyFrom(TextSelection other)
    {
        Start = other.Start;
        End = other.End;
    }
}

