import streamlit as st
from pathlib import Path

assets = Path(__file__).parent / "assets"

html = (assets / "index.html").read_text(encoding="utf-8")
css  = (assets / "styles.css").read_text(encoding="utf-8")
js   = (assets / "script.js").read_text(encoding="utf-8")

_rule_editor = st.components.v2.component(
    name="rule_editor",
    html=html,
    css=css,
    js=js,
    isolate_styles=False
)

def rule_editor(slugs,init_data=None):
    """Complete Rule Editor — now uses single `data=` dict (exactly as you suggested)"""
    # Smart defaults so you don't have to pass everything every time
    defaults = {
        "events": [],
        "slugs": slugs,
        "variables": [],
        "condition_boxes": [],
        "constants": ["Heat4h", "Volatility", "Direction", "Heat10m", "Pace24h", "Pace7d", "Pace30d", "PaceAVG", "Peak", "Bucket", "Volume", "Progress", "Count", "Top", "Center", "Bottom", "RHours", "Price", "PriceAVG", "PnL", "pPnL" ],
        "operators": ["AND", "OR", "NOT", ">", "<", ">=", "<=", "==", "!=", "+", "-", "*", "/", "(", ")"],

    }
    if init_data is None:
        data = defaults
    else:
        data=init_data
    for k, v in defaults.items():
        if k not in data:
            data[k] = v

    # Pass as SINGLE data= object (this is the fix)
    result = _rule_editor(
        data=data   # ← wrapped exactly as you wanted
    )
    return result
