/**
 * Style resolver for the form_styles.json configuration system
 * Handles token substitution, inheritance resolution, and CSS conversion
 */
/**
 * Resolve token references in a value
 * e.g., "{colors.primary}" -> "#1e3a5f"
 */
export function resolve_tokens(value, tokens) {
    if (!value || typeof value !== "string")
        return value;
    return value.replace(/\{(\w+)\.(\w+)\}/g, (match, category, key) => {
        const token_category = tokens[category];
        if (token_category && typeof token_category === "object" && key in token_category) {
            return token_category[key];
        }
        console.warn(`Unknown token: ${match}`);
        return match;
    });
}
/**
 * Resolve a style class with inheritance chain and token substitution
 */
export function resolve_style_class(class_name, styles_config, cache = new Map()) {
    if (cache.has(class_name)) {
        return cache.get(class_name);
    }
    const style_def = styles_config.styles[class_name];
    if (!style_def) {
        console.warn(`Unknown style class: ${class_name}`);
        return {};
    }
    let base_style = {};
    if (style_def.extends) {
        base_style = resolve_style_class(style_def.extends, styles_config, cache);
    }
    const merged = { ...base_style };
    const tokens = styles_config.tokens;
    for (const [key, value] of Object.entries(style_def)) {
        if (key === "extends")
            continue;
        if (typeof value === "string") {
            merged[key] = resolve_tokens(value, tokens);
        }
        else {
            merged[key] = value;
        }
    }
    cache.set(class_name, merged);
    return merged;
}
/**
 * Convert ResolvedStyle to React inline style object
 */
export function to_inline_style(resolved) {
    const style = {};
    if (resolved.color)
        style.color = resolved.color;
    if (resolved.background_color)
        style.backgroundColor = resolved.background_color;
    if (resolved.border_color)
        style.borderColor = resolved.border_color;
    if (resolved.border_width)
        style.borderWidth = resolved.border_width;
    if (resolved.border_radius)
        style.borderRadius = resolved.border_radius;
    if (resolved.font_family)
        style.fontFamily = resolved.font_family;
    if (resolved.font_size)
        style.fontSize = resolved.font_size;
    if (resolved.font_weight)
        style.fontWeight = resolved.font_weight;
    if (resolved.line_height)
        style.lineHeight = resolved.line_height;
    if (resolved.letter_spacing)
        style.letterSpacing = resolved.letter_spacing;
    if (resolved.text_align)
        style.textAlign = resolved.text_align;
    if (resolved.min_width)
        style.minWidth = resolved.min_width;
    if (resolved.min_height)
        style.minHeight = resolved.min_height;
    if (resolved.opacity !== undefined)
        style.opacity = resolved.opacity;
    if (resolved.padding) {
        style.padding = resolved.padding;
    }
    else if (resolved.padding_x || resolved.padding_y) {
        if (resolved.padding_x) {
            style.paddingLeft = resolved.padding_x;
            style.paddingRight = resolved.padding_x;
        }
        if (resolved.padding_y) {
            style.paddingTop = resolved.padding_y;
            style.paddingBottom = resolved.padding_y;
        }
    }
    if (resolved.margin)
        style.margin = resolved.margin;
    if (resolved.margin_left)
        style.marginLeft = resolved.margin_left;
    if (resolved.margin_top)
        style.marginTop = resolved.margin_top;
    if (resolved.indent && !resolved.margin_left) {
        style.marginLeft = resolved.indent;
    }
    return style;
}
/**
 * Create a memoized style resolver for a given styles configuration
 */
export function create_style_resolver(styles_config) {
    const cache = new Map();
    return {
        resolve: (class_name) => resolve_style_class(class_name, styles_config, cache),
        to_style: (class_name) => to_inline_style(resolve_style_class(class_name, styles_config, cache)),
        get_value: (class_name, property) => {
            const resolved = resolve_style_class(class_name, styles_config, cache);
            return resolved[property];
        },
        merge: (...class_names) => {
            const merged = {};
            for (const class_name of class_names) {
                const resolved = resolve_style_class(class_name, styles_config, cache);
                Object.assign(merged, resolved);
            }
            return merged;
        },
        merge_to_style: (...class_names) => {
            const merged = {};
            for (const class_name of class_names) {
                const resolved = resolve_style_class(class_name, styles_config, cache);
                Object.assign(merged, resolved);
            }
            return to_inline_style(merged);
        },
        clear_cache: () => cache.clear(),
        has_style: (class_name) => class_name in styles_config.styles,
        get_style_names: () => Object.keys(styles_config.styles),
        get_tokens: () => styles_config.tokens,
        resolve_token: (token_path) => {
            const match = token_path.match(/^\{(\w+)\.(\w+)\}$/);
            if (!match)
                return token_path;
            const [, category, key] = match;
            const token_category = styles_config.tokens[category];
            if (token_category && typeof token_category === "object" && key in token_category) {
                return token_category[key];
            }
            return undefined;
        },
    };
}
export function merge_with_overrides(resolved, overrides) {
    return {
        ...to_inline_style(resolved),
        ...overrides,
    };
}
//# sourceMappingURL=style_resolver.js.map