import { Host, h } from "@stencil/core";
import { PlatformServiceName, } from "@limetech/lime-web-components";
/**
 * Renders its slot only when the configured rule evaluates true.
 *
 * Pass a {@link Rule} and the gate compiles it on load and on prop
 * changes, then evaluates it against a scope obtained from the
 * {@link RuleRegistry} platform service.
 *
 * @exampleComponent limebb-example-rule-gate
 *
 * @alpha
 * @private
 */
export class RuleGate {
    constructor() {
        this.shouldRender = false;
    }
    componentWillLoad() {
        this.compileAndEvaluate();
    }
    onRuleChange() {
        this.compileAndEvaluate();
    }
    onScopeChange() {
        this.compileAndEvaluate();
    }
    render() {
        return (h(Host, { key: '365df6908301371b8b964671382ad936971b6624', hidden: !this.shouldRender }, h("slot", { key: 'ca7c2a9febf0189dfae882a2ae894d1d0262a213' })));
    }
    get ruleRegistry() {
        return this.platform.get(PlatformServiceName.RuleRegistry);
    }
    compileAndEvaluate() {
        var _a;
        if (!this.rule) {
            this.shouldRender = true;
            return;
        }
        try {
            const compiled = this.ruleRegistry.compile(this.rule);
            const scope = (_a = this.scope) !== null && _a !== void 0 ? _a : this.ruleRegistry.scope({ host: this.host });
            this.shouldRender = compiled(scope);
        }
        catch (error) {
            console.warn('rule-gate failed to compile rule; hiding slot', {
                rule: this.rule,
                error,
            });
            this.shouldRender = false;
        }
    }
    static get is() { return "limebb-rule-gate"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["rule-gate.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["rule-gate.css"]
        };
    }
    static get properties() {
        return {
            "platform": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentPlatform",
                    "resolved": "LimeWebComponentPlatform",
                    "references": {
                        "LimeWebComponentPlatform": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentPlatform",
                            "referenceLocation": "LimeWebComponentPlatform"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false
            },
            "rule": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "Rule | undefined",
                    "resolved": "RuleAll | RuleAny | RuleNot | RuleRef | undefined",
                    "references": {
                        "Rule": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::Rule",
                            "referenceLocation": "Rule"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The rule to evaluate. When unset, the gate renders its slot\nunconditionally \u2014 \"no rule\" means \"no gating\", which mirrors\nthe editor's `Rule | undefined` emit contract."
                },
                "getter": false,
                "setter": false
            },
            "scope": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "ContextScope",
                    "resolved": "ContextScope<keyof ContextMap> | undefined",
                    "references": {
                        "ContextScope": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::ContextScope",
                            "referenceLocation": "ContextScope"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Optional scope override. By default the gate resolves subject\nvalues by walking up the DOM to ancestors that registered providers\nwith the rule registry. Pass an explicit scope to bypass that walk."
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get states() {
        return {
            "shouldRender": {}
        };
    }
    static get elementRef() { return "host"; }
    static get watchers() {
        return [{
                "propName": "rule",
                "methodName": "onRuleChange"
            }, {
                "propName": "scope",
                "methodName": "onScopeChange"
            }];
    }
}
