// #region Imports
import { ColumnDefinition } from './ColumnDefinition';
import { GridLength, parseGridLength } from './GridLength';
/**
 * @public
 */
export var ColumnDefinitions;
(function (ColumnDefinitions) {
    /**
     * Parses a `ColumnDefinitions` value (array or space-separated
     * string) into validated `ColumnDefinition` 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 `width` and optional
     *   non-negative `minWidth` / `maxWidth` where `minWidth ≤ maxWidth`.
     *
     * @public
     * @param self The raw definitions to parse.
     * @returns An array of fully validated `ColumnDefinition` 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 ColumnDefinition(parseGridLength(entry));
                }
                const width = entry.width instanceof GridLength
                    ? entry.width
                    : parseGridLength(entry.width);
                return new ColumnDefinition(width, entry.minWidth ?? 0, entry.maxWidth ?? Number.MAX_SAFE_INTEGER);
            }
            catch (error) {
                const message = error instanceof Error ? error.message : String(error);
                throw new Error(`Invalid ColumnDefinition at index ${index}: ${message}`);
            }
        });
    }
    ColumnDefinitions.parse = parse;
    /**
     * Parses a space-separated string of grid-length tokens into an
     * array of `ColumnDefinitionInput` values.
     *
     * Each whitespace-delimited token is interpreted as a
     * `GridLengthInput`: a non-negative number (pixels), `"auto"`,
     * `"*"`, or `"n*"` (star sizing).
     *
     * @example
     * ```ts
     * ColumnDefinitions.fromString('* 2* auto 120');
     * // → ['*', '2*', 'auto', 120]
     * ```
     *
     * @public
     * @param value The space-separated definition string.
     * @returns An array of parsed `ColumnDefinitionInput` 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 column definition token at index ${index} ("${token}"): ${message}`);
            }
        });
    }
    ColumnDefinitions.fromString = fromString;
    /**
     * Serializes a `ColumnDefinitions` 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 `minWidth`/`maxWidth`) are serialized using their `width`
     * component only, as min/max constraints cannot be expressed in the
     * string format.
     *
     * @example
     * ```ts
     * ColumnDefinitions.toString(['*', '2*', 'auto', 120]);
     * // → '* 2* auto 120'
     * ```
     *
     * @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 width = entry.width instanceof GridLength
                ? entry.width
                : parseGridLength(entry.width);
            return width.toString();
        }).join(' ');
    }
    ColumnDefinitions.toString = toString;
})(ColumnDefinitions || (ColumnDefinitions = {}));
//# sourceMappingURL=ColumnDefinitions.js.map