// #region Imports
import { GridLength, parseGridLength } from './GridLength';
import { RowDefinition } from './RowDefinition';
/**
 * @public
 */
export var RowDefinitions;
(function (RowDefinitions) {
    /**
     * Parses a `RowDefinitions` value (array or space-separated
     * string) into validated `RowDefinition` instances.
     *
     * When `self` is a `string`, it is first split on whitespace into
     * individual tokens, each of which must be a valid
     * `GridLengthInput` literal (`number`, `'auto'`, `'*'`,
     * `` `${number}*` ``).
     *
     * When `self` is an array, each element is validated strictly:
     * - Numbers must be non-negative (absolute pixel sizing).
     * - String literals are parsed via `parseGridLength`.
     * - Object entries must carry a valid `height` and optional
     *   non-negative `minHeight` / `maxHeight` where `minHeight ≤ maxHeight`.
     *
     * @public
     * @param self The raw definitions to parse.
     * @returns An array of fully validated `RowDefinition` instances.
     * @throws {Error} If any entry is invalid.
     */
    function parse(self) {
        const entries = typeof self === 'string'
            ? fromString(self)
            : self;
        return entries.map((entry, index) => {
            try {
                if (typeof entry === 'number' || typeof entry === 'string') {
                    return new RowDefinition(parseGridLength(entry));
                }
                const height = entry.height instanceof GridLength
                    ? entry.height
                    : parseGridLength(entry.height);
                return new RowDefinition(height, entry.minHeight ?? 0, entry.maxHeight ?? Number.MAX_SAFE_INTEGER);
            }
            catch (error) {
                const message = error instanceof Error ? error.message : String(error);
                throw new Error(`Invalid RowDefinition at index ${index}: ${message}`);
            }
        });
    }
    RowDefinitions.parse = parse;
    /**
     * Parses a space-separated string of grid-length tokens into an
     * array of `RowDefinitionInput` values.
     *
     * Each whitespace-delimited token is interpreted as a
     * `GridLengthInput`: a non-negative number (pixels), `"auto"`,
     * `"*"`, or `"n*"` (star sizing).
     *
     * @example
     * ```ts
     * RowDefinitions.fromString('auto * 100');
     * // → ['auto', '*', 100]
     * ```
     *
     * @public
     * @param value The space-separated definition string.
     * @returns An array of parsed `RowDefinitionInput` values.
     * @throws {Error} If the string is empty or any token is invalid.
     */
    function fromString(value) {
        const trimmed = value.trim();
        if (trimmed.length === 0) {
            return [];
        }
        return trimmed.split(/\s+/).map((token, index) => {
            try {
                if (token === 'auto' || token === '*' || token.endsWith('*')) {
                    parseGridLength(token);
                    return token;
                }
                const numeric = Number(token);
                if (isNaN(numeric)) {
                    throw new Error(`Accepted formats: a non-negative number, "auto", "*", or "n*".`);
                }
                parseGridLength(numeric);
                return numeric;
            }
            catch (error) {
                const message = error instanceof Error ? error.message : String(error);
                throw new Error(`Invalid row definition token at index ${index} ("${token}"): ${message}`);
            }
        });
    }
    RowDefinitions.fromString = fromString;
    /**
     * Serializes a `RowDefinitions` value into a space-separated
     * string suitable for use as an HTML attribute value.
     *
     * Each entry is converted to its shortest `GridLength` string
     * representation via `GridLength.toString`. Object entries
     * (with `minHeight`/`maxHeight`) are serialized using their `height`
     * component only, as min/max constraints cannot be expressed in the
     * string format.
     *
     * @example
     * ```ts
     * RowDefinitions.toString(['auto', '*', 100]);
     * // → 'auto * 100'
     * ```
     *
     * @public
     * @param self The definitions to serialize.
     * @returns A space-separated string representation.
     */
    function toString(self) {
        if (typeof self === 'string') {
            return self;
        }
        return self.map((entry) => {
            if (typeof entry === 'number') {
                return `${entry}`;
            }
            if (typeof entry === 'string') {
                return entry;
            }
            const height = entry.height instanceof GridLength
                ? entry.height
                : parseGridLength(entry.height);
            return height.toString();
        }).join(' ');
    }
    RowDefinitions.toString = toString;
})(RowDefinitions || (RowDefinitions = {}));
//# sourceMappingURL=RowDefinitions.js.map