// #region Imports
// #endregion
/**
 * Regex that matches CSS-unit suffixes (px, em, rem, %, fr, vw, vh, etc.).
 * Used by `parseGridLength` to reject unsupported unit strings.
 *
 * @internal
 */
const CSS_UNIT_PATTERN = /\d\s*(px|em|rem|%|fr|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|svh|svw|dvh|dvw|lvh|lvw)\s*$/i;
/**
 * Immutable value object that describes the sizing of a single grid track.
 *
 * Three sizing modes are supported:
 *
 * - **Absolute (pixel)** – a fixed size in device-independent pixels.
 * - **Auto** – the track sizes to fit its content.
 * - **Star** – remaining free space is distributed proportionally.
 *
 * Instances are created either via the constructor or through the
 * `parseGridLength` helper that accepts shorthand string / number
 * forms.
 *
 * @example
 * ```ts
 * const pixel  = new GridLength(120, 'pixel'); // 120 px
 * const auto   = new GridLength(1,   'auto');  // auto
 * const star   = new GridLength(2,   'star');  // 2*
 * const oneStar = new GridLength();            // 1* (default)
 * ```
 *
 * @public
 */
export class GridLength {
    // #region Fields
    _value;
    _type;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `GridLength` class.
     *
     * @param value The numeric component. Must be non-negative. Defaults to `1`.
     * @param type The unit type. Defaults to `'star'`.
     *
     * @throws {Error} If `value` is negative.
     *
     * @public
     */
    constructor(value = 1, type = 'star') {
        if (value < 0) {
            throw new Error(`GridLength value must be non-negative. Received: ${value}`);
        }
        this._value = value;
        this._type = type;
    }
    // #endregion
    // #region Properties
    /**
     * Gets the numeric component of this grid length.
     *
     * @public
     * @readonly
     */
    get value() {
        return this._value;
    }
    /**
     * Gets the unit type of this grid length.
     *
     * @public
     * @readonly
     */
    get type() {
        return this._type;
    }
    /**
     * Returns `true` when the track uses absolute (pixel) sizing.
     *
     * @public
     * @readonly
     */
    get isAbsolute() {
        return this._type === 'pixel';
    }
    /**
     * Returns `true` when the track uses automatic (content-based) sizing.
     *
     * @public
     * @readonly
     */
    get isAuto() {
        return this._type === 'auto';
    }
    /**
     * Returns `true` when the track uses star (proportional) sizing.
     *
     * @public
     * @readonly
     */
    get isStar() {
        return this._type === 'star';
    }
    // #endregion
    // #region Methods
    /**
     * Returns a human-readable string representation.
     *
     * @returns `"auto"` | `"{value}"` | `"{value}*"` (or `"*"` when value is 1).
     *
     * @public
     */
    toString() {
        if (this._type === 'auto') {
            return 'auto';
        }
        if (this._type === 'star') {
            return this._value === 1
                ? '*'
                : `${this._value}*`;
        }
        return `${this._value}`;
    }
}
/**
 * Parses a string or number into a `GridLength` instance.
 *
 * **Accepted formats**
 *
 * | Input          | Result                        |
 * |----------------|-------------------------------|
 * | `120`          | `GridLength(120, 'pixel')`    |
 * | `"120"`        | `GridLength(120, 'pixel')`    |
 * | `"12.5"`       | `GridLength(12.5, 'pixel')`   |
 * | `"auto"`       | `GridLength(1, 'auto')`       |
 * | `"*"`          | `GridLength(1, 'star')`       |
 * | `"2*"`         | `GridLength(2, 'star')`       |
 * | `"0.5*"`       | `GridLength(0.5, 'star')`     |
 *
 * **Rejected inputs** – any CSS unit suffix (`px`, `em`, `%`, `fr`, …),
 * negative values, case variations like `"Auto"` or `"AUTO"`, or
 * otherwise unrecognized strings will throw.
 *
 * @public
 * @param value A number or string to parse.
 * @returns A new `GridLength` instance.
 * @throws {Error} When the input does not match any accepted format.
 * @throws {Error} When a negative value is encountered.
 */
export function parseGridLength(value) {
    // Numeric input → absolute (pixel) sizing
    if (typeof value === 'number') {
        if (value < 0) {
            throw new Error(`GridLength value must be non-negative. Received: ${value}`);
        }
        return new GridLength(value, 'pixel');
    }
    const trimmed = value.trim();
    if (trimmed.length === 0) {
        throw new Error('GridLength value must not be empty.');
    }
    // Reject CSS units early
    if (CSS_UNIT_PATTERN.test(trimmed)) {
        throw new Error(`CSS units are not supported in GridLength. Only pixel (number), "auto", and star (*) sizing ` +
            `are allowed. Received: "${value}"`);
    }
    // Auto (strict lowercase only)
    if (trimmed === 'auto') {
        return new GridLength(1, 'auto');
    }
    // Star: "*" or "n*"
    if (trimmed.endsWith('*')) {
        const factorPart = trimmed.slice(0, -1).trim();
        const factor = factorPart.length === 0
            ? 1
            : Number(factorPart);
        if (isNaN(factor)) {
            throw new Error(`Invalid star factor in GridLength. Expected a number before '*'. Received: "${value}"`);
        }
        if (factor < 0) {
            throw new Error(`GridLength star factor must be non-negative. Received: "${value}"`);
        }
        return new GridLength(factor, 'star');
    }
    // Absolute (pixel)
    const numeric = Number(trimmed);
    if (isNaN(numeric)) {
        throw new Error(`Cannot parse GridLength from string: "${value}". ` +
            `Accepted formats: a non-negative number, "auto", "*", or "n*".`);
    }
    if (numeric < 0) {
        throw new Error(`GridLength value must be non-negative. Received: "${value}"`);
    }
    return new GridLength(numeric, 'pixel');
}
//# sourceMappingURL=GridLength.js.map