function isStyleProps(arg) {
    return arg !== null && typeof arg === "object" && !("type" in arg);
}
function splitStyleAndKey(props) {
    const { key, ...style } = props;
    return { style: Object.keys(style).length > 0 ? style : undefined, key };
}
function makeBox(extraStyle, styleOrChild, rest) {
    const baseStyle = Object.keys(extraStyle).length > 0 ? extraStyle : undefined;
    if (styleOrChild === undefined) {
        return { type: "box", style: baseStyle };
    }
    if (isStyleProps(styleOrChild)) {
        const { style, key } = splitStyleAndKey(styleOrChild);
        const mergedStyle = style || baseStyle ? { ...style, ...extraStyle } : undefined;
        return {
            type: "box",
            style: mergedStyle,
            key,
            children: rest.length > 0 ? rest : undefined,
        };
    }
    return { type: "box", style: baseStyle, children: [styleOrChild, ...rest] };
}
export function box(styleOrChild, ...children) {
    return makeBox({}, styleOrChild, children);
}
export function row(styleOrChild, ...children) {
    return makeBox({ flexDirection: "row" }, styleOrChild, children);
}
export function column(styleOrChild, ...children) {
    return makeBox({ flexDirection: "column" }, styleOrChild, children);
}
export function text(content) {
    return { type: "text", content };
}
// Single-line text row. Sets `height: 1` so the element doesn't
// stretch via the default `flex: 1` when placed inside a column,
// which is the source of the "every row triple-spaces itself"
// surprise. Caller-provided style is merged on top so `line("hi",
// { height: 2 })` still works.
export function line(content, style) {
    return { type: "text", content, style: { height: 1, ...style } };
}
// Convenience: a column of `line()`s. `justifyContent: "flex-start"`
// stops the layout engine from distributing leftover space between
// the children if their combined height is less than the viewport.
export function lines(strings, style) {
    return column({ justifyContent: "flex-start", ...style }, ...strings.map((s) => line(s)));
}
export function list(style, items, selectedIndex) {
    const { style: resolvedStyle, key } = splitStyleAndKey(style);
    return { type: "list", style: resolvedStyle, key, items, selectedIndex };
}
export function textInput(style, value) {
    const { style: resolvedStyle, key } = splitStyleAndKey(style);
    return { type: "textInput", style: resolvedStyle, key, value };
}
