"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_STYLE = void 0;
const element_1 = require("../../../../client/element");
const style_1 = require("../../../../client/style");
exports.DEFAULT_STYLE = {};
class Inherit extends element_1.Element {
    constructor(props, $system) {
        super(props, $system);
        this._unlisten = [];
        this._styles = [];
        this._registerChild = (child, at) => {
            const style = this._current('style');
            if (child.$wrapElement) {
                const wrap_style = (0, style_1.getStyleObj)(child.$element);
                this._styles.push(wrap_style);
                (0, style_1.mergeStyle)(child.$wrapElement, style);
            }
            else {
                const base = child.getRootLeaves();
                for (const leaf of base) {
                    const unlisten = leaf.interceptProp('style', (leaf_style) => {
                        const style = this._current('style');
                        const i = this._unlisten.indexOf(unlisten);
                        this._styles[i] = leaf_style;
                        return {
                            ...style,
                            ...leaf_style,
                        };
                    });
                    const leaf_style = leaf.getProp('style') ?? {};
                    this._styles.push(leaf_style);
                    this._unlisten.push(unlisten);
                    leaf.refreshProp('style');
                }
            }
        };
        this._unregisterChild = (child) => {
            if (child.$wrapElement) {
                this._styles.splice(0, 1);
            }
            else {
                const base = child.getRootLeaves();
                for (let i = 0; i < base.length; i++) {
                    const unlisten = this._unlisten[i];
                    if (!unlisten) {
                        return;
                    }
                    const leaf = base[i];
                    unlisten();
                    leaf.refreshProp('style');
                }
                this._unlisten.splice(0, base.length);
                this._styles.splice(0, base.length);
            }
        };
        const { api: { document: { createElement }, }, } = $system;
        const $element = createElement('div');
        $element.classList.add('inherit');
        $element.style.display = 'contents';
        this.$wrap = true;
        this.$element = $element;
        this.$primitive = true;
        this.$slot = {
            default: this,
        };
    }
    _insertAt(parent, child, at) {
        super._insertAt(parent, child, at);
        this._registerChild(child, at);
    }
    _removeChild(child) {
        this._unregisterChild(child);
        super._removeChild(child);
    }
    onPropChanged(name, style) {
        // if (name === 'style') {
        for (const child of this.$domChildren) {
            if (child.$wrapElement) {
                (0, style_1.mergeStyle)(child.$wrapElement, style);
            }
            else {
                const base = child.getRootLeaves();
                for (const leaf_comp of base) {
                    leaf_comp.refreshProp('style');
                }
            }
        }
        // }
    }
}
exports.default = Inherit;
//# sourceMappingURL=Component.js.map